// global variables
var ticker;
var nextCharacterTimeout  = 50;
var headlineTimeout       = 5000;
var widgetOne             = "_";
var widgetTwo             = "-";
var widgetNone            = "";
var leadString            = "LATEST:&nbsp;";
var headlineIndex         = 0;
var headlineLength;
var headlineText;

var gaSummaries = new Array();  // text to show
var gaLinks = new Array();      // link to go to 
var gaPopups = new Array();     // true/false for popup
var gaScrollable = new Array(); // true/false for popup
var gaWidths = new Array();     // width of popup
var gaHeights = new Array();    // height of popup

// called when the body loads 
function init() {

  // call back to the page for the ticker items
  //addTickerItems();

  // start the ticker
  //	startTicker();
  //theObjects = document.getElementsByTagName("object");
  //for (var i = 0; i < theObjects.length; i++) {
	//theObjects[i].outerHTML = theObjects[i].outerHTML;
  //}
}

function addHeadline(headlineText, link, popup, width, height, scrollable) {
  // adds a headline to the ticker
  gaSummaries.push(headlineText);
  gaLinks.push(link);
  gaPopups.push(popup);
  gaScrollable.push((scrollable == true) ? 'yes' : 'no');
  gaWidths.push(width);
  gaHeights.push(height);
}

// ticker startup
function startTicker()
{
  // define run time values
  headlineIndex = -1;
  currentLength = 0;
  
  // locate base objects if we can
  if (document.getElementById) {	
    ticker = document.getElementById("ticker");
    // update the shell to have the lines
    var tickerShell = document.getElementById("tickerShell");
    if (tickerShell != null) tickerShell.className = "ticker";
    
    // start the ticker
    runTheTicker();   	
  } 
}

// ticker main run loop
function runTheTicker()
{
	var myTimeout;  
	
	// go for the next headline data block
	if(currentLength == 0)
	{
		headlineIndex++;
		headlineIndex = headlineIndex % gaSummaries.length;
		headlineText = gaSummaries[headlineIndex].replace(/&quot;/g,'"');		
		prefix 	     = "<span class=\"ticker_prefix\">" + leadString + "</span>";
		
    // set the index in the div for when we click on an item
		ticker.headlineIndex = headlineIndex;
	}
	
	// stuff the current ticker text into the anchor
	ticker.innerHTML = prefix + headlineText.substring(0, currentLength) + whatWidget();
	
	// modify the length for the substring and define the timer
	if(currentLength != headlineText.length)
	{
		currentLength++;
		myTimeout = nextCharacterTimeout;
	}	else {
		currentLength = 0;
		myTimeout = headlineTimeout;
	}
	// Call up the next cycle of the ticker
	setTimeout("runTheTicker()", myTimeout);
}

// widget generator
function whatWidget()
{
	if(currentLength == headlineText.length)
	{
		return widgetNone;
	}

	if((currentLength % 2) == 1)
	{
		return widgetOne;
	}
	else
	{
		return widgetTwo;
	}
}
    
function TickerClick(ticker) {
  // a headline has been clicked, therefore we need to show the correct page
  
  // first get the index of the currently showing ticker from the ticker
  var headlineIndex = ticker.headlineIndex;
  if (headlineIndex == null) headlineIndex = 0;
  
  // now we much change the URL to match, but being aware if it is a popup
  if (gaPopups[headlineIndex] == true) {
    popup( gaLinks[headlineIndex], "_blank", gaWidths[headlineIndex], gaHeights[headlineIndex], gaScrollable[headlineIndex]);
  } else {
    window.location.href=gaLinks[headlineIndex];
  }
}

function popup(url, name, width, height, scrollable) {
  // shows a pop up window
  var left = (screen.width) ? (screen.width-width)/2 : 0;
  var top = (screen.height) ? (screen.height-height)/3 : 0;
  // create the popup settings now
  var settings = 'height='+ height + ',width=' + width + ',top= ' + top + ',left=' + left + ',scrollbars=' + scrollable + ',resizable=no';
  // open the new window
  win = window.open(url, name, settings);
}
