// SCCSID @(#)/nfs/h2/jiscltp/web/siteman/sites/proof/docroot/javascript/SCCS/s.rss_ticker.js 1.2

function rss_ticker(rss_channel, element_id, format) {
  this.channel = rss_channel;
  this.elementid = element_id; // element id of container to display rss feed items
  this.rotatedelay = 4000;
  this.format = typeof(format) != 'undefined' ? format : 'rotate';
  this.ismouseover = 0;
  this.el = 0;

  // arrays to hold rss feed items
  this.title = [];
  this.link = [];
  this.description = [];

  if(this.format != 'rotate' && this.format != 'list') {
    document.getElementById(this.elementid).innerHTML = "Unsupported RSS format specified.";
    return null;
  }

  this.RequestObj = rss_makeRequest();
  if(this.RequestObj) {
    var ithis = this;
    this.RequestObj.onreadystatechange = function() { ithis.GetXMLdata(); };
    this.RequestObj.open("GET", this.channel, true);
    this.RequestObj.send(null);
  }
}

function rss_makeRequest() {
  var http_request = null;

  if(window.XMLHttpRequest) { // Mozilla, Safari,...
    http_request = new XMLHttpRequest();
    if(http_request.overrideMimeType) {
      http_request.overrideMimeType('text/xml');
    }
  }
  else if(window.ActiveXObject) { // IE
    try {
      http_request = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(err) {
      try {
        http_request = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch(err) {}
    }
  }

  if (!http_request) {
    alert('Your browser does not support ths feature.');
    return null;
  }

  return http_request;
}

rss_ticker.prototype.GetXMLdata = function() {
  if((this.RequestObj.readyState == 4) && (this.RequestObj.status == 200)) {
    var XMLdata = this.RequestObj.responseXML;
    if(XMLdata.getElementsByTagName("item").length == 0) {
      document.getElementById(this.elementid).innerHTML = "RSS feed is temporarily unavailable.";
      return 1;
    }
  }

  if(XMLdata != null && XMLdata.getElementsByTagName("item").length) {
    var ithis = this;
    this.noofitems = XMLdata.getElementsByTagName("item");

    for(var i = 0; i < this.noofitems.length; i++) {
      this.title[i] = this.GetValue(this.noofitems[i], 'title');
      this.link[i] = this.GetValue(this.noofitems[i], 'link');
      this.description[i] = this.GetValue(this.noofitems[i], 'description');
    }

    document.getElementById(this.elementid).onmouseover = function() { ithis.ismouseover = 1; }
    document.getElementById(this.elementid).onmouseout = function() { ithis.ismouseover = 0; }

    switch(this.format) {
      case 'rotate':
        this.RotateFeedItem();
        break;
      case 'list':
        this.DisplayListFormat();
        break;
    }
  }
}

rss_ticker.prototype.RotateFeedItem = function() {
  var ithis = this;

  if(this.ismouseover == 1) { // pause ticker if mouse is over
    setTimeout(function() { ithis.RotateFeedItem() }, this.rotatedelay);
  }
  else {
    var title='<div class="rss_title"><b><a href="'+this.link[this.el]+'">'+this.title[this.el]+'</a></b></div>'
    var description='<div class="rss_body">'+this.description[this.el]+'</div>'
    document.getElementById(this.elementid).innerHTML = title + description;
    this.el = (this.el < this.noofitems.length-1) ? this.el+1 : 0;
    setTimeout(function() { ithis.RotateFeedItem() }, this.rotatedelay);
  }
}

rss_ticker.prototype.DisplayListFormat = function() {
  var title = '';
  for(var i = 0; i < this.noofitems.length; i++) {
    title += '<div class="rss_title"><b><a href="'+this.link[i]+'">'+this.title[i]+'</a></b></div>'
  }
  document.getElementById(this.elementid).innerHTML = title;
}

rss_ticker.prototype.GetValue = function(node, element_name) {
  try {
    var s = node.getElementsByTagName(element_name)[0].firstChild.nodeValue;
  }
  catch (err) {
    var s = '';
  }
  return s
}
