/* Set some variables for homepage image timeout */
var homeImageTimeout = 3000;
var homeImageCycleDuration = 1000;
var totalHomeImages = 3;
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('small','large'));
    $(this).parents('li').children('a').attr('href',$('#product-image-large-link').attr('href'));
    $('#product-image-large-link').attr('href', $(this).attr('src').replace('small','huge'));    
  });
  
  $('.embedded-select').change(function() {
    if ($(this).val() == 'Other')
    {
      $(this).parent().append('<div class="other"><input class="other-input" type="text" value="" name="'+$(this).attr('name')+'"/></div>');
      $(this).attr('name','')
    }  
    else if ($(this).parent().children('.other'))
    {      
      $(this).attr('name',$(this).parent().children('.other').children('.other-input').attr('name'));
      $(this).parent().children('.other').remove();
    }
  });

  
  if ($('#main-image').length > 0)
    cycleHomeImages();
    
  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);
}


function decreaseQty(a)
{
  input = ($(a).parents('p').length > 0) ? $(a).parents('p').find('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('p').length > 0) ? $(a).parents('p').find('input') : $(a).parents('td').find('input');  
  input.val(parseInt(input.val()) + 1);
  return false;
}
