/* Code copied from http://www.javascriptkit.com/dhtmltutors/ajaxticker/ */

function createAjaxObj(){
  var httprequest=false
  if (window.XMLHttpRequest)
  { // if Mozilla, Safari, IE7, etc
    httprequest=new XMLHttpRequest()
    if (httprequest.overrideMimeType)
    {
      httprequest.overrideMimeType('text/xml')
    }
  }
  else if (window.ActiveXObject){ // if IE-borked
    try {
      httprequest=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e){
      try{
        httprequest=new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e){}
    }
  }
  return httprequest
}

// -------------------------------------------------------------------
// Main RSS Ticker Object function
// rss_ticker(RSS_id, cachetime, divId, divClass, delay, optionalswitch)
// -------------------------------------------------------------------

function rss_ticker(RSS_id, cachetime, divId, divClass, delay, optionalswitch){
  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.tickerid=divId //ID of ticker div to display information
  this.delay=delay //Delay between msg change, in miliseconds.
  this.logicswitch=(typeof optionalswitch!="undefined")? optionalswitch : -1
  this.mouseoverBol=0 //Boolean to indicate whether mouse is currently over ticker (and pause it if it is)
  this.pointer=0
  this.type_speed = 50; // delay between characters
  this.ajaxobj=createAjaxObj()
  this.clicked = 0; // boolean to indicate if the user clicked in the ticker; <a href> doesn't work while the script is running
  document.write('<div id="'+divId+'" class="'+divClass+'">Initializing ticker...</div>')
  this.getAjaxcontent()
}

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

rss_ticker.prototype.getAjaxcontent=function(){
  if (this.ajaxobj){
    var instanceOfTicker=this
    //var parameters="id="+encodeURIComponent(this.RSS_id)+"&cachetime="+this.cachetime+"&bustcache="+new Date().getTime()
    var parameters = "feed="+ this.RSS_id;

    this.ajaxobj.open('GET', "rssfeeds.php?"+parameters, true)
    this.ajaxobj.onreadystatechange=function(){
      instanceOfTicker.initialize()
      }
    this.ajaxobj.send()
  }
}

// -------------------------------------------------------------------
// 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 && 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="Helaas, de ticker tape is momenteel niet beschikbaar..."; /*+this.ajaxobj.responseText*/
        document.getElementById(this.tickerid).innerHTML=""; /* No items, no text */
        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)
        this.feeditems[i].setAttribute("cauthor", this.feeditems[i].getElementsByTagName("author")[0].firstChild.nodeValue)
      }
      document.getElementById(this.tickerid).onmouseover=function(){
        instanceOfTicker.mouseoverBol=1
        }
      document.getElementById(this.tickerid).onmouseout=function(){
        instanceOfTicker.mouseoverBol=0
        }
      document.getElementById(this.tickerid).onclick=function(){
        instanceOfTicker.clicked=1;
        }

      this.rotatemsg()
  }
  else
  {
    document.getElementById(this.tickerid).innerHTML="Helaas, de ticker tape is momenteel niet beschikbaar..."; /*+this.ajaxobj.responseText*/
  }
}

// -------------------------------------------------------------------
// 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{
    this.link_pre = '<a href="'+this.feeditems[this.pointer].getAttribute("clink")+'">';
    this.link_post = '</a>';
    this.link = this.feeditems[this.pointer].getAttribute("clink");
    var tickercontent=this.feeditems[this.pointer].getAttribute("ctitle")
    if (this.logicswitch=="showdescription")
      tickercontent+="<br />"+this.feeditems[this.pointer].getAttribute("cdescription")
    if (this.logicswitch=="oneliner")
      tickercontent+=" - "+this.feeditems[this.pointer].getAttribute("cdescription")
    // The 130 character limit has been determined empirically...
    max_text_len = 130 - this.feeditems[this.pointer].getAttribute("cauthor").length;
    if (tickercontent.length > max_text_len)
    {
      tickercontent = tickercontent.substr (0, max_text_len) + "...";
    }
    tickercontent += " (";
    tickercontent += this.feeditems[this.pointer].getAttribute("cauthor");
    tickercontent += ")";
    this.ticker_text = tickercontent;
    this.ticker_text_len = tickercontent.length;
    this.ticker_text_pos = 0;

    tickerDiv=document.getElementById(this.tickerid)
    //tickerDiv.onClick = function() {}

    setTimeout (function() {
      instanceOfTicker.addChar() }, this.type_speed);
  }
}

rss_ticker.prototype.addChar=function()
{
  var instanceOfTicker=this
  tickerDiv=document.getElementById(this.tickerid)

  if (this.clicked==1)
  {
    // go to url
    document.location.href = this.link;
    //alert("clicked!");
    return;
  }
  if (this.ticker_text_pos >= this.ticker_text_len)
  {
    // next line
    this.pointer=(this.pointer<this.feeditems.length-1)? this.pointer+1 : 0
    setTimeout(function(){
      instanceOfTicker.rotatemsg()
      }, this.delay) //update container every second
  }
  else
  {
    // add character to text
    this.ticker_text_pos++;
    if ('&' == this.ticker_text.charAt (this.ticker_text_pos - 1))
    {
      // seek to end of special characters
      while ((this.ticker_text_pos < this.ticker_text_len) && ';' != this.ticker_text.charAt (this.ticker_text_pos - 1))
      {
        this.ticker_text_pos++;
      }
    }
    //tickerDiv.innerHTML = this.link_pre + this.ticker_text.substr(0, this.ticker_text_pos) + this.link_post;
    tickerDiv.innerHTML = this.ticker_text.substr(0, this.ticker_text_pos);
    //tickerDiv.nodeValue = this.link_pre + this.ticker_text.substr(0, this.ticker_text_pos) + this.link_post;
    setTimeout (function() {
      instanceOfTicker.addChar() }, this.type_speed);
  }
}
