/**********************************
	THE COOKS GARDEN HOME PAGE SCRIPTS
	Author: James Van Arsdale III
	Created: 10/13/08
**********************************/

jQuery(document).ready(function() {
	// global vars for rotator script
	var speed = 5000;
	var startpos = 0;
	var timer = 0;
	
	// init the buttons for the main callout switcher
	jQuery(".home #contentBody-container ul.co-nav li a").mouseover(function() {
		var matchIndex = jQuery(".home #contentBody-container ul.co-nav li a").index(this);
		
		// kill timout
		clearTimeout(timer);
		
		// hide all, then show matching callout img
		jQuery(".home #contentBody-container .main-images a").hide();
		jQuery(".home #contentBody-container .main-images a:eq("+matchIndex+")").show();
		
		// set active anchor
		jQuery(".home #contentBody-container ul.co-nav li").removeClass("active");
		jQuery(this).parents("li").addClass("active");
	});
	jQuery(".home #contentBody-container ul.co-nav li a").mouseout(function() {
		var matchIndex = jQuery(".home #contentBody-container ul.co-nav li a").index(this);
		//rotateCallouts(matchIndex);
		timer = setTimeout("rotateKeepAlive("+matchIndex+")",speed);
	});
	jQuery(".home #contentBody-container ul.co-nav li a").click(function() {
		return false;
	});
	
	// rotater function
	rotateCallouts = function(current)
	{
		var imgLen = jQuery(".home #contentBody-container .main-images img").length;
		var numLen = jQuery(".home #contentBody-container ul.co-nav li a").length;
		var newPos = current + 1;
		
		// hide all
		jQuery(".home #contentBody-container .main-images a").hide();
		jQuery(".home #contentBody-container ul.co-nav li").removeClass("active");

		// rotate		
		if( newPos == imgLen || newPos == numLen )
		{
			//alert("restarted. current: "+current+" | newPos: "+newPos+" | img-num: "+imgLen+numLen)
			newPos = startpos;
			jQuery(".home #contentBody-container .main-images a:eq(0)").show();
			jQuery(".home #contentBody-container ul.co-nav li:eq(0)").addClass("active");
		}
		else
		{
			//alert("continuing: "+newPos);
			jQuery(".home #contentBody-container .main-images a:eq("+newPos+")").show();
			jQuery(".home #contentBody-container ul.co-nav li:eq("+newPos+")").addClass("active");
		}

		timer = setTimeout("rotateKeepAlive("+newPos+")",speed);
	}
	
	rotateKeepAlive = function(lastPos)
	{
		// kill timout
		clearTimeout(timer);
		
		// restart
		rotateCallouts(lastPos);
	}
	
	// init the rotator
	rotateCallouts(startpos - 1);
});