﻿
    var LeadString = "<span style='color:red'>INCIDENT:&nbsp;&nbsp;&nbsp; </span>";
       
    // Timer pointers
    var charTimerPointer = 0;
    var storyTimerPointer = 0;
    var fadeOutPointer = 0;

    function startTicker() 
    {
        LengthCounter = 0;

        if (Headlines.length > 0)
            runTicker();
        else
        {
            $("#tickerlink").html("There are currently no live incidents in the area");
        }
    }

    // function called to start the ticker and on a timeout
    function runTicker() 
    {
        if (LengthCounter == 0) {
            if (StoryCounter >= Headlines.length)
                StoryCounter = 0;

            if(StoryCounter < 0)
                StoryCounter = Headlines.length - 1;
                
            CurrentHeadline = Headlines[StoryCounter];
            TargetLink = Links[StoryCounter];
            $("#tickerlink").attr('href', TargetLink);
        }

        $("#tickerlink").html(LeadString + CurrentHeadline.substring(0, LengthCounter) + getSymbol());

        // Have we got to the end of the headline?
        if (LengthCounter != CurrentHeadline.length) 
        {// No
            LengthCounter++;
            charTimerPointer = setTimeout("runTicker()", CharacterTimeout);
        }
        else 
        {// Yes
            LengthCounter = 0;
            tickerFade();
        }        
    }

    // get either an underscore symbol or space
    function getSymbol() 
    {
        if (LengthCounter == CurrentHeadline.length) 
            return ' ';
        else
            return '_'
    }

    // Function called on mouse over to display the entire headline
    function tickerShow() 
    {
        if (Headlines.length > 0) {
            $("#tickerlink").html(LeadString + CurrentHeadline);

            // Stop any existing timeouts
            clearTimeout(charTimerPointer);
            clearTimeout(storyTimerPointer);
            clearTimeout(fadeOutPointer);

            tickerFade();
        }
    }
    
    // Set up a few timeouts to fade out the ticker at the end of the headline
    function tickerFade()
    {
        LengthCounter = 0;
        
        //Call the fade routine after a pause
        fadeOutPointer = setTimeout(function() {
            $('#tickerlink').fadeOut(400)
        }, StoryTimeout);

        StoryCounter++;
        
        storyTimerPointer = setTimeout("runTicker()",StoryTimeout+400);
        
        setTimeout(function(){
            $('#tickerlink').fadeIn(10)},StoryTimeout+410);
    }

    // One of the buttons has been clicked so move the news story along (or back)
    function tickerMove(move)
    {
        LengthCounter = 0;
        StoryCounter += move;

        // Stop any existing timeouts
        clearTimeout(charTimerPointer);
        clearTimeout(storyTimerPointer);
        clearTimeout(fadeOutPointer);

        runTicker();
    }

