﻿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 = 4000;
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(false)
                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)){
        // 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").hide();        
        $("#spotlight_itemcontent a img").attr("src", featureImages[newFeature]);
        $("#spotlight_itemcontent a img").fadeIn(400);       
        
		$("#spotlight_itemcontent a img").attr("alt", featureAltText[newFeature]);
		$("#spotlight_itemcontent a").attr("href", featureLinks[newFeature]);
		$("#spotlight_itemcontent a").attr("name", featureTrackingInfo[newFeature]);	
		
		$("#spotlight_nav li div p a").attr("href", featureLinks[newFeature]);
		
		// change the links
		$("#currently_featured").html($("ul#spotlight_nav > li").eq(newFeature).find(".spotlight_itemfeatures").clone());
        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).hoverIntent(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) {
    	//set thumbnails
    	// $(this).find("a.thumbnail").css("background-image","url("+featureThumbs[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;
	    });			
    });

	$("#spotlight_nav_buttons a").click(function() {
		if ($(this).hasClass("prev") == true) {						
			if (!isNaN(currFeature)) {				
				var nextFeature = ((currFeature - 1) % 4);		
				if(nextFeature<0) {	                               
	                nextFeature = 3;	                
	            }	            	            
	            setFeature(nextFeature, true);
			}
		} else if ($(this).hasClass("next") == true) {
			if (!isNaN(currFeature)) {
				var nextFeature = ((currFeature + 1) % 4);
	            setFeature(nextFeature, true);
	            FeatureCount = FeatureCount + 1;
	            if(FeatureCount>7) {	                               
	                FeatureCount = 0;
	                return;
	            }
			}	
		}
	});

    // 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);

});

