/* Set values for gallery auto-rotation */
var iCurrent=1; // Sets the first gallery image
var timer;  // Variable for timer
var iSeconds = 7; // Number of seconds between rotations
var iMax = 4; // Creates variable for the number of gallery images

/* Set inital promo and nav icon on page load */
var currentGallery = "div#promo"+iCurrent;
var currentNav = "a#promoNav"+iCurrent;

function SetupPromos() {
	/* Set initial gallery nav state */
	$(currentNav).addClass("selected");
	/* Start timer */
	timer = setInterval("ChangePromo($(currentNav), $('a#promoNav'+getNextID()))", (iSeconds*1000));
	
	/* add click event function to switch promos */
	$("div#promoNav a.promoNav").click(function () {
		ChangePromo($(currentNav), $(this));
		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
		$(currentGallery).fadeOut("slow");
		$($(oNew).attr('href')).fadeIn("slow");
		
		// Update current item
		currentGallery = $(oNew).attr('href');
		iCurrent = Number(currentGallery.substring(6));
		
		clearInterval(timer);
		timer = setInterval("ChangePromo($(currentNav), $('a#promoNav'+getNextID()))", (iSeconds*1000));
	};
};

/* Gets ID of next promo for auto-rotate */
function getNextID() {
	iCurrent==iMax ? iCurrent=1 : iCurrent++;
	return iCurrent;
}

/* Gets ID of previous promo for previous button */
function getPrevID() {
	iCurrent==1 ? iCurrent=iMax : iCurrent--;
	return iCurrent;
}

