/**********************************
	MIGHTY LEAF 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;
	
	// init the buttons for the main callout switcher
	$(".home .content ul.co-nav li.imageswitcher a").mouseover(function() {
		var matchIndex = $(".home .content ul.co-nav li.imageswitcher a").index(this);
		
		// kill timout
		clearTimeout(timer);
		
		// hide all, then show matching callout img
		$(".home .content .main-images a").hide();
		$(".home .content .main-images a:eq("+matchIndex+")").show();
		
		// set active anchor
		$(".home .content ul.co-nav li.imageswitcher").removeClass("active");
		$(this).parents("li.imageswitcher").addClass("active");
	});
	
	$(".home .content ul.co-nav li.imageswitcher a").mouseout(function() {
		var matchIndex = $(".home .content ul.co-nav li.imageswitcher a").index(this);
		//rotateCallouts(matchIndex);
		timer = setTimeout("rotateKeepAlive("+matchIndex+")",speed);
	});
	$(".home .content ul.co-nav li.imageswitcher a").click(function() {
		return false;
	});
	
	$(".home .content ul.co-nav li.next a").click(function() {
		var matchItem = $(".home .content ul.co-nav li.active a");
		var numLen = $(".home .content ul.co-nav li.imageswitcher a").length;
		var matchIndex = $(".home .content ul.co-nav li.imageswitcher a").index(matchItem);
		
		if (matchIndex == numLen-1){
			matchIndex = 0;			
		}
		else {
			matchIndex = matchIndex+1;
		}
		clearTimeout(timer);
		$(".home .content .main-images a").hide();
		$(".home .content .main-images a:eq("+matchIndex+")").show();
		
		$(".home .content ul.co-nav li.imageswitcher").removeClass("active");
		$(".home .content ul.co-nav li.imageswitcher a:eq("+matchIndex+")").parents("li.imageswitcher").addClass("active");
		return false;
	});
	
	$(".home .content ul.co-nav li.previous a").click(function() {
		var matchItem = $(".home .content ul.co-nav li.active a");
		var numLen = $(".home .content ul.co-nav li.imageswitcher a").length;
		var matchIndex = $(".home .content ul.co-nav li.imageswitcher a").index(matchItem);
		
		if (matchIndex == 0){
			matchIndex = numLen-1;			
		}
		else {
			matchIndex = matchIndex-1;
		}
		clearTimeout(timer);
		$(".home .content .main-images a").hide();
		$(".home .content .main-images a:eq("+matchIndex+")").show();
		
		$(".home .content ul.co-nav li.imageswitcher").removeClass("active");
		$(".home .content ul.co-nav li.imageswitcher a:eq("+matchIndex+")").parents("li.imageswitcher").addClass("active");
		return false;
	});
	
	// rotater function
	rotateCallouts = function(current)
	{
		var imgLen = $(".home .content .main-images img").length;
		var numLen = $(".home .content ul.co-nav li.imageswitcher a").length;
		var newPos = current + 1;
		
		// hide all
		$(".home .content .main-images a").hide();
		$(".home .content ul.co-nav li.imageswitcher").removeClass("active");

		// rotate		
		if( newPos == imgLen || newPos == numLen )
		{
			//alert("restarted. current: "+current+" | newPos: "+newPos+" | img-num: "+imgLen+numLen)
			newPos = startpos;
			$(".home .content .main-images a:eq(0)").show();
			$(".home .content ul.co-nav li.imageswitcher:eq(0)").addClass("active");
		}
		else
		{
			//alert("continuing: "+newPos);
			$(".home .content .main-images a:eq("+newPos+")").show();
			$(".home .content ul.co-nav li.imageswitcher: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);
	
	$("div.homepage-testimonials ul li").hide();
	$("div.homepage-testimonials ul li.active").show();
	
	$("div.homepage-testimonials .previous a").click(function() {
		var numLen = $("div.homepage-testimonials ul li").length;
		var matchItem = $("div.homepage-testimonials ul li.active");
		var matchIndex = $("div.homepage-testimonials ul li").index(matchItem);
		
		if (matchIndex == 0){
			matchIndex = numLen-1;			
		}
		else {
			matchIndex = matchIndex-1;
		}
		
		$("div.homepage-testimonials ul li").hide();
		$("div.homepage-testimonials ul li").removeClass("active");
		$("div.homepage-testimonials ul li:eq("+matchIndex+")").show().addClass("active");
		
		
		return false;
	});
	$("div.homepage-testimonials .next a").click(function() {
		var numLen = $("div.homepage-testimonials ul li").length;
		var matchItem = $("div.homepage-testimonials ul li.active");
		var matchIndex = $("div.homepage-testimonials ul li").index(matchItem);
		
		if (matchIndex == numLen-1){
			matchIndex = 0;			
		}
		else {
			matchIndex = matchIndex+1;
		}
		
		$("div.homepage-testimonials ul li").hide();
		$("div.homepage-testimonials ul li").removeClass("active");
		$("div.homepage-testimonials ul li:eq("+matchIndex+")").show().addClass("active");
		
		
		return false;
	});
});
