//
// 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_port_id, in_rrate, in_name) {
  this.init (in_port_id, in_rrate, in_name);
}

//
// Methods
//

MMAFeed.prototype.url = "";
MMAFeed.prototype.deals_fetcher = "";
MMAFeed.prototype.port = null;
MMAFeed.prototype.xml_doc = 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.init = function (in_port_id, in_rrate, in_name) {
  var theRules = new Array();

  //
  // Class variables
  //
  this.my_name = in_name;
  this.deals_fetcher = "/cgi-bin/deals.pl";
  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");
}

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) {
      this.xml_doc = this.request.responseXML;
      this.process_data ("");
    } 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 (in_url) {
  var caller = this;

  this.url = this.deals_fetcher + "?" + in_url;

  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 (criterion) {
  //
  // Get basic info
  //
  var desc_len = 70;
  var title = this.xml_doc.getElementsByTagName('title').item(0).firstChild.data;
  var link = this.xml_doc.getElementsByTagName('link').item(0).firstChild.data;
  var full_desc = this.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 = this.xml_doc.getElementsByTagName('item');
  var it_count = 0;
  var do_test = (criterion != "");
  var re = new RegExp(criterion, "i");
  var html_str = "";

  for ( it = 0; it < items.length; it++) {
    var item = items[it];
    var it_title = item.getElementsByTagName('title').item(0).firstChild.data;

    if (re.test (it_title)) {
      it_count ++;
      var it_link  = item.getElementsByTagName('link').item(0).firstChild.data;
      var it_li_class = (it_count % 2) ? 'li_even' : 'li_odd';

      html_str += "<li class=" + it_li_class + "><a href=\"" + it_link + "\" target=\"feedr_win\">" + it_title + "</a>";
    }
  }

  this.port.innerHTML = html_str;
  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;
  }
}

