// recent_comments.js
//

function chhRecentComments(){}

//-----------------------------------------------------------------------------
chhRecentComments.load_comments = function (id, num)
//
// Load <num> recent comments and insert them into node with id=<id>
//
{
  // Get target node
  var node = document.getElementById(id);
  if (!node) {
    return;
  }

  // Put temp text in to start
  var textNode = document.createTextNode("Loading...");
  node.appendChild(textNode);

  // Load the XML
  var xmlhttp = chhRecentComments.getXMLHttpRequest();
  var async = true;
  xmlhttp.open("GET", "http://" + location.host + "/feeds/comments/default?max-results=" + num + "&alt=json", async);

  xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4) {
      if (xmlhttp.status == 200) {
        
        // Parse the JSON
        var data = eval('(' + xmlhttp.responseText + ')');
        
        // Get the entries
        var entries = data.feed.entry;

        // Get the titles for recent posts
        chhRecentComments._get_post_titles();

        // Delete the marker text
        node.removeChild(node.lastChild);

        // Start list
        var list = document.createElement('ul');
        node.appendChild(list);

        // Fill from the xml entries
        for (var i = 0; i !=  entries.length; ++i) {
          var entry =  entries[i];
          var author = entry.author[0];
          var nameText = author.name.$t;
          var link = entry.link[0];
          for (var j = 0; j != entry.link.length; ++j) {
            link = entry.link[j];
            if (link.rel == 'alternate') {
              break;
            }
          }
          var linkURL = link.href;
          //linkURL = linkURL.replace(/#/, '#comment-');
          linkURL = linkURL.replace(/#c/, '#comment-');
          var rootURL = RegExp.leftContext;
          rootURL = rootURL.replace(/\?.*/, '');
          var title = chhRecentComments.post_titles[rootURL];
          if (!title) {
            title = rootURL;
            title = title.replace(/^.*\//, '');
            title = title.replace(/\.html$/, '');
            title = title.replace(/_/g, ' ');
          }
          var link = document.createElement('a');
          link.setAttribute('href', linkURL);
          link.setAttribute('title', entry.title.$t);
          var li = document.createElement('li');
          list.appendChild(li);
          li.appendChild(link);
          var tn = document.createTextNode(nameText + " on " + title);
          link.appendChild(tn);
        }
      } else {
        node.firstChild.setValue("Failed to load");
      }
    }
  };
  xmlhttp.send(null);

};

//-----------------------------------------------------------------------------
chhRecentComments._get_post_titles = function() 
//
// Get a list of post titles from site feed
//
{
  chhRecentComments.post_titles = new Array();

  // Load the XML
  var xmlhttp = chhRecentComments.getXMLHttpRequest();

  var async = false;
  xmlhttp.open("GET", "http://" + location.host + "/feeds/posts/default?alt=json", async);
  xmlhttp.send(null);
  if (xmlhttp.status == 200) {
    
    // Parse the JSON
    var data = eval('(' +xmlhttp.responseText + ')');

    var entries = data.feed.entry;

    for (var i = 0; i !=  entries.length; ++i) {
      var entry = entries[i];
      var title = entry.title.$t;
      var link = entry.link[0];
      for (var j = 0; j != entry.link.length; ++j) {
        link = entry.link[j];
        if (link.rel == 'alternate') {
          break;
        }
      }
      //alert(link + " -> " + title);
      chhRecentComments.post_titles[link.href] = title;
    }
  }
};

//-----------------------------------------------------------------------------
chhRecentComments.getXMLHttpRequest = function() 
//
// Get an XMLHttpRequest object in cross browser way
//
{
  var xmlhttp = null;
  if (self.XMLHttpRequest) {
    xmlhttp = new XMLHttpRequest();
  } else if (self.ActiveXObject) {
    // Try MSXML objects in order of preference
    var msxmls = ["Msxml2.XMLHTTP.6.0", "MSXML2.XMLHTTP.3.0",
      "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"];
    for(var i=0; i < msxmls.length; ++i) {
      try {
        xmlhttp = new ActiveXObject(msxmls[i]);
        // If we are still here it worked
        // don't try any more
        break;
      } catch(err) {
        // Do nothing, just try next one if any
      }
    }
  }
  return xmlhttp;
};
