// -------------------------------------------------------------------
// Advanced RSS Ticker (Ajax invocation) core file
// Author: Dynamic Drive (http://www.dynamicdrive.com)
// -------------------------------------------------------------------

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
}

cachetime=0                                       //Time to cache feed, in minutes. 0=no cache.
tickerid='rssbox';                                //ID of ticker div to display information
delay=0                                           //Delay between msg change, in miliseconds.
mouseoverBol=0                                    //Boolean to indicate whether mouse is currently over ticker
ajaxIsBusy=false;

   //Arrays to hold each component of an RSS item
var title=[], artist=[], version=[],link=[], description=[], pubdate=[], status=[], content=[];

   // XMLHttpRequestObj
ajaxobj=createAjaxObj()

// -------------------------------------------------------------------
// getAjaxcontent()- Makes asynchronous GET request to "nowplaying.rss" with the supplied parameters
// -------------------------------------------------------------------
function getAjaxcontent(){
   if(ajaxIsBusy)return;
   if (ajaxobj){
      //if(ajaxIsBusy){
      //   ajaxobj.onreadystatechange = function () {}
	// ajaxobj.abort();
      //}
      ajaxobj.open('GET', "http://nowplaying.djmonitor.com/dtradio/rss.php?station_id=1&bustcache="+new Date().getTime(), true);
      //ajaxobj.open('GET', "http://artemis.djmonitor.com/rssnew/rss.php?station_id=1", true);
      ajaxIsBusy=true;
      ajaxobj.onreadystatechange=function(){updateTicker()}
      ajaxobj.send(null);
   }

}

// -------------------------------------------------------------------
// -Gets contents of RSS content and parse it using JavaScript DOM methods 
// -------------------------------------------------------------------
function updateTicker(){ 

   if (ajaxobj.readyState != 4) return;
   ajaxIsBusy=false;

   if (ajaxobj.status==200){ //if request was successful

      var xmldata=ajaxobj.responseXML

      if(xmldata.getElementsByTagName("item").length==0){ //if no <item> elements found in returned content

            document.getElementById(tickerid).innerHTML="<b>Error</b> fetching remote RSS feed..<br />"
            //document.getElementById(tickerid).innerHTML=ajaxobj.responseText

            return;
      }

      feeditems=xmldata.getElementsByTagName("item");

         //Cycle through RSS XML object and store each peice of an item inside a corresponding array

      for (var i=0; i<feeditems.length; i++){

             if(feeditems[i].getElementsByTagName("title")[0].firstChild){
                title[i]=feeditems[i].getElementsByTagName("title")[0].firstChild.nodeValue
             } else {
                title[i]='none';
             }
             if(feeditems[i].getElementsByTagName("artist")[0].firstChild){
                artist[i]=feeditems[i].getElementsByTagName("artist")[0].firstChild.nodeValue
             } else {
                artist[i]='none';
             }
             if(feeditems[i].getElementsByTagName("version")[0].firstChild){
                version[i]=feeditems[i].getElementsByTagName("version")[0].firstChild.nodeValue
             } else {
                version[i]='none';
             }
             if(feeditems[i].getElementsByTagName("status")[0].firstChild){
                status[i]=feeditems[i].getElementsByTagName("status")[0].firstChild.nodeValue
             } else {
                status[i]='none';
             }
             //alert(status[i]);
             //if(feeditems[i].getElementsByTagName("content")[0].firstChild){
             //   content[i]=feeditems[i].getElementsByTagName("content")[0].firstChild.nodeValue
             //} else {
             //   content[i]='none';
             // }

             content[i]='';
             if(status=='Now playing:'){
             
                content[i]=artist[i] +" - "+ title[i] + version[i] + '<br/>';

                link='http://main2.dance-tunes.com/index.php?pagina=zoek&amp;zoekwoord='+title[i];

                content[i]+='<a href="'+link+'" target="_new">order this track from Dance Tunes</a>';
             }
             if(feeditems[i].getElementsByTagName("description")[0].firstChild){
                description[i]=feeditems[i].getElementsByTagName("description")[0].firstChild.nodeValue
             } else {
                description[i]='none';
             }
             if(feeditems[i].getElementsByTagName("time")[0].firstChild){
                pubdate[i]=feeditems[i].getElementsByTagName("time")[0].firstChild.nodeValue
             } else {
                pubdate[i]='none';
             }
         }

         document.getElementById(tickerid).onmouseover=function(){mouseoverBol=1}
         document.getElementById(tickerid).onmouseout=function(){mouseoverBol=0}

         rotatemsg(0)
   }
}

// -------------------------------------------------------------------
// rotatemsg()- Rotate through RSS messages and displays them
// -------------------------------------------------------------------
function rotatemsg(pointer){

   //if (mouseoverBol==1) //if mouse is currently over ticker, do nothing (pause it)
      //setTimeout(function(){instanceOfTicker.rotatemsg()}, 100)
   
   //else{ //else, construct item, show and rotate it!

      description="";
      var tickerDiv=document.getElementById(tickerid)

      var thestatus='<div class="rssstatus">'+status[pointer]+'</div>'
      var thecontent='<div class="rsscontent">'+content[pointer]+'</div>'
      var thefeeddate='<div class="rssdate">'+pubdate[pointer]+'</div>'
      var thefooter='<div class="rssfooter"><hr/>Audio Stream Monitoring by <a href="http://www.djmonitor.com" target="_new">DJMonitor</a><br/>&copy; 2008 Dance-Tunes & DJMonitor. All rights reserved.</div>';

      var tickercontent=thestatus+"\n"+thecontent+"\n"+thefeeddate+"\n"+thefooter //STRING FOR FEED CONTENTS 
      tickerDiv.innerHTML=tickercontent
      //tickerDiv.innerHTML+=tickercontent
      pointer=(pointer<feeditems.length-1)? pointer+1 : 0

      //setTimeout(function(){rotatemsg(pointer)}, delay) //update container every second
   //}
}


