/* US Digital Partners / fading callouts script */

var counter = 0;		// Used for navigation calculations
var curCallout = 1;
var nextCallout = 1;
var rotate = null;

function usdpCallouts(autorotate, delay) {
    
	if (autorotate) {
		rotate = setInterval("fadeCallouts('auto')",delay);
	}
}

function fadeCallouts(rotateDirection, index) {
	
	// Get the number of callouts
	var totalCallouts = $('.callouts').size();
	
	// Removes the 'selected' class on the nav link
	$('.toc:eq(' + counter + ')').removeClass("selected");
	
	// Update the counter based on the rotation type
	if (rotateDirection == "next") {
		clearInterval(rotate); // Stop auto rotation
		counter++;
		nextCallout++;
		if (counter >= totalCallouts) { // Wrap back to the beginning
			counter = 0;
			nextCallout = 1;
		}
	} else if (rotateDirection == "prev") {
		clearInterval(rotate); // Stop auto rotation
		counter--;
		nextCallout--;
		if (counter < 0) {	// Wrap around to the end
			counter = totalCallouts - 1;
			nextCallout = totalCallouts;
		}
	} else if (rotateDirection == "auto") {
		counter++;
		nextCallout++;
		if (counter >= totalCallouts) { // Wrap back to the beginning
			counter = 0;
			nextCallout = 1;
		}
	} else if (rotateDirection == "index") {
		clearInterval(rotate); // Stop auto rotation
		counter = index - 1;
		nextCallout = index; // Move to specific callout
	}
	
	// Fade out the current callout; Fade in the next callout
	$("#callout" + curCallout).fadeOut("medium", function () { 	// counter+1 is used because the callouts numbering is not zero based
		$("#callout" + nextCallout).fadeIn("medium"); 
	});
	curCallout = nextCallout;
	
	// Adds the 'selected' class on the nav link
	$('.toc:eq(' + counter + ')').addClass("selected");
}

function usdpFadeNext() {	// Change large image on a delay if autorotate is true
	fadeCallouts("next");
}

function usdpFadePrev() {	// Change large image on a delay if autorotate is true
	fadeCallouts("prev");
}

function usdpFadeTo(navIndex) {	// Change large image on a delay if autorotate is true
	fadeCallouts("index", navIndex);
}
