jQuery(document).ready(function(){
  jQuery('div.store-item-image a').fancybox();
  
  jQuery('div.inline-label label').inFieldLabels();
  
  if(jQuery('#shipping_same').is(':checked')) {
    jQuery('#shipping_address').hide();
  }

  jQuery('#shipping_same').click(function(){
    jQuery('#shipping').slideToggle();
  });
  
  jQuery('input#shipping_same').click(function() {
    if(jQuery('input#shipping_same').is(':checked')) {
      jQuery('select#shipping_country').val(jQuery('select#billing_country').val());
      jQuery('textarea#shipping_address').val(jQuery('textarea#billing_address').val());
      if(jQuery('select#billing_country').val() == '') {
        jQuery('div#shipping-details').html('<p>Shipping will be calculated once the "Billing information" has been provided to the left.</p>');
      }
      else {
        calculate_shipping(jQuery('select#billing_country').val());
      }
    }
    else {
      jQuery('select#shipping_country').val('');
      jQuery('textarea#shipping_address').val('');
      jQuery('div#shipping-details').html('<p>Shipping will be calculated once the "Shipping information" has been provided to the left.</p>');
    }
  });
  
  jQuery('select#billing_country').change(function(){
    if(jQuery('input#shipping_same').is(':checked')) {
      jQuery('select#shipping_country').val(jQuery(this).val());
      calculate_shipping(jQuery(this).val());
    }
  });
  
  jQuery('textarea#billing_address').keyup(function(){
    if(jQuery('input#shipping_same').is(':checked')) {
      jQuery('textarea#shipping_address').val(jQuery(this).val());
    }
  });
  
  jQuery('select#shipping_country').change(function(){
    calculate_shipping(jQuery(this).val());
  });  
  
  jQuery('input#form-submit').click(function(){
    jQuery('div#submit').fadeOut(function(){
      jQuery('div#processing').fadeIn();
    });
  });
  
  jQuery('a#submit_check').click(function(){
    var name              = jQuery('input#name').val();
    var email             = jQuery('input#email').val();
    var phone             = jQuery('input#phone').val();
    var organization      = jQuery('input#organization').val();
    var billing_address   = jQuery('textarea#billing_address').val();
    var billing_country   = jQuery('select#billing_country').val();
    var shipping_address  = jQuery('textarea#shipping_address').val();
    var shipping_country  = jQuery('select#shipping_country').val();
    var shipping_comments = jQuery('textarea#shipping_comments').val();
    var shipping_total    = jQuery('input#handling_cart').val();
    
    jQuery('div#submit').fadeOut(function(){
      jQuery('div#processing').fadeIn();
      jQuery.ajax({
        type: 'POST',
        url: '/index.php/cart/order',
        data: 'name=' + name + '&email=' + email + '&phone=' + phone + '&organization=' + organization + '&billing_address=' + billing_address + '&billing_country=' + billing_country + '&shipping_address=' + shipping_address + '&shipping_country=' + shipping_country + '&shipping_comments=' + shipping_comments + '&shipping_total=' + shipping_total,
        success: function(msg){
          jQuery('div#processing').fadeOut(function(){
            jQuery('div#success').fadeIn();
          });
        }
      });
    });
  });
  
});

function calculate_shipping(country) {
  jQuery.ajax({
    type: 'POST',
    url: '/index.php/cart/shipping/' + country,
    data: '',
    success: function(msg){
      jQuery('div#shipping-details').html(msg);
    }
  });
  
  jQuery.ajax({
    type: 'POST',
    url: '/index.php/cart/shipping/' + country + '/1',
    data: '',
    success: function(msg){
      jQuery('input[name="handling_cart"]').val(msg);
    }
  });
}
