
/**
   Creates XMLHttpRequest object, and assigns it to global variable http_request

   @return XMLHttpRequest object or false on failure
*/
function getAjaxObject()
{
    if (window.XMLHttpRequest) // opera, firefox, mozilla
        {
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType)
                http_request.overrideMimeType('text/xml');
        }
    else if (window.ActiveXObject) // internet explodes
        {
            try
                {
                    http_request = new ActiveXObject("Msxml2.XMLHTTP");
                }
            catch (e)
                {
                    try
                        {
                            http_request = new ActiveXObject("Microsoft.XMLHTTP");
                        }
                    catch (e)
                        {
                            return false;
                        }
                }
        }
    if (!http_request)
        {
            return false;
        }
    return http_request;
}



function rss_ticker(url, divId, delay){
    this.url=url; //Array key indicating which RSS feed to display
    this.tickerid=divId; //ID of ticker div to display information
    this.delay=delay; //Delay between msg change, in miliseconds.
    this.mouseoverBol=0; //Boolean to indicate whether mouse is currently over ticker (and pause it if it is)
    this.pointer=0;
    this.ajaxobj=getAjaxObject();
    this.getRss();
}


// -------------------------------------------------------------------
// getAjaxcontent()- Makes asynchronous GET request to "rssfetch.php" with the supplied parameters
// -------------------------------------------------------------------

rss_ticker.prototype.getRss=function(){
    if (this.ajaxobj){
        var instanceOfTicker=this;
        this.ajaxobj.onreadystatechange=function(){instanceOfTicker.show()};
        this.ajaxobj.open('GET', this.url, true);
        this.ajaxobj.send(null);
    }
}

// -------------------------------------------------------------------
// initialize()- Initialize ticker method.
// -Gets contents of RSS content and parse it using JavaScript DOM methods
// -------------------------------------------------------------------
rss_ticker.prototype.show=function(){
    if (this.ajaxobj.readyState == 4){ //if request of file completed
        if (this.ajaxobj.status==200){ //if request was successful

            var xmldata=this.ajaxobj.responseXML;
            if (xmldata.getElementsByTagName("item").length==0){ //if no <item> elements found in returned content
                document.getElementById(this.tickerid).innerHTML="<b>Error</b> fetching remote RSS feed!<br />"+this.ajaxobj.responseText;
                return;
            }
            var instanceOfTicker=this;
            this.feeditems=xmldata.getElementsByTagName("item");

            //Cycle through RSS XML object and store each piece of the item element as an attribute of the element
            for (var i=0; i<this.feeditems.length; i++){
                this.feeditems[i].setAttribute("ctitle", this.feeditems[i].getElementsByTagName("title")[0].firstChild.nodeValue);
                this.feeditems[i].setAttribute("clink", this.feeditems[i].getElementsByTagName("link")[0].firstChild.nodeValue);
                this.feeditems[i].setAttribute("cdescription", this.feeditems[i].getElementsByTagName("description")[0].firstChild.nodeValue);
            }
            document.getElementById(this.tickerid).onmouseover=function(){instanceOfTicker.mouseoverBol=1};
            document.getElementById(this.tickerid).onmouseout=function(){instanceOfTicker.mouseoverBol=0};
            this.rotatemsg();
        }
    }
}



// -------------------------------------------------------------------
// rotatemsg()- Rotate through RSS messages and displays them
// -------------------------------------------------------------------

    rss_ticker.prototype.rotatemsg=function(){
        var instanceOfTicker=this;
        if (this.mouseoverBol==1) //if mouse is currently over ticker, do nothing (pause it)
            setTimeout(function(){instanceOfTicker.rotatemsg()}, 100);
        else{
            var tickerCount=document.getElementById("newscounter");
            nItems = this.feeditems.length;
            startcount = this.pointer + 1;
            var tickercontent = "<ul style=\"list-style-type: square;\">";
            for (i=0; i<3; i++) {
                var tickerDiv=document.getElementById(this.tickerid);
                tickercontent=tickercontent + '<li><a href="'+this.feeditems[this.pointer].getAttribute("clink")+'">'+this.feeditems[this.pointer].getAttribute("ctitle")+'</a></li>';
                this.pointer=(this.pointer<this.feeditems.length-1) ? this.pointer+1 : 0;
            }
            if (this.pointer == 0)
                endcount = this.feeditems.length;
            else
                endcount = this.pointer;
            tickerCount.innerHTML = startcount + " - " + endcount + " / " + nItems;
            tickerDiv.innerHTML=tickercontent+"</ul>";

            setTimeout(function(){instanceOfTicker.rotatemsg()}, this.delay); //update container every second
        }
    }











function rss_list(url, divId){
    this.url=url; //Array key indicating which RSS feed to display
    this.tickerid=divId; //ID of ticker div to display information
    this.mouseoverBol=0; //Boolean to indicate whether mouse is currently over ticker (and pause it if it is)
    this.pointer=0;
    this.ajaxobj=getAjaxObject();
    this.getRss();
}


// -------------------------------------------------------------------
// getAjaxcontent()- Makes asynchronous GET request to "rssfetch.php" with the supplied parameters
// -------------------------------------------------------------------

rss_list.prototype.getRss=function(){
    if (this.ajaxobj){
        var instanceOfTicker=this;
        this.ajaxobj.onreadystatechange=function(){instanceOfTicker.show()};
        this.ajaxobj.open('GET', this.url, true);
        this.ajaxobj.send(null);
    }
}

// -------------------------------------------------------------------
// initialize()- Initialize ticker method.
// -Gets contents of RSS content and parse it using JavaScript DOM methods
// -------------------------------------------------------------------
rss_list.prototype.show=function(){
    if (this.ajaxobj.readyState == 4){ //if request of file completed
        if (this.ajaxobj.status==200){ //if request was successful

            var xmldata=this.ajaxobj.responseXML;
            if (xmldata.getElementsByTagName("item").length==0){ //if no <item> elements found in returned content
                document.getElementById(this.tickerid).innerHTML="<b>Error</b> fetching remote RSS feed!<br />"+this.ajaxobj.responseText;
                return;
            }
            this.feeditems=xmldata.getElementsByTagName("item");
            var html = "<ul>";
            //Cycle through RSS XML object and store each piece of the item element as an attribute of the element
            for (var i=0; i<this.feeditems.length; i++){
                html = html + '<li>' + '<a href="' + this.feeditems[i].getElementsByTagName("link")[0].firstChild.nodeValue +
                    '">' + this.feeditems[i].getElementsByTagName("title")[0].firstChild.nodeValue +  '</li>';
                /*
                this.feeditems[i].setAttribute("ctitle", this.feeditems[i].getElementsByTagName("title")[0].firstChild.nodeValue);
                this.feeditems[i].setAttribute("clink", this.feeditems[i].getElementsByTagName("link")[0].firstChild.nodeValue);
                this.feeditems[i].setAttribute("cdescription", this.feeditems[i].getElementsByTagName("description")[0].firstChild.nodeValue);
                */
            }
            html = html + "</ul>";
            var tickerDiv=document.getElementById(this.tickerid);
            //            var tickercontent='<a href="'+this.feeditems[this.pointer].getAttribute("clink")+'">'+this.feeditems[this.pointer].getAttribute("ctitle")+'</a>';
            tickerDiv.innerHTML=html;


        }
    }
}



