﻿// ======================
//      News Ticker
// ======================
// Usage: 
//      initTicker(Number To Show, Character Delay (in ms), String Delay (in ms), Link Page)
// Example:
//      initTicker(3, 500, 5000, "news.aspx")
    var arrNews = new Array("", "", "", "", "", "", "");
    var arrIDs = new Array("", "", "", "", "", "");
    
    var chrPos = 0;         // Starting character position
    var strPos = 0;         // Starting string position
    
    var chrPeriod = 100;    // Delay between characters = 0.1 secs
    var strPeriod = 500;   // Delay between strings = 2 secs
    
    var chrTimer = null;  // Timer to show next character
    var strTimer = null;  // Timer to show next string

    var MainLoop = 5;       // Number of strings in main loop
    var RunningTimer = "c"  // To flag which timer is running: c / s
    var NewsPage = "../Redir.aspx"; // The news page to link to for each item
    
    function ShowNextChr()
    {
        var currStr = arrNews[strPos];  // The current string
        if(chrPos <= currStr.length)     // As long as we have not passed the end of the string
        {
            RunningTimer = "c";
            var tickerobj = document.getElementById('ticker');
            
            if(arrIDs[strPos] == "DEADLINKACTIVE")
                tickerobj.innerHTML = currStr.substr(0, chrPos);   // write from beginning to current chr position
            else
                tickerobj.innerHTML = "<a href='../Redir.aspx?ID=" + arrIDs[strPos] + "'>" + currStr.substr(0, chrPos) + "</a>";   // write from beginning to current chr position
            chrPos++;   // Increment chr position
            chrTimer = setTimeout("ShowNextChr()", chrPeriod);  // call this function again
        }
        else        // We've reached the end of the current string, so stop showing chrs, and move to next string
        {
            RunningTimer = "s";
            chrPos = 0;     // Reset back to starting character position as we will move to next string now
            strTimer = setTimeout("ShowNextStr()", strPeriod);
        }
    }
    function Pause()
    {
     if (RunningTimer=="c")      // If char timer running
        {
            clearTimeout(chrTimer); // Cancel the current timer
            chrPos = arrNews[strPos].length;    // Set char position to final char in current string
            var currStr = arrNews[strPos];  // The current string
        if(chrPos <= currStr.length)     // As long as we have not passed the end of the string
        {
            RunningTimer = "c";
var tickerobj = document.getElementById('ticker');
            tickerobj.innerHTML = "<a href='" + NewsPage + "?ID=" + arrIDs[strPos] + "'>" + currStr.substr(0, chrPos) + "</a>";   // write from beginning to current chr position
            chrPos++;   // Increment chr position
            //chrTimer = setTimeout("ShowNextChr()", chrPeriod);  // call this function again
        }
        }     
    }
    function ShowNextStr()
    {
        // Check if position is at end of available items or end of items to show
        if ((strPos < arrNews.length -1) && (strPos < MainLoop - 1))
            strPos++;       // Go to next string in array
        else
            strPos = 0;     // Reset to starting position

        chrTimer = setTimeout("ShowNextChr()", chrPeriod);  // In either case, start showing chrs again
    }
    
    function showPrev()
    {
        // This function is different from ShowNext, since when Previous is clicked, 
        // the user is taken directly to the string *before* the current one,
        // and not the previous character. In case it's clicked on the first string
        // nothing will happen.
        
        // Clear both timers
        clearTimeout(chrTimer);
        clearTimeout(strTimer);
        
        if (strPos > 0) 
            strPos--;
        
        chrPos = 0;          
        ShowNextChr();    
    }

    function showNext()     // called when the Next button is clicked
    {
        // Check which is the running timer currrenty
        if (RunningTimer=="c")      // If char timer running
        {
            clearTimeout(chrTimer); // Cancel the current timer
            chrPos = arrNews[strPos].length;    // Set char position to final char in current string
            ShowNextChr();      // Show the next (i.e. till last) char of current string
        }
        else    // If str timer running
        {
            clearTimeout(strTimer);     // Cancel the current timer
            if (strPos < arrNews.length-1)    // Check that we are not past the number of total items in the array
                strPos++;       // Go to next string
            else
                strPos = 0;     // Go back to first string
            chrPos = 0;     // In either case, set char position back to first char of string
            ShowNextChr();  // Show the next char
        }
    }
    
    function initTicker(mainloop, chrdelay, strdelay, newspage)
    {
        MainLoop = mainloop;
        chrPeriod = chrdelay;
        strPeriod = strdelay;
        NewsPage = newspage;
        document.write("<span id='ticker' class='text'></span><div id=\"headerimg\"> <a href='javascript:showPrev()'><img src='images/arrow-red-lft.gif'></a>&nbsp;<a href='javascript:Pause()'><img src='images/arrow-red-stop.gif'></a>&nbsp;<a href='javascript:showNext()'><img src='images/arrow-red-rft.gif'></a><img src='images/top-news-rft.gif'></div>");
        chrTimer = setTimeout("ShowNextChr()", chrPeriod);      // Start the timer
    }        
    
    
    //Sapinder 20-Aug-07
//Client Call back recieving function    
function RecieveLatestScore(context)
{
    var contents=context.split('@');
    if(contents.length>1)
    {
        context=contents[0].replace('#','(');
        context=context.replace('#',')');
        var tickerobj = document.getElementById('ticker');
       // tickerobj.innerHTML ='<a href="../Cricket/Fullscorecard.aspx?'+contents[1]+'">'+ context +'</a>';
        tickerobj.innerHTML ='<a href="Cricket/Fullscorecard.aspx?'+contents[1]+'" target=_blank>'+ context +'</a>';
    }
}
function initCricketTicker(scoreString)
{
    var contents=scoreString.split('@');
    if(contents.length>1)
    {
        scoreString=contents[0].replace('#','(');
        scoreString=scoreString.replace('#',')');
        document.write("<span id='ticker' class='text'>"+'<a href="Cricket/Fullscorecard.aspx?'+contents[1]+'">'+ scoreString +'</a>' +"</span><div id=\"headerimg\"><a href='javascript:GetLatestScore()'><img src='Images/refresh1.gif'></a></div>");
    }
}            