/* Set some variables for homepage image timeout */
var homeImageTimeout = 3000;
var homeImageCycleDuration = 1000;
var totalHomeImages = 4;
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','huge'));    
  });
  
  if ($('#main-image').length > 0)
    cycleHomeImages();
    
  $('#search-top-text').focus(function()
  {
    $(this).val('');
  });
  
  if ($('a[rel=lightbox]').length > 0)
    $('a[rel=lightbox]').lightBox();
    
});

/* The function that does the work for homepage image cycling */
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 */
    $('#main-image').find('img.'+currentHomeImage).fadeOut(homeImageCycleDuration);
    /* Fade in the new one */
    $('#main-image').find('img.'+nextHomeImage).fadeIn(homeImageCycleDuration, function() {
      currentHomeImage = nextHomeImage;
      cycleHomeImages(); /* call the function again */
    });
  }, homeImageTimeout);
}

