NewsTicker = function(contentId, tickerId) {
    this.newsItem=0;
    this.linePos=0;
    this.newsItems=new Array();
    this.newsLinks=new Array();
    this.newsTicker='';
    this.timer=new Timer(this);
    this.startTicker(contentId,tickerId);
}

/* startTicker function, retrieves html elements and starts the ticker */
NewsTicker.prototype.startTicker = function( contentId, tickerId ) {

    //Get the list containing the news items to be displayed
    var tickerContent = document.getElementById(contentId);

    //Hide the source list
    tickerContent.style.visibility = 'hidden';
    tickerContent.style.display = 'none';

    //Get the html element the ticker is to be displayed in
    this.newsTicker=document.getElementById(tickerId);

    //If the ticker content element is found
    if (tickerContent) {

        //Retrieve a list of list items from the content list
        var newsItemElements=tickerContent.getElementsByTagName('li');

        //Iterate thorugh each list item and add it to the newsItems array
        for(var i=0;i<newsItemElements.length;i++) {
            var localNewsLinks = newsItemElements[i].getElementsByTagName('a');
            if (localNewsLinks && localNewsLinks.length>0) {
                this.newsItems[i]=localNewsLinks[0].childNodes[0].nodeValue;
                this.newsLinks[i]=localNewsLinks[0].href;
            } else {
                this.newsItems[i]=newsItemElements[i].childNodes[0].nodeValue;
                this.newsLinks[i]='';
            }
        }

        //Start the ticker
        this.doTicker();
    }
}


/* doTicker function, displays the ticker output */
NewsTicker.prototype.doTicker = function() {

    // If the ticker has finished the current new item
    if (this.linePos==this.newsItems[this.newsItem].length) {

        //Set the line position to 0 and move onto the next news item
        this.linePos=0;
        this.newsItem = ++this.newsItem % this.newsItems.length;

        var newsTickerLinks = this.newsTicker.getElementsByTagName('a');

        //Replace the flasher at the end of the line with a full stop
        if (newsTickerLinks && newsTickerLinks.length > 0) {
            var tmpVal = newsTickerLinks[0].innerHTML;
            newsTickerLinks[0].innerHTML=tmpVal.substring(0,tmpVal.length-1) + ".";
        } else {
            this.newsTicker.innerHTML=this.newsTicker.innerHTML.substring(0,this.newsTicker.innerHTML.length-1) + ".";
        }

        //Restart the ticker after 3 seconds to allow the user to view the message
        var This=this;
        this.timer.setTimeout("doTicker",3000);

    } else {

        //If this is a new news item, clear the ticker
        if (this.linePos==0) {
            if (this.newsLinks[this.newsItem]!='') {
                this.newsTicker.innerHTML='<a href="' + this.newsLinks[this.newsItem] + '"> </a>';
            } else {
                this.newsTicker.innerHTML='';
            }
        }

        var newsTickerLinks = this.newsTicker.getElementsByTagName('a');

        //Display the current part of the news item, incrementing the text position by 1
        if (newsTickerLinks && newsTickerLinks.length > 0) {
            var tmpVal = new String(newsTickerLinks[0].innerHTML);

            newsTickerLinks[0].innerHTML =
                this.newsItems[this.newsItem].substring(0,++this.linePos) +
                    ((tmpVal.substring(tmpVal.length-1,tmpVal.length)=='-')?'_':'-');

        } else {
            this.newsTicker.innerHTML=
                this.newsItems[this.newsItem].substring(0,++this.linePos) +
                    ((this.newsTicker.innerHTML.substring(this.newsTicker.innerHTML.length-1,this.newsTicker.innerHTML.length)=='-')?'_':'-');
        }

        //Continue the ticker after 50ms
        var This=this;
        this.timer.setTimeout("doTicker",50);
    }
}
