/* Set values for gallery auto-rotation */
var iCurrent=1; // Sets the first gallery image
var timer;  // Variable for timer
var iSeconds = 5; // Number of seconds between rotations
var iMax; // Creates variable for the number of gallery images

/* Set inital promo and nav icon on page load */
var currentPromo = "div#homeImage"+iCurrent;
var currentNav = "a#rotatorNav"+iCurrent;

$(document).ready(function(){
	var promos = document.getElementById("homePromoRotator");
	var aPromos = promos.getElementsByTagName("img");
	iMax = aPromos.length;

	/* Set initial gallery nav state */
	$(currentNav).addClass("selected");
	/* Start timer */
	timer = setInterval("ChangePromo($(currentNav), $('a#rotatorNav'+getNextID()))", (iSeconds*1000));
	
	/* add click event function to switch promos */
	$("div#rotatorNav a.rotatorNav").click(function () {
		ChangePromo($(currentNav), $(this));
		return false;
	});
	
	/* Previous Button */
	$("div#rotatorNav a#rotatorPrev").click(function () {
		ChangePromo($(currentNav), $('a#rotatorNav'+getPrevID()));
		return false;
	}); 
	
	/* Next Button */
	$("div#rotatorNav a#rotatorNext").click(function () {
		ChangePromo($(currentNav), $('a#rotatorNav'+getNextID()));
		return false;
	});

});
		
/* Function to switch promos and restart timer */
function ChangePromo(oCurrent, oNew) {
	// If the user clicks on a different item than the current item
	if ($(oCurrent).attr('href') != $(oNew).attr('href')) {
		// Update classes on  nav items
		$(oCurrent).removeClass("selected");
		$(oNew).addClass("selected");
		
		// Update current nav item
		currentNav = $(oNew);
		
		// Fade photos to switch
		$(currentPromo).fadeOut("slow");
		$($(oNew).attr('href')).fadeIn("slow");
		
		// Update current item
		currentPromo = $(oNew).attr('href');
		iCurrent = Number(currentPromo.substring(10));

		clearInterval(timer);
		timer = setInterval("ChangePromo($(currentNav), $('a#rotatorNav'+getNextID()))", (iSeconds*1000));
	};
};

/* Gets ID of next promo */
function getNextID() {
	iCurrent==iMax ? iCurrent=1 : iCurrent++;
	return iCurrent;
}
/* Gets ID of previous promo */
function getPrevID() {
	iCurrent==1 ? iCurrent=iMax : iCurrent--;
	return iCurrent;
}
