// Ticker startup

function startNewsTicker(){
	// Locate base objects
	if ((newscontainerObj=MM_findObj("newscontainer"))!=null) {	
		newsListObj = newscontainerObj.getElementsByTagName("li") ; 
	
	//  populate arrays
		for ( i = 0 ; i < newsListObj.length ; i = i + 1  ) {
			theSummaries[i] = newsListObj[i].getElementsByTagName("a")[0].innerHTML 
			theSiteLinks[i] = newsListObj[i].getElementsByTagName("a")[0].href
		}
		
//		theNewsObject.innerHTML = "<a id='newsanchor' href='#'  ></a>" 
		thePrefix 	     			= "<span class=\"tickls\">" + theLeadString + "&nbsp;"+  "</span>" ;
		newscontainerObj.innerHTML = "<a id='newsanchor' href='#' onmouseover='pauseNewsTicker()' onmouseout='restartNewsTicker()' ></a>" 
		
		if ((theAnchorObject=MM_findObj("newsanchor"))!=null) {	
			runNewsTicker(); 
		}
	}
	else {
		document.write("<style>.ticki{display:none;}.ticko{border:0px; padding:0px;}</style>");
		return true;
	}
}


function populateCurrentStory() {
		theStorySummary				= theSummaries[theCurrentStory].replace(/&quot;/g,'"') ;		
		theAnchorObject.href 	= theSiteLinks[theCurrentStory] ;
}



/* Ticker main run loop */
/* ==================== */

function runNewsTicker(){
	this.myTimeout;  
	theAnchorObject.innerHTML = thePrefix + theStorySummary.substring(0,theCurrentLength) + whatWidget() ;
	
	if ( theCurrentLength != theStorySummary.length ) {
		theCurrentLength = theCurrentLength + 1 ;
		this.myTimeout = theCharacterTimeout ;
	}
	else {
		populateCurrentStory() ;
		getNextStory();
		this.myTimeout = theStoryTimeout ;
	}
	

	// Call up the next cycle of the ticker
	runNewsTickerFlag =  setTimeout("runNewsTicker()", this.myTimeout);
}


function getNextStory() {
	theCurrentStory				= theCurrentStory + 1 ;
	theCurrentStory				= theCurrentStory % theSummaries.length ;
	theCurrentLength = 0;
}


function getPrevStory() {
	theCurrentStory				= theCurrentStory - 1 ;
	if (theCurrentStory < 0 ) {
		theCurrentStory		= theSummaries.length - 1
	}
	theCurrentLength = 0;
}


function showCompleteLine() {
//	populateCurrentStory() ; 
	theAnchorObject.innerHTML = "<span class=\"tickls\">" + theLeadString + "&nbsp;"+  "</span>"  + theStorySummary  ;
	theCurrentLength = theStorySummary.length ;
}


function pauseNewsTicker() {
	showCompleteLine();
	if ((theButton=MM_findObj('newsbuttonplaypause'))!=null)  {				
				theButton.className = "news_stop" ;
	}
	window.clearTimeout(runNewsTickerFlag);
}


function restartNewsTicker() {
	window.clearTimeout(runNewsTickerFlag) ;
	if ((theButton=MM_findObj('newsbuttonplaypause'))!=null)  {				
				theButton.className = "news_start" ;
	}
	runNewsTicker() ;
}


function startStop() {	
	if ((theButton=MM_findObj('newsbuttonplaypause'))!=null)  {
		if (theButton.className == "news_start") {
				pauseNewsTicker()	}
		else {
				restartNewsTicker();
		}
	}
}


function nextStory() {
	getNextStory() ;
	populateCurrentStory() ;
	pauseNewsTicker();
}


function prevStory() {
	getPrevStory() ;
	populateCurrentStory() ;
	pauseNewsTicker();
}



// Widget generator
function whatWidget() {
	if ( theCurrentLength == theStorySummary.length ) {
		return theWidgetNone ;
	}

	if ( (theCurrentLength % 2) == 1 ) {
		return theWidgetOne ;
	}
	else {
		return theWidgetTwo ;
	}
}


var theCurrentStory     = 0;
var theCurrentLength    = 0;
var theAnchorObject
var theCharacterTimeout = 40;
var theStoryTimeout     = 1000;
var theWidgetOne        = "_";
var theWidgetTwo        = "-";
var theWidgetNone       = "";
var theLeadString       = "NEWS:&nbsp;" 
var runNewsTickerFlag ;
var thePrefix 			= "" ; 
var theStorySummary 	= "" ;

var newsListObj 
var newscontainerObj

var theSummaries 				= new Array();
var theSiteLinks 				= new Array();

startNewsTicker();	