function trackActions()
{
	if( gActions.length > 0 )
	{	
		// actions in queue
		if( gActions[0] == "+" )
		{
			gNext = gCurrent + 1;
		}
		else if( gActions[0] == "-" )
		{
			gNext = gCurrent - 1;
		}
		else
		{
			gNext = gActions[0];
		}
		
		if( gNext < 0 )
		{
			gNext = gMax;
		}
		else if( gNext > gMax )
		{
			gNext = 0;
		}
		
		setNav(gNext);
		
		
		if( gNext > gCurrent )
		{
			fadeInUpper();
		}
		else if( gNext < gCurrent)
		{
			fadeInLower();
		}
		else
		{
			gActions.shift();
		}
		
	}
	else
	{
		// no actions in queue
	}
}

function fadeInUpper()
{
	$("#"+gDiv).children().eq(gNext).children('.hero_fader_txt').hide();
	$("#"+gDiv).children().eq(gNext).fadeIn(gFadeTime, function(){
		$("#"+gDiv).children().eq(gNext).children('.hero_fader_txt').fadeIn(gFadeTime);
			if( gCurrent > 0 )
			{
				$("#"+gDiv).children().eq(gCurrent).hide();
			}
			gCurrent = gNext;
			gActions.shift();
			trackActions();
		});
}

function fadeInLower()
{
	$("#"+gDiv).children().eq(gNext).show();
	$("#"+gDiv).children().eq(gCurrent).children('.hero_fader_txt').fadeOut(gFadeTime, function(){
		$("#"+gDiv).children().eq(gCurrent).fadeOut(gFadeTime, function(){
			gCurrent = gNext;
			gActions.shift();
			trackActions();
		});
	});
	// $("#"+gDiv).children().eq(gNext).show();
	// 	$("#"+gDiv).children().eq(gCurrent).fadeOut(gFadeTime, function(){
	// 			gCurrent = gNext;
	// 			gActions.shift();
	// 			trackActions();
	// 		});
}

function builtNav()
{
	for( i=0; i<=gMax; i++ )
	{
		$("#hero_fader_nav").append("<div></div>");
	}
	$("#hero_fader_nav")
		.css('width', ((gMax+1)*16)+'px')
		.children()
		.first()
		.addClass('current_slide');
}

function setNav(no)
{
	$("#hero_fader_nav")
		.children()
		.removeClass("current_slide")
		.eq(no)
		.addClass("current_slide");
}

function autoCallCycle()
{
	if( autoCycle == 1 && gActions.length == 0 )
	{
		gActions.push("+");
		trackActions();
		
		setTimeout("autoCallCycle()", 9000);
	}
	else
	{
	}
}

$(document).ready(function(){
	
	if ($.browser.msie && $.browser.version.substr(0,1)<7)
	{
		$('div.hfii_img').pngFix();
	}
	
	
	// CYCLE
	autoCycle = 1;
	setTimeout("autoCallCycle()", 9000);
	gDiv = "hero_fader";
	gCurrent = 0;
	gNext = 0;
	gFadeTime = 1100;
	gMax = $("#"+gDiv).children().length - 1;
	gActions = new Array();
	$("#"+gDiv).children().hide().eq(0).show(); 
	builtNav();
	
	
	$("#hero_fader_next, #hero_fader_prev").click(function(){
		autoCycle = 0;
		var gAction = "-";
		if( $(this).attr('id') == "hero_fader_next")
		{
			gAction = "+";
		}
		
		if( gActions.length == 0 )
		{
			gActions.push(gAction);
			trackActions();
		}
		else if( gActions.length < 2 )
		{
			gActions.push(gAction);
		}
	
	});
	
	$(".hero_fader_item img").click(function(){
		autoCycle = 0;
	});
	
	// $("#hero_wrap").mouseleave(function(){
	// 	$("#hero_fader_arrows").fadeOut(250);
	// });
	
	$("#hero_fader_nav_wrap")
	.hoverIntent(
		function(){
			$("#hero_fader_next").show();
			$("#hero_fader_prev").show();
			$('#hero_fader_nav_bg').animate({ bottom: '0px'},350,'easeOutCirc');
		},
		function(){
			$("#hero_fader_next").hide();
			$("#hero_fader_prev").hide();
			$('#hero_fader_nav_bg').animate({ bottom: '-44px'},350,'easeInCirc');
		}
	)
	
	$("#hero_fader_nav div").click(function(){
		autoCycle = 0;
		var gAction = $(this).index();
		
		if( gActions.length == 0 )
		{
			gActions.push(gAction);
			trackActions();
		}
		else if( gActions.length < 2 )
		{
			gActions.push(gAction);
		}
	
	});
	
	
});
