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

$(document).ready(function() {

  $('.small-product-image').click(function() {
    $('#large-product-image').children('A').children('IMG').attr('src', $(this).attr('src').replace('thumb','large'));
    $(this).parents('li').children('a').attr('href',$('#product-image-large-link').attr('href'));
    $('#product-image-large-link').attr('href', $(this).attr('src').replace('thumb','large'));    
  });

  /* Hovering on menu items */
  $('#main-menu').find('LI').hover(function() {
    $(this).addClass('hover');
  }, function() {
    $(this).removeClass('hover');
  });
  
   /* Hovering on menu items */
  $('#navigation').find('a').hover(function() {
    $(this).addClass('ahover');
  }, function() {
    $(this).removeClass('ahover');
  });
  
  if ($('#home-image-main').length > 0)
    cycleHomeImages();
    
  if ($('a[rel=lightbox]').length > 0)
    $('a[rel=lightbox]').lightBox();
});

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);
}


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

function increaseQty(a)
{
  input = ($(a).parents('p').length > 0) ? $(a).parents('p').find('input[type=text]') : $(a).parents('td').find('input[type=text]');  
  input.val(parseInt(input.val()) + 1);
  return false;
}
