//Relative URL syntax:
var lastrssbridgeurl="/js/lastrss/bridge.php"

//Absolute URL syntax. Uncomment below line if you wish to use an absolute reference:
var rotator_url="http://"+window.location.hostname+"/lib/banners/banner.php"

////////////No need to edit beyond here//////////////

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
}

// -------------------------------------------------------------------
// Main RSS Ticker Object function
// banner_ajax(RSS_id, cachetime, divId, divClass, delay, optionallogicswitch)
// -------------------------------------------------------------------

function banner_ajax(ban_group){
    this.ban_group=ban_group 
    this.tickerid='ban_'+ban_group //ID of ticker div to display information
    this.cachetime= 0 //cachetime //Time to cache feed, in minutes. 0=no cache.
    this.delay=10000 //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.link=[], this.description=[], this.pubdate=[] //Arrays to hold each component of an RSS item
    this.ajaxobj=createAjaxObj()
    document.write('<div id="ban_'+ban_group+'" class="ban_'+ban_group+'" >Initializing banner...</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
    this.getAjaxcontent()
}

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

banner_ajax.prototype.getAjaxcontent=function(){
    if (this.ajaxobj){
        var instanceOfTicker=this
        var parameters="ban_group="+this.ban_group+"&bustcache="+new Date().getTime()
        this.ajaxobj.onreadystatechange=function(){instanceOfTicker.initialize()}
        this.ajaxobj.open('GET', rotator_url+"?"+parameters, true)
        this.ajaxobj.send(null)
    }
}

// -------------------------------------------------------------------
// initialize()- Initialize ticker method.
// -Gets contents of RSS content and parse it using JavaScript DOM methods 
// -------------------------------------------------------------------

banner_ajax.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
            this.tickercontent=this.ajaxobj.responseText
            var instanceOfTicker=this
            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
// -------------------------------------------------------------------

banner_ajax.prototype.rotatemsg=function(){
    var instanceOfTicker=this
    if (this.mouseoverBol==1) { //if mouse is currently over ticker, do nothing (pause it)
        setTimeout(function(){instanceOfTicker.rotatemsg()}, this.delay)
   	} else { //else, construct item, show and rotate it!
        var tickerDiv=document.getElementById(this.tickerid)
        this.fadetransition("reset") //FADE EFFECT- RESET OPACITY
        tickerDiv.innerHTML=this.tickercontent
        this.fadetimer1=setInterval(function(){instanceOfTicker.fadetransition('up', 'fadetimer1')}, 100) //FADE EFFECT- PLAY IT        
        setTimeout(function(){instanceOfTicker.getAjaxcontent()}, this.delay) //update container every second
    }
}

// -------------------------------------------------------------------
// fadetransition()- cross browser fade method for IE5.5+ and Mozilla/Firefox
// -------------------------------------------------------------------

banner_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])
}
