$(document).ready(function(){

  //If US show states
  if (($("#address_country_id option:selected").text()) == 'United States')
  {
    $('.us-address').show();
    $('.non-us-address').hide();
    $('.non-us-address input').attr('disabled', 'disabled');
    $('.us-address select').attr('disabled', false);
  }
  else
  {
    $('.us-address').hide();
    $('.non-us-address').show();
    $('.us-address select').attr('disabled', 'disabled');
    $('.non-us-address input').attr('disabled', false);
  }

  $('#address_country_id').change(function()
  {  
    //If US show states
    if (($("#address_country_id option:selected").text()) == 'United States')
    {
      $('.us-address').show();
      $('.non-us-address').hide();
      $('.non-us-address input').attr('disabled', 'disabled');
      $('.us-address select').attr('disabled', false);
    }
    else
    {
      $('.us-address').hide();
      $('.non-us-address').show();
      $('.us-address select').attr('disabled', 'disabled');
      $('.non-us-address input').attr('disabled', false);
    }  
  });
  
  $('.small-product-image').click(function() {
    $('#large-product-image').attr('src', $(this).attr('src').replace('thumb','large'));    
  });
  
  
  $('.featured-products a').mouseenter(function(){
    if (parseInt($(this).attr('scrollHeight')) > 211)
    {
      $('<div></div>')
        .html($(this).html())
        .find('img').remove().end()
        .css({ bottom: 211 - $(this).attr('scrollHeight') })
        .appendTo($(this))
        .animate({ bottom: 0 }, 200);
    }
  }).mouseleave(function(){
    $(this).find('div').remove();
  });
  
  $('div#search input').focus(function(){
    if (this.value == $(this).data('default'))
      $(this).val('').removeClass('faded');
  }).blur(function(){
    if (this.value == '')
      $(this).val($(this).data('default')).addClass('faded');
  }).data('default', $('div#search input').val()).blur().parents('form').submit(function(){
    $(this).find('input').each(function(){
      if (this.value == $(this).data('default'))
        this.value = '';
    });
  });
  
  $('.popup').click(function(){ window.open(this.href); return false; });
  
  
  $('.select-variety').change(function(){
    form = this;
    $.getJSON('/catalogue/varieties', $(this).parents('form').find('select[name="varieties[]"]').serializeArray(), function(sku){
      if (sku.available == 1)
      {
        $('div.product-form').removeClass('unavailable')
        $('div.product-unavailable').remove();
      }
      else
      {
        if (!$('div.product-form').hasClass('unavailable'))
          $('div.product-form').append('<div class="product-unavailable">This item has been sold. It is possible that we may be able to find you another, or a very similar item. If you would like us to try to do this, please <a href="/page/contact-us">go to the contact form</a> and let us know.</div>')
        $('div.product-form').addClass('unavailable');
      }
      
      $(form).parents('form')
        .find('input[name="sales_order_item[sku_id]"]').val(sku.id).end()
        .find('.price').html(sku.price);
    });
  });
  
  $('.category-products a.product').mouseenter(function(){ $(this).addClass('shover'); }).mouseleave(function(){ $(this).removeClass('shover'); });
  $('.products a.product').mouseenter(function(){ $(this).addClass('shover'); }).mouseleave(function(){ $(this).removeClass('shover'); });
  
});


function handleCheckoutAddress()
{
  $(document).ready(function(){
    if (!$('.checkout-address').hasClass('enabled'))
      $('.checkout-address').css({ display: 'none', opacity: 0});
  });
  $('input[type="radio"]').change(function(){
    if (this.id == 'new-address' && this.checked)
      $('.checkout-address').animate({ height:"show", opacity: 1});
    else
      $('.checkout-address').animate({ height:"hide", opacity: 0});
  });
}

function decreaseQty(a)
{
  input = $(a).parents('td').find('input');
  input.val((parseInt(input.val()) > 0) ? parseInt(input.val()) - 1 : 0);
  return false;
}

function increaseQty(a)
{
  input = $(a).parents('td').find('input');
  input.val(parseInt(input.val()) + 1);
  return false;
}
