//
// MMAFeed
// -------
//   This class represents function and display of a feed
//
// Many Thanks to:
//   http://weblogs.macromedia.com/mesh/archives/2006/01/encapsulating_a.cfm
//

function MMAFeed (in_url, in_port_id, in_rrate, in_name) {
  this.init (in_url, in_port_id, in_rrate, in_name);
}

//
// Methods
//

MMAFeed.prototype.url = "";
MMAFeed.prototype.port = null;
MMAFeed.prototype.port_id = null;
MMAFeed.prototype.port_head = null;
MMAFeed.prototype.port_toggle = null;
MMAFeed.prototype.port_prog = null;
MMAFeed.prototype.port_flash = null;
MMAFeed.prototype.request = null;
MMAFeed.prototype.refresh_rate = 180000;
MMAFeed.prototype.timer = null;
MMAFeed.prototype.my_name = "";
MMAFeed.prototype.in_refresh = 0;
MMAFeed.prototype.is_collapsed = 0;
MMAFeed.prototype.css_rules = null;

MMAFeed.prototype.init = function (in_url, in_port_id, in_rrate, in_name) {
  var theRules = new Array();

  //
  // Class variables
  //
  this.my_name = in_name

  if (document.styleSheets[0].cssRules)
	  this.css_rules = document.styleSheets[0].cssRules;
  else if (document.styleSheets[0].rules)
	  this.css_rules = document.styleSheets[0].rules;

  if (in_url.indexOf('http') > -1) {
    // this.url = '/~maheshak/cgi-bin/get_feed.cgi?' + in_url;
    this.url = '/cgi-bin/get_feed.pl?' + in_url;
  } else {
    this.url = in_url;
  }

  this.refresh_rate = in_rrate * 1000;

  this.port_id = in_port_id;
  this.port = document.getElementById(in_port_id);
  this.port_head = document.getElementById(in_port_id + "_head");
  this.port_toggle = document.getElementById(in_port_id + "_toggle");
  this.port_prog = document.getElementById(in_port_id + "_prog");
  this.port_flash = document.getElementById(in_port_id + "_flash");

	//
	// First update
	//
	this.update();
}

MMAFeed.prototype.update_status = function () {

  if (this.request.readyState == 1) {
    //this.port.innerHTML = "Loading...";
  }
  else if (this.request.readyState == 2) {
    //this.port.innerHTML = "Loaded...";
  }
  else if (this.request.readyState == 3) {
    //this.port.innerHTML = "Interactive...";
  }
  else if (this.request.readyState == 4) {
    if (this.request.status == 200) {
      var xml_doc = this.request.responseXML;
      this.process_data (xml_doc);
    } else {
      this.port.innerHTML = "Request done<ul><li>status = " + this.request.status + " : " + this.request.statusText + "</li><li>URL : " + this.url + "</li></ul>";
    }
    delete this.request;
  } else {
    this.port.innerHTML = "There was a problem with the request (" + this.request.status + ")";
  }
}

MMAFeed.prototype.update = function () {
  var caller = this;

  window.clearTimeout (this.timer);

  this.in_refresh = 1;
  this.port_prog.src = "throb.gif";
  if (this.is_collapsed == 0) this.port_flash.style.display = "block";

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

  if (!this.request) {
    alert('Sorry.. Cannot create an XMLHTTP instance');
    return false;
  }

  this.request.onreadystatechange = function () {
    caller.update_status()
  };

  this.request.open('GET', this.url, true);

  try {
    this.request.send( null );
  } catch (e) {
    this.port.innerHTML = "Send error... " + e;
  }

}

MMAFeed.prototype.process_data = function (xml_doc) {
  //
  // Get basic info
  //
  var desc_len = 70;
  var title = xml_doc.getElementsByTagName('title').item(0).firstChild.data;
  var link = xml_doc.getElementsByTagName('link').item(0).firstChild.data;
  var full_desc = xml_doc.getElementsByTagName('description').item(0).firstChild.data;
  var desc = full_desc;
  if (desc.length > desc_len) desc = desc.substring(0, desc_len-4) + "...";

  this.port_head.innerHTML  = "<a href=\"" + link + "\" title=\"" + title + "\" target=\"feedr_win\"><b>" + title + "</b></a>";
  this.port_head.innerHTML += "<br><small title=\"" + full_desc + "\">" + desc + "</small>";
  //this.port.innerHTML = "<ul>";
  this.port.innerHTML = "";

  //
  // Traverse Items
  //
  var items = xml_doc.getElementsByTagName('item');

  for ( it = 0; it < items.length; it++) {
    var it_title = items[it].getElementsByTagName('title').item(0).firstChild.data;
    var it_link  = items[it].getElementsByTagName('link').item(0).firstChild.data;
    var it_crtr  = ""; // (items[it].getElementsByTagName('creator')) ? items[it].getElementsByTagName('creator').item(0).firstChild.data : "";
    var it_cmts  = ""; // items[it].getElementsByTagName('comments').item(0).firstChild.data;
    var it_li_class = (it % 2) ? 'li_even' : 'li_odd';

    this.port.innerHTML += "<li class=" + it_li_class + "><a href=\"" + it_link + "\" target=\"feedr_win\">" + it_title + "</a>";
    //  - " + it_crtr + "(<a href=\"" + it_cmts + "\" title=\"Comment\" >#</a>)</li>";
  }
  //this.port.innerHTML += "</ul>";

  this.port_prog.src = "no-throb.png";

  this.timer = window.setTimeout (this.my_name + '.update()', this.refresh_rate);
  this.port_flash.style.display = "none";
  this.in_refresh = 0;
}

MMAFeed.prototype.toggle_minimize = function () {
  if (this.port.style.display == 'none') {
    this.port_toggle.src = "close.gif";
    this.port_toggle.title = "Collapse";
    this.port.style.display = 'block';
    if (this.in_refresh == 1) this.port_flash.style.display = 'block';
    this.is_collapsed = 0;
  } else {
    this.port_toggle.src = "open.gif";
    this.port_toggle.title = "Expand";
    this.port.style.display = 'none';
    this.port_flash.style.display = 'none';
    this.is_collapsed = 1;
  }
}

