/**
 * @author dave
 */
$(document).ready(function(){
	
	var iState = 1;
	var iStage = 1;
	var iStateSteps = 1;
	var flag = 1;
	var iShunt = 20;
	var iShiftGap = 600;
	var check = 0;

	// inits
	var iReelWidth = 0;
	var iActualShiftGap = 0;
	
	
	//$("#mainholder").fadeOut("slow");
	
	function swapperInit()
	{
		//alert("iStateSteps >>> " + iStateSteps);
		//settup var
		iReelWidth = Math.round(iShiftGap*iStateSteps);
		iActualShiftGap = Math.round(iShiftGap + iShunt);
		
	//	alert("iReelWidth >>> " + iReelWidth);
		
		//init the Reel
		$("#featurenav_reel").css({
			"width" :  (iReelWidth + 12) + "px"
		});
		
		// apply functionality
		if (iStateSteps > 1) {
			bindNavEvents();
		}
	}
	
	function obtainStepCount()
	{
		// get all the featureitems in side the reel
		var stepCount = $("ul > .featurenavitem").size();
		var tempCount = stepCount/3;
		iStateSteps = Math.ceil(tempCount);
	}
	
	function unbindNavEvents()
	{
		$("#featurenav_leftbutton").unbind("click", trackLeft);
		$("#featurenav_rightbutton").unbind("click", trackRight);
	}

	function rotate() 
	{
		/*if($("#featurenav_leftbutton").css("display") == "none"){
			trackRight();
		}
		else if($("#featurenav_rightbutton").css("display") == "none"){
			trackLeft();
		}
		else {
			if(flag == 1){
				trackRight();
				flag += 1;
			}
			else {
				trackLeft();
				flag=1;
			}
		}*/
		if(iStage == 1){
			iStage += 1;
			trackRight1();
		}
		else if(iStage == 2){
			iStage += 1;
			trackRight1();
		}
		else if(iStage == 3){
			iStage += 1;
			trackLeft1();
		}
		else if(iStage == 4){
			iStage=1;
			trackLeft1();
		}
	}
	
	function Func1Delay()
	{
		if (check == 0) {
			setTimeout(rotate, 5000);
		}	
	}
		
	function bindNavEvents()
	{
		// unbind the Navigation events
		unbindNavEvents();
		Func1Delay();
		
		if(iState == 1)
		{
			if($("#featurenav_rightbutton").css("display") == "none")
			{
				$("#featurenav_rightbutton").fadeIn("slow");
			}					
			$("#featurenav_rightbutton").bind("click", trackRight);
			$("#featurenav_leftbutton").fadeOut("slow");
			//$("#featurenav_leftbutton").css({"display" :  "none"});	
		}
		else if(iState == iStateSteps)
		{
			if($("#featurenav_leftbutton").css("display") == "none")
			{
				$("#featurenav_leftbutton").fadeIn("slow");
			}			
			$("#featurenav_rightbutton").fadeOut("slow");
			//$("#featurenav_rightbutton").css({"display" :  "none"});	
			
			
			$("#featurenav_leftbutton").bind("click", trackLeft);
		}
		else
		{
			if($("#featurenav_leftbutton").css("display") == "none")
			{
				$("#featurenav_leftbutton").fadeIn("slow");
			}
			
			if($("#featurenav_rightbutton").css("display") == "none")
			{
				$("#featurenav_rightbutton").fadeIn("slow");
			}			
			
			
			$("#featurenav_leftbutton").bind("click", trackLeft);
			$("#featurenav_rightbutton").bind("click", trackRight);
		}	
	}
	
	function trackLeft()
	{
		if (iState > 1) {
			check=1;
			animateLeft();
			return false;
		}	
	}
	function trackLeft1()
	{
		if (iState > 1 && check == 0) {
			animateLeft();
			return false;
		}	
	}
	function trackRight()
	{
		if (iState < iStateSteps) {
			check=1;
			animateRight();
			return false;			
		}
	}
	function trackRight1()
	{
		if (iState < iStateSteps && check == 0) {
			animateRight();
			return false;			
		}
	}
	
	function animateLeft()
	{
		$("#featurenav_reel").animate({left:"-="+iShunt+"px"},"fast").animate({left:"+="+iActualShiftGap+"px"},"slow","swing",updateState(-1));
	}
	function animateRight()
	{
		$("#featurenav_reel").animate({left:"+="+iShunt+"px"},"fast").animate({left:"-="+iActualShiftGap+"px"},"slow","swing",updateState(+1));	
	}	
	
	function updateState(updateBy)
	{
		iState = iState + updateBy;
		if (iStateSteps > 1) {
			// now reassign events.
			bindNavEvents();
		}
	}
	obtainStepCount();	
	swapperInit();
}); 
