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

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

$(document).ready(function(){
	/* Preload Homepage Promo Images */
	/* Update this when gallery images change */
	MM_preloadImages(
		'_images/gallery2.jpg',
		'_images/gallery3.jpg',
		'_images/gallery4.jpg',
		'_images/gallery5.jpg'
	);
	
	/* GALLERY */
	/* Set initial gallery nav state */
	$(currentNav).addClass("selected");
	/* Start timer */
	timer = setInterval("ChangePromo($(currentNav), $('a#mgNav'+getNextID()))", (iSeconds*1000));
	
	/* add click event function to switch promos */
	$("div#marketingGalleryNav 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(22));
		clearInterval(timer);
		timer = setInterval("ChangePromo($(currentNav), $('a#mgNav'+getNextID()))", (iSeconds*1000));
	};
};
/* Gets ID of next promo for auto-rotate */
function getNextID() {
	if (iCurrent==iMax) {
		iCurrent=1;
	} else {
		iCurrent=iCurrent+1;
	}
	return iCurrent;
}