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

/* Set inital promo and nav icon on page load */
var currentGallery = "div#photoGalleryImage"+iCurrent;
var currentNav = "a#pgNav"+iCurrent;

$(document).ready(function(){
	/* Set initial gallery nav state */
	$(currentNav).addClass("selected");
	/* Start timer */
	timer = setInterval("ChangePromo($(currentNav), $('a#pgNav'+getNextID()))", (iSeconds*1000));
	
	/* add click event function to switch promos */
	$("div#photoGalleryNav a.galleryNav").click(function () {
		ChangePromo($(currentNav), $(this));
		return false;
	});
});
		
/* Function to switch promos and restart timer */
function ChangePromo(oCurrent, oNew) {
	if ($(oCurrent).attr('href') != $(oNew).attr('href')) {
		$(oCurrent).removeClass("selected");
		$(oNew).addClass("selected");
		
		currentNav = $(oNew);
		
		$(currentGallery).fadeOut("normal");
		$($(oNew).attr('href')).fadeIn("normal");
		
		currentGallery = $(oNew).attr('href');
		iCurrent = Number(currentGallery.substring(18));
		clearInterval(timer);
		timer = setInterval("ChangePromo($(currentNav), $('a#pgNav'+getNextID()))", (iSeconds*1000));
	};
};
/* Gets ID of next promo for auto-rotate */
function getNextID() {
	//alert (iCurrent + ' ' + iMax);
	if (iCurrent==iMax) {
		iCurrent=1;
	} else {
		iCurrent=iCurrent+1;
	}
	return iCurrent;
}
