//Relative URL syntax:
var lastrssbridgeurl="/include/lastrss/bridge.php"
//var lastrssbridgeurl="lastrss/bridge.php"


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
// rssticker_ajax(RSS_id, cachetime, divId, divClass, delay, optionallogicswitch)
// -------------------------------------------------------------------

function rssticker_ajax(RSS_id, cachetime, divId, divClass, delay, logicswitch, refresh){
this.RSS_id=RSS_id //Array key indicating which RSS feed to display
this.cachetime=cachetime //Time to cache feed, in minutes. 0=no cache.
this.bustcache=new Date().valueOf()+cachetime*60*1000 //Time where bust cache feed, in msec
this.tickerid=divId //ID of ticker div to display information
this.refresh=refresh
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.link=[], this.description=[], this.pubdate=[], this.source=[] //Arrays to hold each component of an RSS item
this.author=[], this.category=[], this.comments=[], this.guid=[] //Arrays to hold each component of an RSS item
this.orden=[]

this.ajaxobj=createAjaxObj()
document.write('<div id="'+divId+'" class="'+divClass+'" >Leyendo datos...</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
// -------------------------------------------------------------------

rssticker_ajax.prototype.getAjaxcontent=function(){
if (this.ajaxobj){
var instanceOfTicker=this
var parameters="id="+encodeURIComponent(this.RSS_id)+"&cachetime="+this.cachetime+"&bustcache="+this.bustcache
this.ajaxobj.onreadystatechange=function(){instanceOfTicker.initialize()}
this.ajaxobj.open('GET', lastrssbridgeurl+"?"+parameters, true)
this.ajaxobj.send(null)
}
}

// -------------------------------------------------------------------
// initialize()- Initialize ticker method.
// -Gets contents of RSS content and parse it using JavaScript DOM methods 
// -------------------------------------------------------------------

rssticker_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
    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 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
      this.link[i]=this.feeditems[i].getElementsByTagName("link")[0].firstChild.nodeValue
      this.description[i]=(this.feeditems[i].getElementsByTagName("description")[0].firstChild)?this.feeditems[i].getElementsByTagName("description")[0].firstChild.nodeValue:""
      this.pubdate[i]=(this.feeditems[i].getElementsByTagName("pubDate")[0])?this.feeditems[i].getElementsByTagName("pubDate")[0].firstChild.nodeValue:""
      this.source[i]=(this.feeditems[i].getElementsByTagName("source")[0])?this.feeditems[i].getElementsByTagName("source")[0].firstChild.nodeValue:""
      this.author[i]=(this.feeditems[i].getElementsByTagName("author")[0])?this.feeditems[i].getElementsByTagName("author")[0].firstChild.nodeValue:""
      this.category[i]=(this.feeditems[i].getElementsByTagName("category")[0])?this.feeditems[i].getElementsByTagName("category")[0].firstChild.nodeValue:""
      this.comments[i]=(this.feeditems[i].getElementsByTagName("comments")[0])?this.feeditems[i].getElementsByTagName("comments")[0].firstChild.nodeValue:""
      this.guid[i]=(this.feeditems[i].getElementsByTagName("guid")[0])?this.feeditems[i].getElementsByTagName("guid")[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
// -------------------------------------------------------------------

rssticker_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()}, 100)

else if( this.bustcache < new Date().valueOf() ) {
  //El cache paso a ser viejo y aun no refrescaron la pagina...entonces refresco el AJAX
  this.bustcache=new Date().valueOf()+this.cachetime*60*1000 //Time where bust cache feed, in msec  
  this.getAjaxcontent()
} 
else{ //else, construct item, show and rotate it!
  var tickerDiv=document.getElementById(this.tickerid)
  var title='<div class="rsstitle"><a href="'+this.link[this.pointer]+'">'+this.title[this.pointer]+'</a></div>'
  var description='<div class="rssdescription">'+this.description[this.pointer]+'</div>'
  var pubdate='<div class="rsspubdate">'+this.pubdate[this.pointer]+'</div>'
  var source='<div class="rsssource"><img src="'+this.source[this.pointer]+'"/></div>'
  var author='<div class="rssauthor"><img src="'+this.author[this.pointer]+'"/></div>'
  var category='<div class="rsscategory"><img src="'+this.category[this.pointer]+'"/></div>'
  var comments='<div class="rsscomments">'+this.comments[this.pointer]+'</div>'
  var guid='<div class="rssguid"><img src="'+this.guid[this.pointer]+'"/></div>'

  if (this.logicswitch.indexOf("title")==-1) title=""; else this.orden[this.logicswitch.indexOf("title")]=title;
  if (this.logicswitch.indexOf("description")==-1) description=""; else this.orden[this.logicswitch.indexOf("description")]=description;
  if (this.logicswitch.indexOf("pubdate")==-1) pubdate="" ; else this.orden[this.logicswitch.indexOf("pubdate")]=pubdate;
  if (this.logicswitch.indexOf("source")==-1) source="" ; else this.orden[this.logicswitch.indexOf("source")]=source;
  if (this.logicswitch.indexOf("author")==-1) author="" ; else this.orden[this.logicswitch.indexOf("author")]=author;
  if (this.logicswitch.indexOf("category")==-1) category="" ; else this.orden[this.logicswitch.indexOf("category")]=category;
  if (this.logicswitch.indexOf("comments")==-1) comments="" ; else this.orden[this.logicswitch.indexOf("comments")]=comments;
  if (this.logicswitch.indexOf("guid")==-1) guid=""; else this.orden[this.logicswitch.indexOf("guid")]=guid;

  var tickercontent="";
  for (i=0; i<this.logicswitch.length; i++) 
    if(this.orden[i]) 
      tickercontent+=this.orden[i];
 

  this.fadetransition("reset") //FADE EFFECT- RESET OPACITY
  tickerDiv.innerHTML=tickercontent
  this.fadetimer1=setInterval(function(){instanceOfTicker.fadetransition('up', 'fadetimer1')}, 100) //FADE EFFECT- PLAY IT

  //this.pointer=(this.pointer<this.feeditems.length-1)? this.pointer+1 : 0
  if(this.pointer<this.feeditems.length-1) {
    this.pointer+=1
    setTimeout(function(){instanceOfTicker.rotatemsg()}, this.delay) //update container every second
  } else { 
    this.pointer=0
    if( (typeof this.refresh!="undefined") ) { 
      this.bustcache=new Date().valueOf()+this.cachetime*60*1000 //Time where bust cache feed, in msec  
      this.getAjaxcontent()
    } else {
      setTimeout(function(){instanceOfTicker.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])
}

