//determines if nav should be hidden
var showNav = true;
var timerDuration = "7000";
$(document).ready(function(){
	
/* ****************
 * Timer Setup
 ****************** */

	//create and instance of the timer set at 5 seconds
	var timer = $.timer(timerDuration, function(timer){
		
		//get the current promo item
		var curPromo = $("a.selPromo")[0].rel;
		
		//set the next promo item to display
		var nextPromo = eval(curPromo) + 1;
		curPromo == pItems ? nextPromo = 1 : "";
		
		//do the slide
		mPromoSlider(nextPromo);
	})

/* ****************
 * Navigation Setup
 ****************** */

	showNav ? $("div#gatewayPromoNav", this).fadeIn(500) : "";

	//setup the mouseover events
	$("div#gatewayPromo").hover(
		function(){
			showNav == false ? $("div#gatewayPromoNav", this).fadeIn(500) : "";
			timer.stop();
		},
		function(){
			showNav == false ? $("div#gatewayPromoNav", this).fadeOut(400) : "";
			timer.reset(timerDuration);
		}
	);
	
	//Determine total promo items
	var pItems = $("#promoSlider > div.promoItem").length;
	
	//add the navigation links
	for(i = 1; i <= pItems; i++){
		var sClass = "";
		i == 1 ? sClass = "selPromo" : "" ;
		$("div#gatewayPromoNav").append("<a href='#' rel='"+i+"' title='Show "+i+"' class='"+sClass+"'>"+i+"</a>");
	}
	
	//Add the Click Functionality & location vars
	$("div#gatewayPromoNav > a").click(function(){
		
		//do the slide
		mPromoSlider(this.rel);

	});	
});

//Moves the slider
function mPromoSlider(pItem){
	var pLoc = ((pItem - 1)*940);
	pLoc > 0 ? pLoc = "-" + pLoc : "";
	pLoc = pLoc + "px";
	
	//remove the selected class from the nav items
	$("a.selPromo").removeClass('selPromo');
	
	//add the selected class to the current nav item
	$("a[rel='"+pItem+"']").addClass('selPromo');
	
	//show the nav
	if (showNav) {
		$("#promoSlider").animate({
			marginLeft: pLoc
		}, 500);
	}
	else{
		$("div#gatewayPromoNav").fadeIn(500, function(){
			$("#promoSlider").animate({
				marginLeft: pLoc
			}, 500, function(){
				//hide the nav
				$("div#gatewayPromoNav").fadeOut(500);
			});
		})
	}
}
