/**********************************
	Main Callout Rotator Script
	Author: James Van Arsdale III
	Created: 5/21/09
**********************************/

$(document).ready(function() {
	// global vars for rotator script
	var speed = 5000,
		pauseTime = 1000000,
		startpos = 0,
		timer = 0;
	
	//Image Rotator on Homepage
	var hero	= $('#content-container div.rotating-hero'), 
		imgWrap = $("div.images", hero),
		imgLen = $("img", imgWrap).length,
		counter = $('<ul class="co-nav"></ul>');




		//Check for Multiple Callouts
		if (imgLen > 1) {
			//Insert UL.co-nav Before Callout Div
			hero.prepend(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="#"><span>Play/Pause</span></a></li>');
		};
		
		//Label First and Last Classes of UL.co-nav
		$("ul.co-nav li:first",hero).addClass("first");
		$("ul.co-nav li:last",hero).addClass("last");
	
	// init the buttons for the main callout switcher
	$("ul.co-nav a.image-index",hero).hover(
		function() {
			var matchIndex = $("ul.co-nav li a.image-index",hero).index(this);
			
			// kill timout
			clearTimeout(timer);
			
			// hide all, then show matching callout img
			$("a",imgWrap).hide();
			$("a:eq("+matchIndex+")",imgWrap).show();
			
			// set active anchor
			$("ul.co-nav a",hero).removeClass("active");
			$(this).addClass("active");
			$("ul.co-nav a.rotator-control",hero).removeClass("rotator-play");
		},
		function() {
			var matchIndex = $("ul.co-nav li a",hero).index(this);
			//rotateCallouts(matchIndex);
			timer = setTimeout("rotateKeepAlive("+matchIndex+")",speed);
		}
	);

	// play/pause on click
	$("ul.co-nav a.rotator-control",hero).click(function() {
		var buttonCurrent = $(this).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);
			$(this).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 a",hero).click(function() {
		return false;
	});
	
	// rotater function
	rotateCallouts = function(current)
	{
		var numLen = $("ul.co-nav li a",hero).length,
			newPos = current + 1;
		
		// hide all
		$("a",imgWrap).hide();
		$("ul.co-nav a.active",hero).removeClass("active");
		//alert(numLen);

		// rotate		
		if( newPos == imgLen || newPos == numLen )
		{
			//alert("restarted. current: "+current+" | newPos: "+newPos+" | img-num: "+imgLen+numLen)
			newPos = startpos;
			$("a:eq(0)",imgWrap).show();
			$("ul.co-nav li:eq(0) a",hero).addClass("active");
		}
		else
		{
			//alert("continuing: "+newPos);
			$("a:eq("+newPos+")",imgWrap).show();
			$("ul.co-nav li:eq("+newPos+") a",hero).addClass("active");
		}

		timer = setTimeout("rotateKeepAlive("+newPos+")",speed);
	}
	
	rotateKeepAlive = function(lastPos)
	{
		// kill timout
		clearTimeout(timer);
		
		// restart
		rotateCallouts(lastPos);
	}
	
	// init the rotator
	rotateCallouts(startpos - 1);
	
});