/* US Digital Partners / written for nuvision */

var counter = 0;		// Used for image calculations
var curDiv = 1;
var nextDiv = 1;

var autorotate = true;		// Auto rotate images
var delay = 5000;			// Time delay for the image rotation
var totalDivs;
function usdpAutorotate(numSlides) {	// Change large image on a delay if autorotate is true
	totalDivs = numSlides;
	if (autorotate) {
		rotate = setInterval("fadeDivs('auto')",delay);
	}
}

function fadeDivs(rotateDirection, index) {
	// Removes the selected class on the nav
	$('.toc:eq(' + counter + ')').removeClass("selected");
	
	// Control rotation
	if (rotateDirection == "next") {
		clearInterval(rotate); // Stop auto rotation
		counter++;
		nextDiv++;
		if (counter >= totalDivs) {
			counter = 0;
			nextDiv = 1;
		}
	} else if (rotateDirection == "prev") {
		clearInterval(rotate);
		counter--;
		nextDiv--;
		if (counter < 0) {
			counter = totalDivs - 1;
			nextDiv = totalDivs;
		}
	} else if (rotateDirection == "auto") {
		counter++;
		nextDiv++;
		if (counter >= totalDivs) {
			counter = 0;
			nextDiv = 1;
		}
	} else if (rotateDirection == "index") {
		clearInterval(rotate);
		counter = index - 1;
		nextDiv = index;
	}
	// Fade out the current div, fade in the next one
	$("#slide" + curDiv).fadeOut("medium", function () { 	
		$("#slide" + nextDiv).fadeIn("medium"); 
	});
	curDiv = nextDiv;
	
	// Adds the selected class on the nav
	$('.toc:eq(' + counter + ')').addClass("selected");
}

function usdpRotateNext() {	// Change large image on a delay if autorotate is true
	fadeDivs("next");
}

function usdpRotatePrev() {	// Change large image on a delay if autorotate is true
	fadeDivs("prev");
}

function usdpRotateIndex(navIndex) {	// Change large image on a delay if autorotate is true
	fadeDivs("index", navIndex);
}