﻿var d=document;
function dce(obj) {
	return d.createElement(obj); // called like this: eDIV = dce("div");
}
function geID(objID) {
	return d.getElementById(objID); //called like this: geID("mContainer");
}

//Setup ticker Array to store the contents
var line=[];
/*line[1]="Static line 1 from the .js file";*/
/*line[2]="This is a static line 2";*/

//Specify Main Variables
var ts_fontsize="14px"
var ts_bgColor = "Transparent"
var ts_brderColor = "none"
var ts_fontweight = "bold"
var temp=""
var nextchar=-1;
var nextline=1;
var cursor="_"
var charspeed=35;
var linepause=2000;
var bnspinterval=30000; /************ Change this to reflect an actual time *************/
var animateTimeoutInt=1000;
var animateloop;
var animateloopchars;
var tickerActivated=false;
var bnews_active=false;
var serverPollActivated=false;
var bnews_ticker_loop;
var bnews_server_poll;
var bnews_loop_count=0;
var bnews_loop_count_max=5;
var longestmessage=1;
var newscontainerform = dce('form');
var newscontainer = dce('input');

function createHTMLticker ()
{
    //Calculate the longest text message chars length
    for (i=2;i<line.length;i++)
    {
    if (line[i].length>line[longestmessage].length)
    longestmessage=i
    }

    //Set scroller width

    tscroller_width = 72; //82 previous value
    
    lines=line.length-1; //--Number of text lines
    //Write out the ticker text to the screen
    newscontainerform.setAttribute('name', 'breakingnewsform');
    newscontainerform.setAttribute('id', 'breakingnewsform');
    newscontainer.setAttribute('id', 'breakingnews');
    newscontainer.setAttribute('name', 'breakingnews'); 
    newscontainer.setAttribute('size', tscroller_width); 
    
    newscontainer.setAttribute("style", "background-color: "+ts_bgColor+"; color: font-family: verdana; font-size: "+ts_fontsize+"; border:1px solid "+ts_brderColor+"; font-weight:"+ts_fontweight+"; onfocus='blur()'");  
    
    newscontainerform.appendChild(newscontainer);
}

function animate()
{       
    if (temp==line[nextline] & temp.length==line[nextline].length & nextline!=lines)
    {
    nextline++;
    nextchar=-1; 
    document.forms["breakingnewsform"].breakingnews.value=temp;
    temp="";
    setTimeout("nextstep()",linepause)
    }
    else if (nextline==lines & temp==line[nextline] & temp.length==line[nextline].length)
    {
    nextline=1;
    nextchar=-1;
    document.forms["breakingnewsform"].breakingnews.value=temp;
    temp="";

        if(bnews_loop_count<bnews_loop_count_max)
        {
        bnews_ticker_loop=setTimeout("nextstep()",linepause);
        bnews_loop_count++;
        } else {
            bnews_loop_count=0;
            resttickervalues();
            check4BreakingNews();
        }
    }
    else
    {
    nextstep()
    }
}

function nextstep()
{
    if (cursor=="_"){
    cursor=" "}
    else if (cursor==" "){
    cursor="_"}
    else if (cursor=="_"){
    cursor=" "}
    else if (cursor==" "){
    cursor="_"}
    
    nextchar++;
    temp+=line[nextline].charAt(nextchar);
    
    document.forms["breakingnewsform"].breakingnews.value=temp+cursor
    animateloopchars = setTimeout("animate()",charspeed);
}

function buildticker()
{   var targetdiv = geID("ticker");
    targetdiv.appendChild(newscontainerform);
    animateloop = setTimeout(function(){animate();}, animateTimeoutInt);
    tickerActivated = true;
 }
 
function check4BreakingNews () 
{
    var myRequest = new ajaxObject('functions/call_breaking_news.asp');
    /* USE THIS LINE FOR TESTING var myRequest = new ajaxObject('vars.asp');*/
    myRequest.callback = function(responseText) 
    {
        eval(responseText);
        var targetdiv = geID("newsticker");
        var targetdiv2 = geID("breakingnews");
        
        if ((responseText!="")&(bnews_active))
        {
             createHTMLticker();
             if(serverPollActivated){
             clearInterval(bnews_server_poll);
             serverPollActivated=false;
             }
             targetdiv2.value="";
             targetdiv.style.display = 'block';
             resttickervalues();
             buildticker();
             
         } else if ((!bnews_active)&(!serverPollActivated)){
                targetdiv2.value="";
                targetdiv.style.display = 'none';
                resttickervalues();
                tickerActivated=false;
                bnews_server_poll = setInterval(check4BreakingNews, bnspinterval);
                serverPollActivated=true;
                }
    }
    myRequest.update(); 
}

function resttickervalues ()
{
                clearTimeout(bnews_ticker_loop);
                clearTimeout(animateloop);
                clearTimeout(animateloopchars);
                charspeed=35;
                linepause=2000;
                bnspinterval=20000;
                animateTimeoutInt=1000;
}