/* Set some variables for homepage image timeout */
var homeImageTimeout = 3000;
var homeImageCycleDuration = 1000;
var totalHomeImages = 11;
var currentHomeImage = 1;
var nextHomeImage = false;

$(document).ready(function() {
  
  /* Product images on product detail page click to enlarge */
  $('.product-picture-thumb').click(function() {
    $('#product-image-large').attr('src', $(this).attr('href'));
    $('#product-image-large').parent().attr('href', $(this).attr('href').replace('large','largest'));
    return false;
  });
  
  if ($('a[rel=lightbox]').length > 0)
    $('a[rel=lightbox]').lightBox();
  
  /* Setup Gallery animation */
  if ($('.gallery').length > 0)
  {
    $('.gallery').append($('<img src="/images/gallery-hand-right.gif" id="gallery-right-arrow" />').mousedown(function(){
      changeGalleryDirection(1, 3);
    }).mouseup(function(){
      changeGalleryDirection(1, 15);
    }));
    $('.gallery').append($('<img src="/images/gallery-hand-left.gif" id="gallery-left-arrow" />').mousedown(function(){
      changeGalleryDirection(0, 3);
    }).mouseup(function(){
      changeGalleryDirection(0, 15);
    }));
    setTimeout('animateForwardGallery()', 2000);
  }
  
  /* Hovering on menu items */
  $('#navigation').find('LI').hover(function() {
    $(this).addClass('hover');
  }, function() {
    $(this).removeClass('hover');
  });
  
  if ($('#home-image-main').length > 0)
    cycleHomeImages();    
});

/* The function that does the work */
function cycleHomeImages()
{
  /* Choose the next image */
  if (currentHomeImage == totalHomeImages)
    nextHomeImage = 1;
  else
    nextHomeImage = currentHomeImage + 1;    
  /* Run the code after a certain amount of time */
  setTimeout(function() {
    /* Fade out the old image */
    $('#home-image-main').find('img.'+currentHomeImage).fadeOut(homeImageCycleDuration);
    /* Fade in the new one */
    $('#home-image-main').find('img.'+nextHomeImage).fadeIn(homeImageCycleDuration, function() {
      currentHomeImage = nextHomeImage;
      cycleHomeImages(); /* call the function again */
    });
  }, homeImageTimeout);
}

var galleryScrollSpeed = 15;
var galleryAnimatingItem = null;
var galleryDirection = 1;
function animateForwardGallery()
{
  galleryAnimatingItem = $('.gallery li:first');
  itemWidth = galleryAnimatingItem.width();
  leftMargin = (!isNaN(parseInt(galleryAnimatingItem.css('margin-left')))) ? parseInt(galleryAnimatingItem.css('margin-left')) : 0;
  galleryAnimatingItem.animate({ 'margin-left': (-1 * itemWidth) + 'px' },(itemWidth - (-1 * leftMargin)) * galleryScrollSpeed,'linear', function(){
    $(this).appendTo($(this).parent('ul')).css('margin', '0px');
    animateForwardGallery();
  });
}
function animateBackGallery()
{
  if (parseInt(galleryAnimatingItem.css('margin-left')) == 0)
  {
    galleryAnimatingItem = $('.gallery li:last');
    itemWidth = galleryAnimatingItem.width();
    galleryAnimatingItem
      .css('margin-left', (-1 * itemWidth) + 'px')
      .prependTo(galleryAnimatingItem.parent('ul'))
      .animate({ 'margin-left': '0px' }, itemWidth * galleryScrollSpeed,'linear', function(){ animateBackGallery(); })
  }
  else
  {
    galleryAnimatingItem.animate({ 'margin-left': '0px' }, parseInt(galleryAnimatingItem.css('margin-left')) * galleryScrollSpeed * -1,'linear', function(){ animateBackGallery(); })
  }
}
function changeGalleryDirection(newDirection, newSpeed)
{
  galleryScrollSpeed = newSpeed;
  galleryDirection = newDirection;
  galleryAnimatingItem.stop();
  if (galleryDirection)
    animateForwardGallery();
  else
    animateBackGallery();
}

function decreaseItemQty(a)
{
  input = $(a).next();
  input.val((parseInt(input.val()) > 0) ? parseInt(input.val()) - 1 : 0);
  return false;
}

function increaseItemQty(a)
{
  input = $(a).prev();
  input.val(parseInt(input.val()) + 1);
  return false;
}
