﻿var theTickerObject;
var theTickerSpan;
var theCurrentRSSItem;
var theCurrentLength;
var theRSSItem;
var theRSSLink;
var theWidgetOne = "_";
var theWidgetTwo = " - ";
var theWidgetNone = "";

// Ticker startup
function startTicker()
{
    // Define run time values
    theCurrentRSSItem     = -1;
    theCurrentLength    = 0;

    // Locate base objects
    if (document.getElementById("tickerAnchor") && document.getElementById("tickerSpan"))
    {
        theTickerObject = document.getElementById("tickerAnchor");
        theTickerSpan = document.getElementById("tickerSpan");
        runTheTicker();    
    }
    else 
    {
        //Hide the ticker
        document.write("<style>.ticker{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)
    {
        theCurrentRSSItem++;
        theCurrentRSSItem       = theCurrentRSSItem % ItemCount;
        theRSSItem              = RSSItems[theCurrentRSSItem].replace(/&quot;/g,'"');  
        theRSSLink              = RSSLinks[theCurrentRSSItem];
        thePrefix               = "<span class=\"tickerPrefix\">" + StartString + "</span>";
        theTickerObject.href    = theRSSLink;
        theTickerSpan.innerHTML = thePrefix;
    }
    // Stuff the current ticker text into the anchor
    theTickerObject.innerHTML = theRSSItem.substring(0,theCurrentLength) + whatWidget();
    
    // Modify the length for the substring and define the timer
    if(theCurrentLength != theRSSItem.length)
    {
        theCurrentLength++;
        myTimeout = CharacterTimeout;
    }
    else
    {
        theCurrentLength = 0;
        myTimeout = TickerTimeout;
    }
    
    // Call up the next cycle of the ticker
    setTimeout("runTheTicker()", myTimeout);
}

// Widget generator
function whatWidget()
{
    if(theCurrentLength == theRSSItem.length)
    {
        return theWidgetNone;
    }

    if((theCurrentLength % 2) == 1)
    {
        return theWidgetOne;
    }
    else
    {
        return theWidgetTwo;
    }
}