var currHomeImg = 0;
var slideshowImage = null;
var cachedOffset = null;
var zoomedImg = null;
var zoomRatio = 0;
var zoomLocked = false;
$(document).ready(function(){
  
  $(window).resize(function(){
    if ($(window).width() <= 1084)
      $('#allaround').css({ marginLeft: '-42px', width: '1024px', overflow: 'hidden' });
    else
      $('#allaround').css({ marginLeft: '0px', width: 'auto', overflow: 'visible' });
  }).resize();
  
  $('.share').click(share);
  $('.catalogue-popup').click(popup);
  
  $('.product-thumbs a').click(function(){
    $('#product-picture').attr('src', this.href).parent().attr('href', this.href);
    return false;
  });
  
  $('.basket-add').click(function(){
    qty = $(this).parent('td').find('input');
    qty.val(parseInt(qty.val()) + 1);
    return false;
  });
  
  $('.basket-subtract').click(function(){
    qty = $(this).parent('td').find('input');
    if (parseInt(qty.val()) > 0) qty.val(parseInt(qty.val()) - 1);
    return false;
  });
  
  $('.popup').click(function(){ 
    if ($(this).hasClass('terms'))
      window.open(this.href, 'terms', 'width=530,height=400');
    else
      window.open(this.href);
    return false; 
  });

  $('.select-variety').change(function(){
    form = this;
    $.getJSON('/catalogue/varieties', $(this).parents('form').find('select[name="sales_order_item[varieties][]"]').serializeArray(), function(sku){
      $(form).parents('form').find('input[name="sales_order_item[sku_id]"]').val(sku.id);
      $(form).parents('form').find('#product-price').html(sku.price);
    });
  });
  
  var miniBasketTimeout = null
  $('.mini-basket-form').submit(function(){
    clearTimeout(miniBasketTimeout);
    $('#bag').load(this.action, $(this).serializeArray(), function(){
      if ($('#bag-added').length == 0)
        $('#bag').append('<div id="bag-added"><div id="slit"></div><div id="bag-note">Items were added to your basket.</div></div>');
      $('#bag-added').stop().fadeIn(500, function(){ 
        $('#bag-note').stop().animate({ right: 1 });
        miniBasketTimeout = setTimeout(function(){ 
          $('#bag-note').animate({ right: -100 }, 500, function(){ 
            $('#bag-added').fadeOut(function(){ $(this).remove(); })
          });
        }, 2000);
      })
    });
    return false;
  });
  
  $('#trade-filter').click(filterTradeProducts).click();
  
  if ($('a[rel=lightbox]').length > 0)
    $('a[rel=lightbox]').lightBox();
});

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

function share()
{
  $('<div id="overlay"></div>').css({ opacity: 0.5 }).appendTo('body');
  $('<div id="share"></div>').appendTo('body');
  $.get(this.href,function(d){ $('#share').html(d); });
  $(window).resize(function(e){
    $('#overlay').css({ height: (($(window).height() > $(document.body).height()) ? $(window).height() : $(document.body).height()) + 'px', width: $(document.body).width() + 'px' });
    $('#share').css({ left: ($(window).width() / 2) - 165, top: ($(window).height() / 2) - 235 + (($(window).attr('scrollY')) ? $(window).attr('scrollY') : document.body.parentNode.scrollTop) });
  }).scroll(function(e){
    $('#share').css({ top: ($(window).height() / 2) - 235 + (($(window).attr('scrollY')) ? $(window).attr('scrollY') : document.body.parentNode.scrollTop) });
  }).resize();
  return false;
}
function closeShare()
{
  $('body').css({ overflow: 'auto' });
  $(window).unbind('resize').unbind('scroll');
  $('#overlay').remove();
  $('#share').remove();
}

function popup()
{
  $('<div id="overlay"></div>').css({ opacity: 0.5 }).appendTo('body');
  $('<div id="popup"></div>').appendTo('body');
  $.get(this.href,function(d){ $('#popup').html(d).prepend('<a href="#" onclick="closePopup(); return false;" class="fright"><img src="/images/close.png" alt="Close Window" /></a>'); });
  $(window).resize(function(e){
    $('#overlay').css({ height: $(document.body).height() + 'px' });
    $('#popup').css({ left: ($(window).width() / 2) - 265, top: ($(window).height() / 2) - 265 + (($(window).attr('scrollY')) ? $(window).attr('scrollY') : document.body.parentNode.scrollTop) });
  }).scroll(function(e){
    $('#popup').css({ top: ($(window).height() / 2) - 265 + (($(window).attr('scrollY')) ? $(window).attr('scrollY') : document.body.parentNode.scrollTop) });
  }).resize();
  return false;
}
function closePopup()
{
  $('body').css({ overflow: 'auto' });
  $(window).unbind('resize').unbind('scroll');
  $('#overlay').remove();
  $('#popup').remove();
}


/* Trade
 *****************************************************************************/
 
function filterTradeProducts()
{
  $('span#trade-loading').show();
  $('#trade-products-container').load('/trade/catalogue/products', $('#trade-form').serializeArray(), function(){
    $('#trade-products input.trade-sku').keyup(function(){
      
      if (parseFloat(this.value) > 0)
        $(this).parents('tr').addClass('highlight');
      else
        $(this).parents('tr').removeClass('highlight');
      
      currency = $(this).parents('tr').find('td.trade-item-price').html().substring(0,1);
      $(this).parents('tr').find('td.trade-item-value').html(currency + (parseInt(this.value) * parseFloat($(this).parents('tr').find('td.trade-item-price').html().substring(1))).toFixed(2));
      $('#trade-totals').load('/trade/order/totals', $('#trade-form').serializeArray());
    });
    
    $('a.trade-item-detail').click(function(){
      $('<div id="overlay"></div>').css({ opacity: 0.5 }).appendTo('body');
      $('<div id="popup"></div>').appendTo('body');
      $.get(this.href,function(d){ $('#popup').html(d).prepend('<a href="#" onclick="closePopup(); return false;" class="fright"><img src="/images/close.png" alt="Close Window" /></a>'); });
      $(window).resize(function(e){
        $('#overlay').css({ height: $(document.body).height() + 'px' });
        $('#popup').css({ left: ($(window).width() / 2) - 265, top: ($(window).height() / 2) - 265 + (($(window).attr('scrollY')) ? $(window).attr('scrollY') : document.body.parentNode.scrollTop) });
      }).scroll(function(e){
        $('#popup').css({ top: ($(window).height() / 2) - 265 + (($(window).attr('scrollY')) ? $(window).attr('scrollY') : document.body.parentNode.scrollTop) });
      }).resize();
      return false;
    });
    $('#trade-totals').load('/trade/order/totals', $('#trade-form').serializeArray());
    $('span#trade-loading').hide();
  });
}


jQuery.easing['jswing'] = jQuery.easing['swing'];

jQuery.extend( jQuery.easing,
{
	def: 'easeOutQuad',
	easeInQuad: function (x, t, b, c, d) {
		return c*(t/=d)*t + b;
	},
	easeOutQuad: function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	},
	easeInOutQuad: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t + b;
		return -c/2 * ((--t)*(t-2) - 1) + b;
	}
});
