$(document).ready(function() {
  
  if ($('#home-slideshow').length > 0)
  {
    currHomeImg = parseInt($('#home-slideshow img').attr('src').slice(-5,-4));
    setInterval('progressSlideshow()', 10000);
  }
  
   if ($('#sales-slideshow').length > 0)
  {
    $('#sales-slideshow').cycle({
       height: 190,
       pause: 1
       })
  }
  
  // open links with class 'popup' in new windows
  $('a.popup').click(function() {
    window.open($(this).attr('href'));
    return false;
  });
  
  
  // some jqueryified son of suckerfish stuff
  $('#navigation').find('LI').each(function() {
    $(this).hover(function() {
      $(this).addClass('sfhover');
    }, function() {
      $(this).removeClass('sfhover');
    });
  });  
  
  // Slide down a showable div when it's show link is clicked
  $('.divshow').click(function() {
    $(this).parent().next().show();
    $(this).hide()
    return false;  
  });

  
  $('.divhide').click(function() {
    $(this).parents('.showable').hide().prev().children('a.divshow').show();
    return false;
  });
  
  fillProducts();
    
});

    
function progressSlideshow()
{

	var linkArr = ['/products/','/products/fieldtemplater/','/products/fieldstere/','/products/fieldalign/'];
  	currHomeImg++;
  	if (currHomeImg == 5) currHomeImg = 1;
 	src = 'http://www.cresset-group.com/wp-content/themes/Cresset/images/homefade/' + currHomeImg + '.jpg';
 	slideshowImage = new Image();
 	slideshowImage.src = src;
 	slideshowImage.onload = function()
  		{
		$('#home-slideshow').append('<img src="' + slideshowImage.src + '" style="display: none;" />');
		$('#home-slideshow img:last-child').fadeIn(2000, function(){ $('#home-slideshow img:first-child').remove(); });
		$('#home-slideshow').attr('href',linkArr[currHomeImg]);
  		}
}

function progressSalesSlideshow()
{

	var linkArr = ['mailto:sales@cresset-group.com?subject=Sales enquiry for Viv','mailto:sales@cresset-group.com','mailto:sales@cresset-group.com?subject=Sales enquiry for Viv','mailto:sales@cresset-group.com'];
  	currSalesImg++;
  	if (currSalesImg == 5) currSalesImg = 1;
 	src = 'http://www.cresset-group.com/images/roll/' + currSalesImg + '.png';
 	slideshowImage = new Image();
 	slideshowImage.src = src;
 	slideshowImage.onload = function()
  		{
		$('#sales-slideshow').append('<img src="' + slideshowImage.src + '" style="display: none;" />');
		$('#sales-slideshow img:last-child').fadeIn(100, function(){ $('#sales-slideshow img:first-child').remove(); });
		$('#sales-slideshow').attr('href',linkArr[currSalesImg]);
  		}
}

function fillProducts() {

	//Move all of the non-sticky products to the bottom of the dom tree
	$('#home-boxes').children('li.sticky-false').each(function() {
		$('#home-boxes').append($(this));
	});
	
	
	var stickyLength = $('#home-boxes').children('li.sticky-true').length;
	
	if (stickyLength < 4) { //We only rotate images if there are less than 4 sticky images/links, otherwise the featured section is full.
		
		var remainder = 4 - stickyLength;  //Figure out how many images we're going to rotate
		var available = $('#home-boxes').children('li.sticky-false').length;  //Figure out how many non-visible images are available to show

		var counter = 1;

		//Show however many hidden products we need to meet 4
		$('#home-boxes').children('li.sticky-false').each(function() {
			$(this).show().addClass('visible');
			if (counter == remainder) return false; //We've filled the initially empty spaces, end the function.
			counter++;
		});
		
		//Initialize the rotating images
		setInterval(rotateProducts, 12000);
		
	}
}

function rotateProducts() {

	var nonSticky = $('#home-boxes').children('li.sticky-false');
	var visible = $('#home-boxes').children('li.visible');
	
	
	visible.each(function(i) {
	
		var index = nonSticky.index($(this));
		
		if ($(this).hasClass('skip')) { //If the skip class/flag is set on this element, we need to not hide and remove the visible class
			$(this).removeClass('skip');
		} else {
			$(this).hide().removeClass('visible');
		}
		
		if (nonSticky.get(index+(visible.length))) {
			var get = index+(visible.length);
		} else {
			var carry = (nonSticky.length)-index;
			var get = carry - visible.length;
		}
		
		var img = nonSticky.get(Math.abs(get)); //Because we will end up with negative integers when we loop back through the array, we need to make those positive
		
		if ($(img).hasClass('visible')) { //Need to check that the product we're cycling to isn't already visible, if it is, then we'll just leave it, and add a flag on it so it doesn't get hidden further on in the loop
			$(img).addClass('skip');
		} else {
			$(img).addClass('visible').fadeIn();
		}
		
	});	
	
}
