/**********************************
	SCHNEIDERS HOME PAGE SCRIPTS
	Author: James Van Arsdale III
	Created: 5/21/09
**********************************/

$(document).ready(function() {
	// global vars for rotator script
	var speed = 5000;
	var pauseTime = 1000000;
	var startpos = 0;
	var timer = 0;
	
	
	//Image Rotator on Homepage
	var imgWrap = $(".main-images");
	var imgLen = $(".main-images img").length;
	var counter = $('<ul class="co-nav"></ul>');
		//Check for Multiple Callouts
		if (imgLen > 1) {
			//Insert UL.co-nav Before Callout Div
			$(imgWrap).before(counter);
			//Populate UL.co-nav
			for (i=1;i<=imgLen;i++) {
				$(counter).append('<li><a class="image-index" href="#">' + i + '</a></li>');
			};
			$(counter).append('<li><a class="rotator-control" href="#" title="Pause the current image."></a></li>');
		};
		//Label First and Last Classes of UL.co-nav
		$("ul.co-nav li:first").addClass("first");
		$("ul.co-nav li:last").addClass("last");
	
	// init the buttons for the main callout switcher
	$("ul.co-nav li a.image-index").mouseover(function() {
		var matchIndex = $("ul.co-nav li a.image-index").index(this);
		
		// kill timout
		clearTimeout(timer);
		
		// hide all, then show matching callout img
		$(".main-images a").hide();
		$(".main-images a:eq("+matchIndex+")").show();
		
		// set active anchor
		$("ul.co-nav li a").removeClass("active");
		$(this).addClass("active");
		$(".rotator-control").removeClass("rotator-play");
	});
	$("ul.co-nav li a.image-index").mouseout(function() {
		var matchIndex = $("ul.co-nav li a").index(this);
		//rotateCallouts(matchIndex);
		timer = setTimeout("rotateKeepAlive("+matchIndex+")",speed);
	});

	// play/pause on click
	$(".rotator-control").click(function() {
		var buttonCurrent = $(".rotator-control").attr("class");
		if ( buttonCurrent == 'rotator-control' )
			{
				clearTimeout(timer);
				var matchIndex = $("ul.co-nav li").index($("ul.co-nav > li:has(a.active)"));
				timer = setTimeout("rotateKeepAlive("+matchIndex+")",pauseTime);
				$(".rotator-control").addClass("rotator-play");
			}
		else
			{
				clearTimeout(timer);
				var matchIndex = $("ul.co-nav li").index($("ul.co-nav > li:has(a.active)"));
				timer = setTimeout("rotateKeepAlive("+matchIndex+")",speed);
				$(".rotator-control").removeClass("rotator-play");
			};
		return false;
	});

	$("ul.co-nav li a").click(function() {
		return false;
	});
	
	// rotater function
	rotateCallouts = function(current)
	{
		var imgLen = $(".main-images img").length;
		var numLen = $("ul.co-nav li a").length;
		var newPos = current + 1;
		
		// hide all
		$(".main-images a").hide();
		$("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;
			$(".main-images a:eq(0)").show();
			$("ul.co-nav li:eq(0) a").addClass("active");
		}
		else
		{
			//alert("continuing: "+newPos);
			$(".main-images a:eq("+newPos+")").show();
			$("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);
	
});