/* Create a listing of an RSS feed NOTE: Only from same domain !!! */

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
}

function rss_listing(URL, divId, rssURL, siteURL, max){
  this.URL = URL;
  this.rssURL = rssURL;
  this.siteURL = siteURL;
  this.divId = divId;
  this.max = max && max > 0 ? max : 1000;
  this.ajaxobj=createAjaxObj()
  document.write('<div id="'+divId+'" class="snelnieuws_list"  style="position: relative;">Initializing RSS...</div>')
  this.getAjaxcontent()
}

rss_listing.prototype.getAjaxcontent=function(){
  if (this.ajaxobj){
    var instanceOfTicker=this
    this.ajaxobj.onreadystatechange=function(){instanceOfTicker.initialize()}
    this.ajaxobj.open('GET', this.URL, true)
    this.ajaxobj.send(null)
  }
}

rss_listing.prototype.initialize=function(){
  if (this.ajaxobj.readyState == 4 ) {
    var div = document.getElementById(this.divId);
    //alert(lu.innerHTML);
    var tmpdiv = document.createElement('div');

    if ( this.ajaxobj.status==200 ){ //if request of file completed && successful
      var xmldata=this.ajaxobj.responseXML
      if (xmldata.getElementsByTagName("item").length>0){ // Has data
        var instanceOfTicker=this
        this.feeditems=xmldata.getElementsByTagName("item")
        var listHtml = '<li style="position: relative;" class="kop"><a class="rss" title="rss-feed poker" href="'+this.rssURL+'" target="_blank" >rss</a><a href="'+this.siteURL+'">Nieuws</a></li>';

        //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++){
          if ( i >= this.max ) break;

          var title = this.feeditems[i].getElementsByTagName("title")[0].firstChild.nodeValue;
          var link  = this.feeditems[i].getElementsByTagName("link")[0].firstChild.nodeValue;
          var desc  = this.feeditems[i].getElementsByTagName("description")[0].firstChild.nodeValue;

          // Get image URL
          tmpdiv.innerHTML = desc;
          var imgurl = 'http://images2-telegraaf.nl/multimedia/empty.gif'; // Default

          try {
            imgurl = tmpdiv.getElementsByTagName("img")[0].getAttribute('src');
          }
          catch (e){}

          listHtml += '<li class="item ' + (i%2 ? 'A':'B') + ' imgitem" style="overflow: hidden; position: relative"><a href="'+link+'" target="_blank" class="snelnieuws_link" style="position: relative"><img src="'+imgurl+'" alt="" style="width: 35px !important; position: relative;">'+title+'</a></li>'; 
        }

        listHtml='<lu class="snelnieuws_list">'+listHtml+'</lu>';
        div.innerHTML=listHtml;
        return;
      }
      lu.innerHTML="<b>RSS fetching Error !</b><br />";
    }
  }
}


