

$(document).ready(function() {
	// global vars for rotator script
	var speed = 5000;
	var startpos = 0;
	var timer = 0;
	var index = 0;
	var pause = 0;
	
	// init the buttons for the main callout switcher
	$(".main #contentbody-container ul.co-nav li a").mouseover(function() {
		var matchIndex = $(".main #contentbody-container ul.co-nav li a").index(this);
		index = matchIndex;
		// kill timout
		clearTimeout(timer);
		
		// hide all, then show matching callout img
		$(".main #contentbody-container .main-images a").hide();
		$(".main #contentbody-container .main-images a:eq("+matchIndex+")").show();
		
		// set active anchor
		$(".main #contentbody-container ul.co-nav li").removeClass("active");
		$(this).parents("li").addClass("active");
	});
	$(".main #contentbody-container ul.co-nav li a").mouseout(function() {
	
		var matchIndex = $(".main #contentbody-container ul.co-nav li a").index(this);
		index = matchIndex;
		//rotateCallouts(matchIndex);
		if(pause == 0)
		{	
		    
		    timer = setTimeout("rotateKeepAlive("+matchIndex+")",speed);
		}
	});
	
	
	$(".main #pause").click(function() {
	    pause = 1;
      	var matchIndex = index;
      	
      	//$(".main #contentbody-container ul.co-nav li a").index(this);
		// kill timout
		clearTimeout(timer);
		
		// hide all, then show matching callout img
		$(".main #contentbody-container .main-images a").hide();
		$(".main #contentbody-container .main-images a:eq("+matchIndex+")").show();
		
		var imgLen = $(".main #contentbody-container .main-images img").length;
		var numLen = $(".main #contentbody-container ul.co-nav li a").length;
		
		// set active anchor
		$(".main #contentbody-container ul.co-nav li").removeClass("active");
		$(".main #contentbody-container ul.co-nav li:eq("+matchIndex+")").addClass("active");
			$("#play").removeClass("active");
			$("#pause").addClass("active");
	});
	
	$(".main #play").click(function() {
	    
	    if(pause == 0)
	    {
	            //Condition for Pause
	            pause = 1;
	            var matchIndex = index;
              	
      	        //$(".main #contentbody-container ul.co-nav li a").index(this);
		        // kill timout
		        clearTimeout(timer);
        		
		        // hide all, then show matching callout img
		        $(".main #contentbody-container .main-images a").hide();
		        $(".main #contentbody-container .main-images a:eq("+matchIndex+")").show();
        		
		        var imgLen = $(".main #contentbody-container .main-images img").length;
		        var numLen = $(".main #contentbody-container ul.co-nav li a").length;
        		
		        // set active anchor
		        $(".main #contentbody-container ul.co-nav li").removeClass("active");
		        $(".main #contentbody-container ul.co-nav li:eq("+matchIndex+")").addClass("active");
        		$("#play").removeClass("pause");
        		$("#play").addClass("play");
	   
        }
        else
        {
                //Condition for Play
                   pause = 0;
                   $("#play").removeClass("play");
        		   $("#play").addClass("pause");
        //         $("#play").addClass("active");
        //			$("#pause").removeClass("active");
                   rotateCallouts(index);
                
        }
	});
	
	
	$(".main #contentbody-container ul.co-nav li a").click(function() {
		return false;
	});
	
	// rotater function
	rotateCallouts = function(current)
	{
		var imgLen = $(".main #contentbody-container .main-images img").length;
		var numLen = $(".main #contentbody-container ul.co-nav li a").length;
		var newPos = current + 1;
		index = newPos;
		// hide all
		$(".main #contentbody-container .main-images a").hide();
		$(".main #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;
			index = startpos;
			$(".main #contentbody-container .main-images a:eq(0)").show();
			$(".main #contentbody-container ul.co-nav li:eq(0)").addClass("active");
		}
		else
		{
			//alert("continuing: "+newPos);
			$(".main #contentbody-container .main-images a:eq("+newPos+")").show();
			$(".main #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
	$(".main #contentlowerbody-container a.up").click(function() {
		var cntrTop = $(".main #contentlowerbody-container").offset().top;
		//var menusHeight = $(".main #contentlowerbody-container .menus").height();
		var menusTop = $(".main #contentlowerbody-container .menus").offset().top;
		var menuHeight = $(".main #contentlowerbody-container .menus .menu:eq(0)").height();
		//var menuCount = $(".main #contentlowerbody-container .menus .menu").length;
		
		//alert(menuHeight+" | "+menusTop+" | "+cntrTop);
		
		if( $(".main #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);
			$(".main #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);	
			}
			
			$(".main #contentlowerbody-container .menus").animate({top: "+="+menuHeight+"px"}, "slow");
		}
		else
		{
			$(this).animate({ opacity: 0.1 }, 500);			
		}

		return false;
	});
	
	// scroll down
	$(".main #contentlowerbody-container a.down").click(function() {
		var cntrTop = $(".main #contentlowerbody-container").offset().top;
		var menusHeight = $(".main #contentlowerbody-container .menus").height();
		var menusTop = $(".main #contentlowerbody-container .menus").offset().top;
		var menuHeight = $(".main #contentlowerbody-container .menus .menu:eq(0)").height();
		var menuCount = $(".main #contentlowerbody-container .menus .menu").length;
		
		//alert(menusHeight+" | "+menuHeight+" | "+menuCount+" | "+menusTop+" | "+cntrTop+" | "+(-(menuHeight * (menuCount - 1) - (cntrTop + 3))));
		//alert((-(menuHeight * (menuCount - 2) - cntrTop))+" | "+menuCount+" | "+menusTop);
		
		if( $(".main #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);
			$(".main #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);
			}
			
			$(".main #contentlowerbody-container .menus").animate({top: "-="+menuHeight+"px"}, "slow");
		}
		else
		{
			$(this).animate({ opacity: 0.1 }, 500);			
		}

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