
//OBJECTS

//object containing the RSS 2.0 item
function onnRSS2Item(itemxml)
{
  //required
  this.title;
  this.link;
  this.description;

  //optional vars
  this.author;
  this.comments;
  this.pubDate;

  var properties = new Array("title", "link", "description", "pubDate");
//  var properties = new Array("title");
  var tmpElement = null;
  for (var i=0; i<properties.length; i++)
  {
    tmpElement = itemxml.getElementsByTagName(properties[i])[0];
    if (tmpElement != null) {
      if (tmpElement.childNodes[0] != null) {
        eval("this."+properties[i]+"=tmpElement.childNodes[0].nodeValue");
      }
    }
  }
}

//objects inside the onnRSS2Channel object
function onnRSS2Category(catElement)
{
  if (catElement == null)
  {
    this.domain = null;
    this.value = null;
  }
  else
  {
    this.domain = catElement.getAttribute("domain");
    this.value = catElement.childNodes[0].nodeValue;
  }
}

//object containing RSS image tag info
function onnRSS2Image(imgElement)
{
  if (imgElement == null)
  {
  this.url = null;
  this.link = null;
  this.width = null;
  this.height = null;
  this.description = null;
  }
  else
  {
    imgAttribs = new Array("url","title","link","width","height","description");
    for (var i=0; i<imgAttribs.length; i++)
      if (imgElement.getAttribute(imgAttribs[i]) != null)
        eval("this."+imgAttribs[i]+"=imgElement.getAttribute("+imgAttribs[i]+")");
  }
}

//object containing the parsed RSS 2.0 channel
function onnRSS2Channel(rssxml)
{
  //required
  this.title;
  this.link;
  this.description;

  //array of onnRSS2Item objects
  this.items = new Array();

  //optional vars
  this.language;
  this.copyright;
  this.managingEditor;
  this.webMaster;
  this.pubDate;
  this.lastBuildDate;
  this.generator;
  this.docs;
  this.ttl;
  this.rating;

  //optional objects
  this.category;
  this.image;

  var chanElement = rssxml.getElementsByTagName("channel")[0];
  var itemElements = rssxml.getElementsByTagName("item");

  for (var i=0; i<itemElements.length; i++)
  {
    Item = new onnRSS2Item(itemElements[i]);
    this.items.push(Item);
    //chanElement.removeChild(itemElements[i]);
  }

  var properties = new Array("title", "link", "description", "language", "copyright", "managingEditor", "webMaster", "pubDate", "lastBuildDate", "generator", "docs", "ttl", "rating");
  var tmpElement = null;
  for (var i=0; i<properties.length; i++)
  {
    tmpElement = chanElement.getElementsByTagName(properties[i])[0];
    if (tmpElement!= null) {
      if (tmpElement.childNodes[0] != null) {
        eval("this."+properties[i]+"=tmpElement.childNodes[0].nodeValue");
      }
    }
  }

  this.category = new onnRSS2Category(chanElement.getElementsByTagName("category")[0]);
  this.image = new onnRSS2Image(chanElement.getElementsByTagName("image")[0]);
}

//PROCESSES

//uses xmlhttpreq to get the raw rss xml
function onnRSS(rssurl)
{
  this.channelIndex = 0;
  onnXhr;
  //shows the RSS content in the browser
  this.showNext = function ()
  {
    if (onnXhr) {
      if (!isNaN(onnXhr.channelItems.items.length)) {
        if (this.channelIndex >= onnXhr.channelItems.items.length)
          this.channelIndex = 0; 
        // alert(this.channelIndex++);
        document.getElementById('onn-stat').innerHTML = onnXhr.channelItems.items[this.channelIndex++].title;
      }
    }
    //we're done
    return true;
  }
  //call the right constructor for the browser being used
  this.Init = function(rssurl)
  {
    if (window.ActiveXObject)
      onnXhr = new ActiveXObject("Microsoft.XMLHTTP");
    else if (window.XMLHttpRequest)
      onnXhr = new XMLHttpRequest();
    else
      alert("not supported");
  
    //prepare the xmlhttprequest object
    onnXhr.open("GET",rssurl,true);
    onnXhr.setRequestHeader("Cache-Control", "no-cache");
    onnXhr.setRequestHeader("Pragma", "no-cache");
    onnXhr.onreadystatechange = function() {
      if (onnXhr.readyState == 4)
      {
        if (onnXhr.status == 200)
        {
          if (onnXhr.responseText != null) {
            onnXhr.channelItems = new onnRSS2Channel(onnXhr.responseXML);
          }
          else
          {
            alert("Failed to receive RSS file from the server - file not found.");
            return false;
          }
        }
        else
          alert("Error code " + onnXhr.status + " received: " + onnXhr.statusText);
      }
    }
  
    //send the request
    onnXhr.send(null);
    return true;
  }
}

var onnXhr;
var onnStat = new onnRSS();
onnStat.Init('/stat');
window.onload = cycleStart;
function cycleStart() {
  setTimeout('cycleStat()', 5*1000);
}

function cycleStat() {
  onnStat.showNext();
  setTimeout('cycleStat()', 10*1000);
}
