/**********************************
	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 #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").removeClass("active");
		$(this).parents("li").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").removeClass("active");

		// 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)").addClass("active");
		}
		else
		{
			//alert("continuing: "+newPos);
			$(".home #contentbody-container .main-images a:eq("+newPos+")").show();
			$(".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);
	
	
	
	/* slider menu */
	// scroll up
	/*$(".home #contentlowerbody-container a.up").click(function() {
		var cntrTop = $(".home #contentlowerbody-container").offset().top;
		//var menusHeight = $(".home #contentlowerbody-container .menus").height();
		var menusTop = $(".home #contentlowerbody-container .menus").offset().top;
		var menuHeight = $(".home #contentlowerbody-container .menus .menu:eq(0)").height();
		//var menuCount = $(".home #contentlowerbody-container .menus .menu").length;
		
		//alert(menuHeight+" | "+menusTop+" | "+cntrTop);
		
		if( $(".home #contentlowerbody-container .menus").is(":animated") )
		{
			return false;
		}
		
		// if menus container is higher than parent container, proceed to move menus down
		if( menusTop < cntrTop )
		{
			// turn arrows on
			$(this).animate({ opacity: 1 }, 500);
			$(".home #contentlowerbody-container a.down").animate({ opacity: 1 }, 500);
			
			// if next to last item, set opacity
			if( (cntrTop - menuHeight) == menusTop )
			{
				$(this).animate({ opacity: 0.1 }, 500);	
			}
			
			$(".home #contentlowerbody-container .menus").animate({top: "+="+menuHeight+"px"}, "slow");
		}
		else
		{
			$(this).animate({ opacity: 0.1 }, 500);			
		}

		return false;
	});
	
	// scroll down
	$(".home #contentlowerbody-container a.down").click(function() {
		var cntrTop = $(".home #contentlowerbody-container").offset().top;
		var menusHeight = $(".home #contentlowerbody-container .menus").height();
		var menusTop = $(".home #contentlowerbody-container .menus").offset().top;
		var menuHeight = $(".home #contentlowerbody-container .menus .menu:eq(0)").height();
		var menuCount = $(".home #contentlowerbody-container .menus .menu").length;
		
		//alert(menusHeight+" | "+menuHeight+" | "+menuCount+" | "+menusTop+" | "+cntrTop+" | "+(-(menuHeight * (menuCount - 1) - (cntrTop + 3))));
		//alert((-(menuHeight * (menuCount - 2) - cntrTop))+" | "+menuCount+" | "+menusTop);
		
		if( $(".home #contentlowerbody-container .menus").is(":animated") )
		{
			return false;
		}
		
		// if top of the last menu is at the top of the ctnr, don't move, else move them
		if( (-(menuHeight * (menuCount - 1) - (cntrTop + 3))) < menusTop )
		{
			// turn arrows on
			$(this).animate({ opacity: 1 }, 500);
			$(".home #contentlowerbody-container a.up").animate({ opacity: 1 }, 500);
			
			// if next to last item, set opacity
			if( (-(menuHeight * (menuCount - 2) - cntrTop)) == menusTop )
			{
				$(this).animate({ opacity: 0.1 }, 500);
			}
			
			$(".home #contentlowerbody-container .menus").animate({top: "-="+menuHeight+"px"}, "slow");
		}
		else
		{
			$(this).animate({ opacity: 0.1 }, 500);			
		}

		return false;
	});
	
	// turn arrows on/off on page load
	$(".home #contentlowerbody-container a.up").animate({ opacity: 0.1 }, 500);
	if( $(".home #contentlowerbody-container .menus .menu").length < 2 )
	{
		// remove the arrows
		$(".home #contentlowerbody-container a.up").remove();
		$(".home #contentlowerbody-container a.down").remove();
	}*/
});
