$(document).ready(function() {

  $(".slider").each( function() {
   var value = $(this).find('input').val();
   $(this).slider({
       step: 1,
       min:0,
       max:10,
       value: value,
       start: function(event, ui) { $(this).find('p.counter').fadeIn('slow');},
       slide: function(event, ui) {        
        var value = ui.value;
        if (value <= 5) { $(this).find('p.counter').addClass('counter-left'); }
        if (value > 5) { $(this).find('p.counter').removeClass('counter-left'); }
         $(this).find('p.counter').html(ui.value);
         $(this).find('input').val(ui.value);
       },
       stop: function(event, ui) { $(this).find('p.counter').delay(400).fadeOut('slow'); }
   });   
  });
   

   function cost(isChecked) {
     
     $('#coupon').val('');
   	 $('#validate-coupon').html('');
     
     var pages = eval($('#thepages').val());
     var subs  = eval($('#thesubs').val());     
     var total_pages = pages * 449;

     if (subs < 5) { total_subs = subs * 100; }
     if (subs == 5) { total_subs = 490;}
     if (subs == 6) { total_subs = 570; }
     if (subs == 7) { total_subs = 650; }
     if (subs == 8) { total_subs = 730; }
     if (subs == 9) { total_subs = 810; }
     if (subs == 10) { total_subs = 890; }
     if (subs == 11) { total_subs = 970; }
     if (subs == 12) { total_subs = 1050; }
     if (subs == 13) { total_subs = 1130; }
     if (subs == 14) { total_subs = 1210; }
     if (subs == 15) { total_subs = 1290; }              
     
     if (isChecked) {
      var current_price   = eval($('input#amount').val());  
      var xpage           = 160;
      var xsub            = 80; 
      var pages_selected  = eval($('#thepages').val());
      var subs_selected   = eval($('#thesubs').val());     
      var pages_pre_total = pages_selected * xpage;
      var subs_pre_total  = subs_selected * xsub;     
      var pre_total       = pages_pre_total + subs_pre_total;     
      var with_slicing    = pre_total + current_price;     
      $('div#total p span').html(with_slicing);
      $('input#amount').val(with_slicing);
     }
     else {
      var total_without_slicing = total_pages + total_subs;
      $('div#total p span').html(total_without_slicing);
      $('input#amount').val(total_without_slicing);
     }     
   }
   
 
   $('#htmlcheck').click( function() {   	 
   	 var isChecked = $(this).attr('checked');
   	 $('#coupon').val('');
   	 $('#validate-coupon').html('');
     cost(isChecked);     
   });
   

   $(".sliderpage").each( function() {
    var valuepage = $(this).find('input').val(); 
    $(this).find('p.pcount').html(valuepage);
    $(this).slider({
       step: 1,
       min:1,
       max:10,
       value: valuepage,
       slide: function(event, ui) {
         $(this).find('p.pcount').html(ui.value);
         $('#thepages').val(ui.value);
         $('#htmlcheck').removeAttr("checked");
         cost();
       }
    });
   });
   
  $(".slidersub").each( function() {
   var valuesub = $(this).find('input').val(); 
   $(this).find('p.pcount').html(valuesub); 
   $(this).slider({
       step: 1,
       min:0,
       max:15,
       value: valuesub,
       slide: function(event, ui) {  
        $(this).find('p.pcount').html(ui.value);        
        $('#thesubs').val(ui.value);
        $('#htmlcheck').removeAttr("checked");
        cost();
       }
    });  
   });
     
   $('#color1,#color2,#color3,#color4,').ColorPicker({
    onSubmit: function(hsb, hex, rgb, el) { $(el).val(hex); $(el).ColorPickerHide(); },
    onBeforeShow: function () { $(this).ColorPickerSetColor(this.value); }
   })
  
   .bind('keyup', function(){ $(this).ColorPickerSetColor(this.value); });
   
   $('div#step2').hide();
 		$('a#bstep1').hide();
 		$('#submit').hide();
   $('a#bstep2').click( function() {
   		$('a#bstep1').show();
   		$('a#bstep2').hide();
   		$('#submit').show();
     $('div#step1').hide();
     $('div#step2').show();
     return false;
   });
   
   $('a#bstep1').click( function() {
   		$('a#bstep2').show();
   		$('a#bstep1').hide();
   		$('#submit').hide();
     $('div#step2').hide();
     $('div#step1').show();
     return false;
   });
   
  if ($('#cycle')) {   	
 	 $('#cycle').cycle({
  		fx: 'fade',
  		timeout: 0,
  		prev: '#prev',
  		next: '#next'
 	 });
  }
  
  if ($('#cycle-wrap')) {   	
 	 $('#cycle-wrap').cycle({
  		fx: 'fade',
  		timeout: 0,
  		prev: '#prev-show',
  		next: '#next-show'
 	 });
  }
  
  if ($('#t')) {
  $('div#tab2, div#tab3,div#tab4').hide();
  $('div#tabs ul li a').click(function(){
   var tab = $(this).attr('rel');
     $(this).addClass('active').parents('li').siblings().find('a').removeClass('active');   
     
     // show up its box
     $('div#'+tab).show().siblings().hide(); // don't try toggleClass at home.
     return false;
    });
  }  

  $('#explain').tooltip({showURL: false});
  
  $('a.w').hide();
  $("a.photos").fancybox();
  $("a.iframe").fancybox({
   'padding': 0,
   'type' : 'iframe',
   'width': 730,
   'height' : 430,
   'scrolling' : 'no'
  });  
  var toggled = false
  
  $('p.more a').click( function() {
    $(this).parents('div.item').find('a.w').toggle();
    $(this).parents('div.item').find('a.t').toggle();
    if (!toggled) {
     $(this).html('View design');
     toggled = true;
    }
    else {
     $(this).html('View wireframe');
     toggled = false;
    }    
    return false; 
  });
  
  $('p.more-show a').click( function() {
    $(this).parents('div.showcase').find('a.w').toggle();
    $(this).parents('div.showcase').find('a.t').toggle();
    if (!toggled) {
     $(this).html('View design');
     toggled = true;
    }
    else {
     $(this).html('View wireframe');
     toggled = false;
    }
    return false; 
  });
  
  $("#coupon").live('input paste',function(e){ 
  var item = $('#coupon').val();
  var per = $('#per').val(); 
   $.ajax({
     type: "POST",
     url: "http://wireframeplus.com/index.php/client/coupon",
     data: "coupon=" + item + "&per=" + per,
     success: function(item){     
     if (item == 'true') { 
      $('#validate-coupon').html('<p class="vc">Valid Coupon (-' + (per * 100) + '%)</p>');
      var price = $('div#total p span').html();
      var discount = Math.round(price - (price * per));
      $('div#total p span').html(discount);
      $('input#amount').val(discount);
     }// true
     else {
      $('#validate-coupon').html('<p class="ic">Invalid Coupon</p>');
     }
     }//success
    });  
  });
  
  $("#validate-coupon").ajaxStart(function(){ $(this).html('Validating...'); });
 
  $("#ufile").change(function(){
   var val = $(this).val();
   $('#fakeupload').val(val);
  });
  
});