$(document).ready(function(){
  storeUpdateActions();
  storeOrderForm();
  storeSwitchSite();
});

function storeUpdateActions(){
  updateAction = function(elt, value){
    inputElt = $(elt).find('~ .value > input');
    inputElt.val(Math.max(0, parseInt(inputElt.val()) + value));
    $.ajax({
      url: '/store/updateQuantity/'+inputElt.attr('id').split('_')[1],
      data: { quantity: value },
      type: 'POST',
      success: function(html){
        $('#total').html(html);
      }
    });
  };

  $('.range_store .table_store_3 .td .list_actions .minus').click(function(){ updateAction(this, -1) });
  $('.range_store .table_store_3 .td .list_actions .plus').click(function(){ updateAction(this, +1) });
}

function storeOrderForm()
{
  $('#zd_store_order_department_block, #zd_store_order_dealer_block').hide();

  $('#zd_store_order_country').change(function(){
    if ($(this).val() == 'FR')
    {
      $('#zd_store_order_department_block, #zd_store_order_dealer_block').show();
    }
    else
    {
      $('#zd_store_order_department_block, #zd_store_order_dealer_block').hide();
      $('#zd_store_order_department, #zd_store_order_dealer').val('');
    }
  });

  $('#zd_store_order_department').change(function(){
    $('#zd_store_order_dealer_block').show();
    $('#zd_store_order_dealer').load('/store/ajaxDealers', {department_id: $(this).val()}).show();
  });
}

function storeSwitchSite()
{
  $('#store_country').change(function(){
    window.location.href = '/store/switchSite/site_id/'+$(this).val();
  });
}
