﻿// JScript File
var text;
var text1;
var delay=50;
var currentChar=1;
var destination;
var host;

// -------------------------------------------------------------------
// Main RSS Ticker Object function
// rss_ticker(RSS_id, cachetime, divId, divClass, delay, optionalswitch)
// -------------------------------------------------------------------

function rss_ticker(divId, cssclass, rss_feed)
{
    this.tickerid=divId //ID of ticker div to display information
    this.cssclass=cssclass
    this.rss_feed=rss_feed
    this.delay=8000 //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=createAjaxObj()
    this.getAjaxcontent()
}

function createAjaxObj()
{   
    var httprequest=false
    
    if (window.XMLHttpRequest)
    { // if Mozilla, Safari etc
        httprequest=new XMLHttpRequest()
        if (httprequest.overrideMimeType)
        httprequest.overrideMimeType('text/xml')
    }
    else if (window.ActiveXObject)// if IE
    { 
        try 
        {
            httprequest=new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch (e)
        {
            try
            {
                httprequest=new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e){}
        }
    }
    return httprequest
}

// -------------------------------------------------------------------
// getAjaxcontent()- Makes asynchronous GET request to "rssfetch.php" with the supplied parameters
// -------------------------------------------------------------------
var hostpath
rss_ticker.prototype.getAjaxcontent=function()
{
    if (this.ajaxobj)
    {
        var instanceOfTicker=this
        this.ajaxobj.onreadystatechange=function(){instanceOfTicker.initialize()}
        hostpath = this.rss_feed;
        this.ajaxobj.open('GET', hostpath , true) 
        this.ajaxobj.send(null)
    }
}

// -------------------------------------------------------------------
// initialize()- Initialize ticker method.
// -Gets contents of RSS content and parse it using JavaScript DOM methods 
// -------------------------------------------------------------------

rss_ticker.prototype.initialize=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)
            }	
            
            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 tickerDiv=document.getElementById(this.tickerid)

        var tickercontent=this.feeditems[this.pointer].getAttribute("ctitle")
        tickercontent = tickercontent.replace('&#8217;','\'');
        var clink=this.feeditems[this.pointer].getAttribute("clink")
        var currentChar

        startTyping(clink, tickercontent, 50, tickerDiv);

        this.pointer=(this.pointer<this.feeditems.length-1)? this.pointer+1 : 0
        setTimeout(function(){instanceOfTicker.rotatemsg()}, this.delay) //update container every second
    }
}


function type()
{ 
      currentChar++
	  
  	  if (currentChar<text.length)
      {
	  	destination.innerHTML = "<a href=\""+text1+"\">"+text.substr(0, currentChar)+"</a>"
        setTimeout("type()", delay);
      } 
      else if (currentChar=text.length) 
      {
	  	destination.innerHTML = "<a href=\""+text1+"\">"+text.substr(0, currentChar)+"</a>"
	  } 
}

function startTyping(textParam1, textParam, delayParam, destinationParam)
{
      text=textParam;
      text1=textParam1;
      delay=delayParam;
      currentChar=1;
      destination=destinationParam;
      type();
}
