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