/******************************************
    Filename: newsticker.js

    Author: Sandferne Solutions
    On Behalf of: BNA International

    Purpose: Display news ticker

    Version: 1.0
        Initial version

    Version: 1.1
        Added code to recalculate end headline point
*******************************************/

/************* CONFIGURATION **************

    This section is used to configure the
    behaviour of the ticker

********************************************/

var ssntc_width          = '754px';                  // width of ticker (in pixels)
var ssntc_height         = '18px';                   // height of ticker (in pixels)
var ssntc_blStop         = true;                     // pause on mouseover (true or false)
var ssntc_scrollspeed    = 2;                        // scroll speed (1 = slow, 5 = fast)
var ssntc_pauseinseconds = 2;                        // number of seconds to pause
var ssntc_xmlfile        = 'newsticker.xml'          // name and location of the news ticker xml file
var ssntc_spacebetweenheadlines = 0;                // number of spaces between headlines;

//***********END CONFIGURATION ***************/

/************* FUNCTIONS **************

    This section contains the code
    required to run the news ticker.

    Please do not change this code
    unless authorised to do so

****************************************/

var ssnt_content = "";
var ssnt_xmldoc;
var ssnt_scrollspeed = ssntc_scrollspeed;
var aw, mq;
var ssnt_fontsize = parseInt(ssntc_height) - 4;

var ssnt_pausepoint = parseInt(ssntc_width) - ((parseInt(ssntc_scrollspeed)+10) * (Math.round((parseInt(ssntc_width) / (parseInt(ssntc_scrollspeed) + 10)))));
var objssnt_headlines = new ossnt_headlines();

var ssnt_re_lessthan = new RegExp('\\[', "g");;
var ssnt_re_greaterthan = new RegExp('\\]', "g");;

function ssnt_initticker()
{
    var blSuccess = false;

    if (document.getElementById)
    {
        blSuccess = ssnt_loadXML();
    }
}

function ssnt_startticker()
{
    if (document.getElementById)
    {
        var tick = ssnt_getdivtext();

        document.getElementById('ssnt_ticker').innerHTML = tick;

        ssnt_content = objssnt_headlines.getnext();
        mq = document.getElementById("ssnt_marquee");
        mq.style.left=(parseInt(ssntc_width)+10)+"px";
        mq.innerHTML='<span id="ssnt_tickertext">'+ssnt_content+'<\/span>';
        aw = document.getElementById("ssnt_tickertext").offsetWidth;

        lefttime=setInterval("ssnt_scrollticker()",35);
    }
}


function ssnt_getdivtext()
{
    var tick = '<div style="position:relative;margin-left:5px;width:'+ssntc_width+';height:'+ssntc_height+';overflow:hidden"';

    if (ssntc_blStop)
    {
        tick += ' onmouseover="ssnt_scrollspeed=0" onmouseout="ssnt_scrollspeed=ssntc_scrollspeed"';
    }

    tick +='><div id="ssnt_marquee" style="position:absolute;left:0px;top:0px;'+';font-size:'+ssnt_fontsize+'px;white-space:nowrap;"><\/div><\/div>';

    return tick;
}

function ssnt_loadXML(){
    var blSuccess = false;

    if (document.implementation && document.implementation.createDocument){
		ssnt_xmldoc = document.implementation.createDocument("", "", null);
		ssnt_xmldoc.onload = ssnt_getheadlines;

		blSuccess = true;
	}else if (window.ActiveXObject){
		ssnt_xmldoc = new ActiveXObject("Microsoft.XMLDOM");
		ssnt_xmldoc.onreadystatechange = function (){
							if (ssnt_xmldoc.readyState == 4) {ssnt_getheadlines();}
						};

		blSuccess = true;
 	}

    if(blSuccess){
	  ssnt_xmldoc.load(ssntc_xmlfile);
	}

	return blSuccess;
}


function ssnt_getheadlinespacer()
{
    var szSpacer = '';

    for(var i=0; i < ssntc_spacebetweenheadlines; i++)
    {
        szSpacer += '&nbsp;';
    }

    return szSpacer;
}
function ssnt_getheadlines(xmlheadlines)
{

    var szHeadlines = '';

    var oRoot = ssnt_xmldoc.documentElement;
    var oHeadlines = oRoot.childNodes;
    var szHeadline = '';
    var szLink = '';
    var szSpacer = ssnt_getheadlinespacer();

    for(var i=0; i < oHeadlines.length; i++)
    {
        if ((oHeadlines[i].nodeType == 1) )
        {

            for(var j=0; j < oHeadlines[i].childNodes.length; j++)
            {

                if(oHeadlines[i].childNodes[j].nodeType == 1)
                { 

                    if(oHeadlines[i].childNodes[j].nodeName == 'text')
                    {
                        //headline text
                        if(szHeadline.length == 0)
                        {
                            szHeadline = oHeadlines[i].childNodes[j].firstChild.nodeValue;

                            szHeadline = szHeadline.replace(ssnt_re_lessthan, '<');;
                            szHeadline = szHeadline.replace(ssnt_re_greaterthan, '>');
                        }
                        else
                        {
                            //previous node was not a link so add static string
                            szHeadline = szHeadline.replace(ssnt_re_lessthan, '<');
                            szHeadline = szHeadline.replace(ssnt_re_greaterthan, '>');

                            szHeadlines += szHeadline + szSpacer;
                            szHeadline = oHeadlines[i].childNodes[j].firstChild.nodeValue;
                            szLink = '';

                            objssnt_headlines.add(szHeadlines);
                            szHeadlines = '';
                        }
                    }
                    else if(oHeadlines[i].childNodes[j].nodeName == 'link')
                    {
                        szHeadlines = ssnt_createlink(szHeadline, oHeadlines[i].childNodes[j].firstChild.nodeValue);
                        szHeadlines += szSpacer;

                        objssnt_headlines.add(szHeadlines);
                        szHeadlines = '';
                        szHeadline = '';
                        szLink = '';
                    }
                }
            }
        }
    }

    ssnt_content = szHeadlines;
    ssnt_startticker();

    return;
}

function ssnt_createlink(xszheadline, xszlink)
{
    var szLink = xszlink;
    var szHeadlineLink = "";

    szLink = szLink.replace(/"/, '');
    szLink = szLink.replace(ssnt_re_lessthan, '<');
    szLink = szLink.replace(ssnt_re_greaterthan, '>');

    if(szLink.length > 0)
    {
        szHeadlineLink = '<a href="' + szLink + '">';
    }

    szHeadlineLink += xszheadline;

    if(szLink.length > 0)
    {
        szHeadlineLink += '</a>';
    }

    return szHeadlineLink;
}

function ssnt_scrollticker()
{
    var iLeftValue;

    if(blPauseComplete)
    {
        if(parseInt(mq.style.left)>(-10 - aw))
        {
            iLeftValue = parseInt(mq.style.left)-ssnt_scrollspeed+"px";
        }
        else
        {
            //start scrolling again
            iLeftValue = parseInt(ssntc_width)+10+"px";
            document.getElementById("ssnt_tickertext").innerHTML = objssnt_headlines.getnext();
            aw = document.getElementById("ssnt_tickertext").offsetWidth;
        }
        mq.style.left = iLeftValue;

        if(iLeftValue == (ssnt_pausepoint+"px"))
        {
            pause();
        }
    }

}

function ssnt_openlink(xszLink)
{
    //document.getElementById("ssnt_ticker").innerHTML = "";
    window.location.href = xszLink;
}

var blPauseComplete = true;
var oPauseID;

function pause()
{
   pause_start();

   return
}
function pause_start()
{
    blPauseComplete = false;
    oPauseID = setInterval("pause_stop()", ssntc_pauseinseconds*1000);
}

function pause_stop()
{
    clearInterval(oPauseID);
    blPauseComplete = true;
}

function ossnt_headlines()
{
    this.item = new Array(0);
    this.total = 0;
    this.pointer = 0;

    this.add = function (xszHeadline)
                {		
                    this.item.push('&nbsp;&nbsp;' + xszHeadline);
                    this.total++;
                }

    this.getnext = function ()
                {
                    var szHeadline = '';
                    if(this.pointer >=this.total)
                    {
                        this.pointer = 0;
                    }

                    szHeadline = this.item[this.pointer];

                    this.pointer++;

                    return szHeadline;
                }
}

window.onload +=ssnt_initticker;