var currFeature;
var newFeature = 0;
var FeatureCount = 0;

// "slideshowPlaying" value here determines whether slideshow starts on page load or not
var slideshowPlaying = true;
var slideshowTimerInterval = 5000;
var slideshowTimerPtr;

if(featureImages) {
	var j = 0;
	var p = featureImages.length;
	var preBuffer = new Array();
	 
	for (i = 0; i < p; i++){
		preBuffer[i] = new Image();
		preBuffer[i].src = featureImages[i];
	}
}		

function slideshowTimer() {
    if (slideshowPlaying) {
        if (!isNaN(currFeature)) {
            var nextFeature = ((currFeature + 1) % 4);
            setFeature(nextFeature, true);
            FeatureCount = FeatureCount + 1;
            if(FeatureCount>7)
            {
                slideshowPlay(true)
                slideshowTimerPtr = clearTimeout(slideshowTimerInterval);
                FeatureCount = 0;
                return;
            }
        }
        
        slideshowTimerPtr = setTimeout("slideshowTimer()", slideshowTimerInterval);
    }
}

function setFeature(newFeature, buttonOn) {

    // Set up button display
	$("ul#spotlight_nav > li").removeClass("on").eq(newFeature).addClass("on");
	
	// If click event, show new content
    if ((buttonOn) && (currFeature != newFeature)){

        // Move arrow pointer
        var top1 = $("ul#spotlight_nav > li").eq(newFeature).offset().top;    // Position of image div
        var top2 = 35;                                                        // Must match ".spotlight_headings li" height
        var top3 = 20;                                                        // Must match "#spotlight_heading_pointer" height
        
        // If we have a div main, remove it.
        if ($("#main").length > 0) {
            var posDivMain = $("#main").offset();                             // Position of main div
            top1 = top1 - posDivMain.top; 
        }
        else {
            top1 = top1 - 70;
        }
        
        // Calculate final top of pointer
        var top4 = top1 + (top2 / 2) - (top3 / 2);
	    
        $("#spotlight_heading_pointer").addClass("show").css("top",(top4 + "px"));

        // Load new content
	    $("ul#spotlight_nav > li").eq(currFeature).find(".spotlight_itemfeatures").removeClass("on");

	    $("ul#spotlight_nav > li").eq(newFeature).find(".spotlight_itemfeatures").addClass("on");
	    
        // Change image, link and alt text
        $("#spotlight_itemcontent a img").attr("src", featureImages[newFeature]);
			$("#spotlight_itemcontent a img").attr("alt", featureAltText[newFeature]);
			$("#spotlight_itemcontent a").attr("href", featureLinks[newFeature]);
			$("#spotlight_itemcontent a").attr("name", featureTrackingInfo[newFeature]);
	
        currFeature = newFeature;
	}
}

function slideshowPlay(newSlideshowState) {

    // Hide all controls
    $(".spotlight_slideshow_control").hide();
    clearTimeout(slideshowTimerPtr);
    
    if (!newSlideshowState) {
        // Pause the slideshow
        $("#spotlight_slideshow_control_paused").show();
    }
    else {
        // Play the slideshow
        $("#spotlight_slideshow_control_playing").show();
        slideshowTimerPtr = setTimeout("slideshowTimer()", slideshowTimerInterval);
    }
    
    slideshowPlaying = newSlideshowState;
}

$(document).ready(function(){

    // Initialize default feature
    setFeature(newFeature, true); 

    // Set up button actions for hover and click
    $("ul#spotlight_nav > li").not(".spotlight_itemfeatures").each(function(i) {
	    $(this).hover(function(){
		    setFeature(i, true);
		    slideshowPlay(false);
		    window.status=featureLinks[i];  // Note: writing to status bar is turned OFF in Firefox by default - the user must manually enable it in "Preferences".
		}, function(){
		    setFeature(currFeature, false);
		    window.status='';
	    });					
    });

    $("ul#spotlight_nav > li").each(function(i) {
	    $(this).click(function(){
		    // Build WSS parameters and fire the event
            buttonTitle = $("ul#spotlight_nav > li > h4").eq(i).text().replace(/ /g,"+");
            lid = "FArea4_Bu" + (i + 1) + "_" + buttonTitle;
            lpos = "FArea4_Bu" + (i + 1);
            _hbLink(lid, lpos)

            location.href=featureLinks[i];
            
            // Returning true allows clicked element within the jquery element to process it's onclick event (e.g. hyperlinks work)
            return false;
	    });					
    });

    // Set up slideshow buttons
    $(".spotlight_slideshow_control").click(function(){

        // Record user action
        lid = "FArea4_" + (slideshowPlaying ? "Pause" : "Play") + "Button";
        _hbLink(lid, "");
        
        slideshowPlay(!slideshowPlaying);
        
        return false;
    });
    
    // Set up slideshow
    slideshowPlay(slideshowPlaying);

});

