/* US Digital Partners / fading callouts script */

var count = 0;		// Used for navigation calculations
var curSlide = 0;
var nextSlide = 0;
var rotateSlides = null;

function usdpHomeCallouts(autorotateSlides, delay) {
	if (autorotateSlides) {
		rotateSlides = setInterval("fadeHomeCallouts('auto')",delay);
	}
}

function fadeHomeCallouts(rotateSlidesDirection, index) {
	
	// Get the number of callouts
	var totalHomeSlides = $('.homeslides').size();
	
	// Removes the 'selected' class on the nav link
	$('.toc:eq(' + count + ')').removeClass("selected");
	
	// Update the count based on the rotation type
	if (rotateSlidesDirection == "next") {
		clearInterval(rotateSlides); // Stop auto rotation
		count++;
		nextSlide++;
		if (count >= totalHomeSlides) { // Wrap back to the beginning
			count = 0;
			nextSlide = 1;
		}
	} else if (rotateSlidesDirection == "prev") {
		clearInterval(rotateSlides); // Stop auto rotation
		count--;
		nextSlide--;
		if (count < 0) {	// Wrap around to the end
			count = totalHomeSlides - 1;
			nextSlide = totalHomeSlides;
		}
	} else if (rotateSlidesDirection == "auto") {
		count++;
		nextSlide++;
		if (count >= totalHomeSlides) { // Wrap back to the beginning
			count = 0;
			nextSlide = 0;
		}
	} else if (rotateSlidesDirection == "index") {
		clearInterval(rotateSlides); // Stop auto rotation
		count = index;
		nextSlide = index; // Move to specific callout
	}

    // Fade out the current callout; Fade in the next callout
    var curSlideId = '#' + $('.homeslides').get(curSlide).id;
    var nextSlideId = '#' + $('.homeslides').get(nextSlide).id;

    
    
    
    
    $(curSlideId).fadeOut('medium', function () { 	// count+1 is used because the callouts numbering is not zero based
        
        $(nextSlideId).fadeIn('medium');


        // special functionality for changing the whole slider's background image - originally implemented for kindependence promo
        if (nextSlideId == '#slide_kindependence') $('#slide_container').css('background-image', 'url(/images/slide_background_white.jpg)');
        else $('#slide_container').css('background-image', 'url(/images/slide_background.jpg)');


    });
	curSlide = nextSlide;
	
	// Adds the 'selected' class on the nav link
	$('.toc:eq(' + count + ')').addClass("selected");
}

function kandooHomeFadeNext() {	// Change large image on a delay if autorotateSlides is true
	fadeHomeCallouts("next");
}

function kandooHomeFadePrev() {	// Change large image on a delay if autorotateSlides is true
	fadeHomeCallouts("prev");
}

function kandooHomeFadeTo(navIndex) {	// Change large image on a delay if autorotateSlides is true
	fadeHomeCallouts("index", navIndex);
}
