/**
 * Adapted from http://www.bbcnews.com/
 */

// Ticker startup
function startTicker() {
	
	// Define run time values
	theCurrentStory     = -1;
	theCurrentLength    = 0;
	
	// Locate base objects
	if (document.getElementById) {	
		theAnchorObject     = document.getElementById("ticker-wrapper");
		runTheTicker();   	
	} else {
		document.write("<style>.ticki{display:none;}.ticko{border:0px; padding:0px;}</style>");
		return true;
	}
}

// Ticker main run loop
function runTheTicker() {
	var myTimeout;  
	// Go for the next story data block
	if (theCurrentLength == 0) {
		theCurrentStory++;
		theCurrentStory      = theCurrentStory % theItemCount;
		theStorySummary      = theSummaries[theCurrentStory].replace(/&quot;/g,'"');		
		theTargetLink        = theSiteLinks[theCurrentStory];
		theAnchorObject.href = theTargetLink;
		thePrefix 	     	 = "<p>" + theLeadString;
	}
	
	/* Second arg for substr() */
	var subStrEnd = theCurrentLength;
	
	/* Find char length and start moving to the left if we're over count */
	var subStrStart = 0;
	if (theCurrentLength > maxCharCount) {
		subStrStart = theCurrentLength - maxCharCount;
		subStrEnd = theCurrentLength; // - maxCharCount;
	}
	
	// Stuff the current ticker text into the anchor
	theAnchorObject.innerHTML = thePrefix + theStorySummary.substring(subStrStart,subStrEnd) + whatWidget() + "</p>";
	
	// Modify the length for the substring and define the timer
	if(theCurrentLength != theStorySummary.length) {
		theCurrentLength++;
		myTimeout = theCharacterTimeout;
	} else {
		theCurrentLength = 0;
		myTimeout = theStoryTimeout;
	}
	// Call up the next cycle of the ticker
	setTimeout("runTheTicker()", myTimeout);
}

// Widget (cursor) generator
function whatWidget() {
	if (theCurrentLength == theStorySummary.length) {
		return theWidgetNone;
	}

	if ((theCurrentLength % 2) == 1) {
		return theWidgetOne;
	} else {
		return theWidgetTwo;
	}
}

var theCharacterTimeout = 80;
var theStoryTimeout     = 3000;
var theWidgetOne        = "<span class=\"cursor\">:</span>";
var theWidgetTwo        = "<span class=\"cursor\">-</span>";
var theWidgetNone       = "";
var theLeadString       = "";

var maxCharCount		= 100;

//startTicker();
