﻿// -------------------------------------------------------------------
// Main RSS Ticker Object function
// rssticker_ajax(RSS_id, cachetime, divId, divClass, delay, optionallogicswitch)
// -------------------------------------------------------------------

function rssticker_ajax(cachetime, divId, divClass, delay, logicswitch){
this.cachetime=cachetime //Time to cache feed, in minutes. 0=no cache.
this.tickerid=divId //ID of ticker div to display information
this.delay=delay //Delay between msg change, in miliseconds.
this.logicswitch=(typeof logicswitch!="undefined")? logicswitch : ""
this.mouseoverBol=0 //Boolean to indicate whether mouse is currently over ticker (and pause it if it is)
this.pointer=0
this.opacitysetting=0.2 //Opacity value when reset. Internal use.
this.title=[],this.description=[]//Arrays to hold each component of an RSS item
document.write('<div id="'+divId+'" class="'+divClass+' row" >Initializing ticker...</div>')
if (window.getComputedStyle) //detect if moz-opacity is defined in external CSS for specified class
    this.mozopacityisdefined=(window.getComputedStyle(document.getElementById(this.tickerid), "").getPropertyValue("-moz-opacity")==1)? 0 : 1
 
 var instanceOfAnnouncementTicker=this;
 instanceOfAnnouncementTicker.initialize();
}


// -------------------------------------------------------------------
// initialize()- Initialize ticker method.
// -Gets contents of RSS content and parse it using JavaScript DOM methods 
// -------------------------------------------------------------------

rssticker_ajax.prototype.initialize=function(){ 
    var instanceOfAnnouncementTicker=this
    try //Internet Explorer
      {
      xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
      xmlDoc.async="false";
      xmlDoc.loadXML(announcementData);
      }
    catch(e)
      {
      try //Firefox, Mozilla, Opera, etc.
        {
        parser=new DOMParser();
        xmlDoc=parser.parseFromString(announcementData,"text/xml");
        }
      catch(e) {alert(e.message)}
      }
    
    this.feeditems=xmlDoc.getElementsByTagName("item"); //xmldata.getElementsByTagName("item")
    //Cycle through RSS XML object and store each peice of an item inside a corresponding array
    for (var i=0; i<this.feeditems.length; i++){
        this.title[i]=this.feeditems[i].getElementsByTagName("title")[0].firstChild.nodeValue
        try{
            this.description[i]=this.feeditems[i].getElementsByTagName("description")[0].firstChild.nodeValue
        }
        catch (e){
           this.description[i]='';
        }
    }
        document.getElementById(this.tickerid).onmouseover=function(){instanceOfAnnouncementTicker.mouseoverBol=1}
        document.getElementById(this.tickerid).onmouseout=function(){instanceOfAnnouncementTicker.mouseoverBol=0}
        this.rotatemsg()
  
}

// -------------------------------------------------------------------
// rotatemsg()- Rotate through RSS messages and displays them
// -------------------------------------------------------------------

rssticker_ajax.prototype.rotatemsg=function(){
    var instanceOfAnnouncementTicker=this
    if (this.mouseoverBol==1) //if mouse is currently over ticker, do nothing (pause it)
        setTimeout(function(){instanceOfAnnouncementTicker.rotatemsg()}, 100)
    else{ //else, construct item, show and rotate it!
        var tickerDiv=document.getElementById(this.tickerid)
        var shtml = "<div class='float padLeft'>"
        var linktitle='<div class="float bold">'+this.title[this.pointer]+': </div>';
        var description='<div class="float">&nbsp;'+this.description[this.pointer]+'</div>';
        var shtml2 = "</div>"
        
        var tickercontent=linktitle+description //STRING FOR FEED CONTENTS 
        this.fadetransition("reset") //FADE EFFECT- RESET OPACITY
        tickerDiv.innerHTML=tickercontent
        this.fadetimer2=setInterval(function(){instanceOfAnnouncementTicker.fadetransition('up', 'fadetimer2')}, 100) //FADE EFFECT- PLAY IT
        this.pointer=(this.pointer<this.feeditems.length-1)? this.pointer+1 : 0
        setTimeout(function(){instanceOfAnnouncementTicker.rotatemsg()}, this.delay) //update container every second
    }
}

// -------------------------------------------------------------------
// fadetransition()- cross browser fade method for IE5.5+ and Mozilla/Firefox
// -------------------------------------------------------------------

rssticker_ajax.prototype.fadetransition=function(fadetype, timerid){
var tickerDiv=document.getElementById(this.tickerid)
if (fadetype=="reset")
this.opacitysetting=0.2
if (tickerDiv.filters && tickerDiv.filters[0]){
if (typeof tickerDiv.filters[0].opacity=="number") //IE6+
tickerDiv.filters[0].opacity=this.opacitysetting*100
else //IE 5.5
tickerDiv.style.filter="alpha(opacity="+this.opacitysetting*100+")"
}
else if (typeof tickerDiv.style.MozOpacity!="undefined" && this.mozopacityisdefined){
tickerDiv.style.MozOpacity=this.opacitysetting
}
if (fadetype=="up")
this.opacitysetting+=0.2
if (fadetype=="up" && this.opacitysetting>=1)
clearInterval(this[timerid])
}