/***
 * Author: Ryan Kahn
 * Date: 03-29-2010
 */

//On page load begin headline scrolling
Event.observe(window, 'load', function() {
	beginHeadlineScrolling();
}); 

/**
 * Initializes the headline scrolling infinite loop
 */
function beginHeadlineScrolling(){
	//Foreach headline we want to tell them to start their displayNextHeadline loop
	$$(".headlineScroller").each(function(headlineScroller){
		displayNextHeadline(headlineScroller.identify());
	});
}

/**
 * 
 * @param headlineScrollerId - identifier for the container for the headline scrollers
 */
function displayNextHeadline(headlineScrollerId){
	var headlineScroller = $(headlineScrollerId);
	//Current headlines that are displayed (possibly plural, shouldnt be possible however but since its a css selector it is possible!)
	var currentHeadlines = $$("#"+headlineScrollerId + " a.displayed");
	if(currentHeadlines.size() == 0){//If there are no current headlines (should only be on the first pass)
		currentHeadline = headlineScroller.down("a");
	}else{//Grab the first headline
		currentHeadline = currentHeadlines[0];
	}
	//Make the current one no longer displayed
	currentHeadline.removeClassName("displayed");
	//Fade it away, notice the duration and scope.
	//Duration means it will take .8 seconds to go away
	//The queue and scope are used to say that once this is done other things added to the queue can be completed (the fade in that comes later)
	Effect.Fade(currentHeadline.identify(),{ duration: .8, queue: 'end', scope:headlineScrollerId});
	
	//Get the next headline
	nextHeadline = currentHeadline.next();
	if(nextHeadline == undefined){//If there is no next headline
		//Wrap to the top again
		nextHeadline = headlineScroller.down("a");
	}
	//Make it appear
	//Notice the duration of 1.8 seconds and queue and scope as mentioned earlier prevents this from being executed before the .8 second fade above is finished.
	//Scope means its only for elements inside this headliineScrollerId so we can have multiple fades happening at once
	Effect.Appear(nextHeadline.identify(),{ duration: 1.8, queue: 'end', scope:headlineScrollerId});
	nextHeadline.addClassName("displayed");
	
	//Loop it! the 6000 is the next time this code will be executed in ms
	setTimeout("displayNextHeadline('"+headlineScrollerId+"');", 6000);
}
