﻿// ======================
//      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 = "../news/full.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='" + arrIDs[strPos] + "' target=_blank style='color:white;text-decoration:none;'>" + currStr.substr(0, chrPos) + "</a>";   // write from beginning to current chr position
            tickerobj.setAttribute("href", arrIDs[strPos]);
            tickerobj.innerHTML = "" + currStr.substr(0, chrPos) + "";   // 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='" + arrIDs[strPos] + "' target=_blank style='color:white;text-decoration:none;'>" + currStr.substr(0, chrPos) + "</a>";   // write from beginning to current chr position
            tickerobj.setAttribute("href", arrIDs[strPos]);
            tickerobj.innerHTML = "" + currStr.substr(0, chrPos) + "";   // 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;
    var cName = document.URL;
    if (cName.indexOf("Default.aspx") > -1) {
        cName = "tickerPos";
    }
    else {
        cName = "tickerInnerPos";
    }

    /*document.write("<div id=\"headerimg\" class="+cName+"><span style='padding-right:1px;'><a href='javascript:showPrev()'><img src='images/Tickerprevious.gif' border=0></a></span><span style='padding-right:1px;'><a href='javascript:Pause()'><img src='images/Tickerpause.gif' border=0></a></span><a href='javascript:showNext()'><img src='images/Tickerplay.gif' border=0></a></div><span id='ticker' class='tick'></span>");*/
    document.write("<div style='float:left;width:668px;border-top:0px solid #9C9899'><ul style='padding:0px; margin:0px; list-style-type:none;'><li style='float:left;width:345px;text-align:left;margin-top:5px;margin-left:5px'><span class='tick'><a href='" + arrIDs[0] + "' id='ticker' target=_blank style='color:black;text-decoration:none;outline:0;'></a></span></li><li style='float:right;border:solid 0px black; '><span style='padding-right:1px; text-decoration:none;'><a href='javascript:showPrev()' ><img src='images/next.jpg' border=0 / ></a></span><span style='padding-right:1px;'><a href='javascript:Pause()'><img src='images/play.jpg' border=0></a></span><a href='javascript:showNext()'><img src='images/prev.jpg' border=0></a></li></ul></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="#" target=_blank>'+ context +'</a>';
//        //tickerobj.innerHTML ='+ context +';
//    }
//}
//function initCricketTicker(scoreString)
//{
//    var contents=scoreString.split('@');
//    if(contents.length>1)
//    {
//        scoreString=contents[0].replace('#','(');
//        scoreString=scoreString.replace('#',')');
//        document.write("<span id='ticker' class='tick'>"+'<a href="#">'+ scoreString +'</a>' +"</span><div id=\"headerimg\"><a href='javascript:GetLatestScore()'><img src='../Images/refresh1.gif'></a></div>");
//       // document.write("<span id='ticker' class='gallery_right'>"+''+ scoreString +'' +"</span><div id=\"headerimg\"><a href='javascript:GetLatestScore()'><img src='../Images/refresh1.gif'></a></div>");
//    }
//}            
