// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

$(function() {
  $("#slider-panes").jCarouselLite({
    btnNext: "#slider-next",
    btnPrev: "#slider-prev"
  });

  // Rounded Corners
  $('body.pages-home #side-content #offer').corner("round 15px");

  // Smooth Scrolling
  // http://blog.medianotions.de/en/articles/2009/smoothscroll-for-jquery
  $('a[href*=#]').click(function(event) {
     var newHash=this.hash; // get / set parameters
     var target=$(document.getElementById(this.hash.replace(/^#/, ''))).offset().top;
     var oldLocation=window.location.href.replace(window.location.hash, '');
     var newLocation=this;
     if(oldLocation+newHash==newLocation) { // make sure it's the same location
        $('html:not(:animated),body:not(:animated)').animate({ scrollTop: target - 180 }, 500, 'swing');
        return false; // cancel default click action
     }
  });
  
  // On form submit, disable submit button
  $('form').submit(function(){
      $('input[type=submit], button[type=submit]', this).attr('disabled', true);
  });

  // Checkout
  $('#order_same_billing_address').click(function(event) {
    if ($("#order_same_billing_address").is(":checked")) {
      $('#order_billing_first_name').attr('disabled', true)
        .val($('#order_customer_first_name').val());
      $('#order_billing_last_name').attr('disabled', true)
        .val($('#order_customer_last_name').val());
      $('#order_billing_address_1').attr('disabled', true)
        .val($('#order_customer_address_1').val());
      $('#order_billing_address_2').attr('disabled', true)
        .val($('#order_customer_address_2').val());
      $('#order_billing_city').attr('disabled', true)
        .val($('#order_customer_city').val());
      $('#order_billing_state').attr('disabled', true)
        .val($('#order_customer_state').val());
      $('#order_billing_zip_code').attr('disabled', true)
        .val($('#order_customer_zip_code').val());
    } else {
      $('#order_billing_first_name').removeAttr('disabled');
      $('#order_billing_last_name').removeAttr('disabled');
      $('#order_billing_address_1').removeAttr('disabled');
      $('#order_billing_address_2').removeAttr('disabled');
      $('#order_billing_city').removeAttr('disabled');
      $('#order_billing_state').removeAttr('disabled');
      $('#order_billing_zip_code').removeAttr('disabled');
    };
  });
  
  $('#order_customer_first_name').keyup(function(event) {
    if ($("#order_same_billing_address").is(":checked"))
      $('#order_billing_first_name').val($(this).val());
  })
  $('#order_customer_last_name').keyup(function(event) {
    if ($("#order_same_billing_address").is(":checked"))
      $('#order_billing_last_name').val($(this).val());
  })
  $('#order_customer_address_1').keyup(function(event) {
    if ($("#order_same_billing_address").is(":checked"))
      $('#order_billing_address_1').val($(this).val());
  })
  $('#order_customer_address_2').keyup(function(event) {
    if ($("#order_same_billing_address").is(":checked"))
      $('#order_billing_address_2').val($(this).val());
  })
  $('#order_customer_city').keyup(function(event) {
    if ($("#order_same_billing_address").is(":checked"))
      $('#order_billing_city').val($(this).val());
  })
  $('#order_customer_state').change(function(event) {
    if ($("#order_same_billing_address").is(":checked"))
      $('#order_billing_state').val($(this).val());
  })
  $('#order_customer_zip_code').keyup(function(event) {
    if ($("#order_same_billing_address").is(":checked"))
      $('#order_billing_zip_code').val($(this).val());
  })
});
