// -*- coding: utf-8 -*-

// A simple script to build my "recent comments box".

// Only works on browsers supporting new XMLHttpRequest(), which is
// probably Firefox and Mozilla.  But should not cause problems
// where not supported.

function mgbcf_sendRequest(url, callback) {
  try {
    var req = new XMLHttpRequest();
    if(req) {
      req.open("GET", url, true);
      req.setRequestHeader('User-Agent', 'XMLHTTP/1.0');
      req.onreadystatechange = function () {
        if(req.readyState == 4) {
          if(req.status == 200 || req.status == 304) {
            callback(req);
          }
          // else alert('HTTP error ' + req.status);
        }
      }

      if(req.readyState != 4) {
        req.send(null);
      }
    }
  }
  catch(e) {
    // nop
  }
}

function mgbcf_handler(req) {
  if(req.responseXML) {
    var root = req.responseXML.documentElement;

    if(root && root.childNodes) {
      var entries = new Array();

      // procesa cada entry, agregando los resultados a un arreglo
      // maldita sea, esto deberia ser ruby...
      for(var n = 0; n < root.childNodes.length; n++) {
        if(root.childNodes[n].nodeName == "entry") {
          var r = mgbcf_process_entry(root.childNodes[n]);
          if(r) {
            entries.push(r);
          }
        }
      }

      if(entries.length > 0) {
        var h3 = document.createElement("H3");
        h3.appendChild(document.createTextNode("Comentarios recientes"));
        document.mgbcf_target.appendChild(h3);

        for(var n = 0; n < entries.length; n++) {
          document.mgbcf_target.appendChild(entries[n]);
        }
      }
    }
  }
}

function mgbcf_process_entry(entry) {
  var blurb = false;
  var summary = false;
  var author = false;
  var link = false;
  var r = false;

  for(var n = 0; n < entry.childNodes.length; n++) {
    var node = entry.childNodes[n];

    if(node.nodeName) {
      if(node.nodeName == "title") {
        blurb = mgbcf_extract_text(node);
      }
      else if(node.nodeName == "summary") {
        if(!blurb) {
          summary = mgbcf_extract_text(node);
        }
      }
      else if(node.nodeName == "link" &&
              node.getAttribute("rel") == "alternate") {
        link = node.getAttribute("href");
      }
      else if(node.nodeName == "author") {
        for(var n1 = 0; n1 < node.childNodes.length; n1++) {
          if(node.childNodes[n1].nodeName == "name") {
            author = mgbcf_extract_text(node.childNodes[n1]);
            break;
          }
        }
      }
    }
  }

  if(!blurb && summary) {
    blurb = summary;
  }

  if(blurb && author && link) {
    if(blurb.length > 23) {
      var s = blurb.substring(0, 20) + "...";
      blurb = s;
    }

    if(author.length > 9) {
      var s = author.substring(0, 9);
      author = s;
    }

    var a = document.createElement("A");
    a.href = link;
    a.appendChild(document.createTextNode('"' + blurb + '"'));

    var sp = document.createElement("SPAN");
    sp.appendChild(document.createTextNode(" por " + author));

    r = document.createElement("DIV");
    r.appendChild(a);
    r.appendChild(sp);
  }

  return r;
}

function mgbcf_extract_text(node) {
  var text = "";

  for(var n = 0; n < node.childNodes.length; n++) {
    var child = node.childNodes[n];
    if(child.nodeType == 3) {
      text = text + child.nodeValue;
    }
  }

  return text;
}

function mgbcf_make(element_id) {
  if(document.getElementById) {
    document.mgbcf_target = document.getElementById(element_id);
    if(document.mgbcf_target) {
      mgbcf_sendRequest("/feeds/comments/default?max-results=8",
                        mgbcf_handler);
    }
  }
}
