/**
* Application-Wide Javascript
*/

/**
* Unobtrusive Javascript
* - Keep the views clear of javascripts by 
*   working on the DOM when ready
*/ 
$(document).ready(function() {

  $('.wholesale-request').change(function(e) {
    $('#wholesaler').toggle();
  });

  // handle user addresses
  userAddresses();

  // handle retailer locations
  retailerLocations();

  /*
  // display checkout shipping addresses
  $('#OrderShippingId').change(function(e){
    
    // value from the select menu
    var selected = $(this).val();
    
    // only replace html if not billing address
    if (selected > 0) {
      
      // get the address html
      var address = $('#shippingAddress' + selected).html();

      // replace the html with the new address
      $('#holderAddress').html(address);
      
    } else {
      
      // replace the html with the no address
      $('#holderAddress').html('');
    };
  });
  */
  
  
  
  /*
  // update the cart subtotal
  $('#CartRecalculate').submit(function(){
    
    // ensure field data exists
    if ($('#CartPostal').val() == '') {
      
      // display the error
      $('#CartPostalError').html('Please include your Postal Code.');
      
      // disable browser submit
      return false;
    } else {
    
      // clear the error text
      $('#CartPostalError').html('');
    }
    
    // fade the div while loading
    $('#recalculateDIV').stop('fx');
    $('#recalculateDIV').fadeTo('fast', 0.2);
    
    // make the ajax request
    $.ajax({
      data: $(this).serialize(),
      success: function(data){
        
        // insert the new html
        $('#recalculateDIV').html(data);
        
        // fade in the new html
        $('#recalculateDIV').stop('fx');
        $('#recalculateDIV').fadeTo('fast', 1);
      },
      type: 'POST',
      url: '/cart/recalculate'
    });
    
    // disable browser submit
    return false;
  });
  */
});

/**
* Handle the adding and removing of user address (usually Shipping Addresses)
*
* @access public
*/
function userAddresses() {
  // add shipping address fieldset
  $('.add-shipping-address').click(function(e) {
    e.preventDefault();

    // only use if save button exists
    if ($('#save-addresses').get() != '') {
      
      // add new address field
      $.get('/users/address/add', function(data) {
        $('.alertMessage').hide();
        $('#no-addresses').hide();
        $('#addresses').append(data);
      });
    }
  });
  
  // remove shipping address
  $('.remove-shipping-address').live('click', function(e) {
    e.preventDefault();
    
    if (confirm('Are you sure you want to permanently remove this shipping address?')) {
      $('.alertMessage').hide();

      // remove the address container
      var relID = $(this).attr('rel');
      if (relID != '') $.post($(this).attr('href'));

      $('fieldset#ShippingAddress' + relID).fadeOut('fast', function(e) {
        $(this).remove();
      });
    }
  });
};

/**
* Displays retailer locations based on province id
*
* @access public
*/
function retailerLocations() {
  
  $('.page.retailers .provinces').change(function(e) {
    
    // hide all
    $('.province').hide();
    
    // show all
    if ($(this).val() == '') $('.province').show();
    
    // show specific
    if ($(this).val() != '') $('.province.'+ $(this).val()).show();
  });
};