
document.getDivsByClassName = function(needle) {
  var        my_array = document.getElementsByTagName('div');
  var        retvalue = new Array();
  var        i;
  var        j;

  for (i = 0, j = 0; i < my_array.length; i++) {
    var c = " " + my_array[i].className + " ";
    if (c.indexOf(" " + needle + " ") != -1)
      retvalue[j++] = my_array[i];
  }
  return retvalue;
}

var comments = new Array();
var current_comment;
var comments_div;
var comments_next;
var comment_form;
var comment_send
var context_div;
var technoKey = 'febe762f0c746d1b15ed54168c0d58e6';

function init() {
    comments_div = document.getElementById('comments');
    comments_next = document.getElementById('comments_next');
    comment_form = document.getElementById('comment_form');
    comment_send = document.getElementById('comment_send');
    findComments();
    deStyleComments();
}

function doContext() {
    fillDiv('context', technoURL(window.location));
}

function technoURL(url) {
    var techno_url = 'http://api.technorati.com/cosmos?key=' + technoKey + '&url=' + url + '&type=link&format=rss&limit=10';
    return techno_url;
}

function deStyleComments() {
    comments_div.style.display = "none";
    var i;
    for (i = 0; i < comments.length ; i++ ) {
        comments[i].hide();
    }

    var windowHalfHeight = (window.innerHeight/2);

    comments_div.style.position = "fixed";
    comments_div.style.right = 0;
    comments_div.style.top = windowHalfHeight.toString() + 'px';
    comments_div.style.width = "50%";
    comments_div.style.height = windowHalfHeight.toString() + 'px';
    if (current_comment) 
        comments_next.style.display = "inline";
}

function nextComment() {
    current_comment.nextHandler();
    return false;
}

function readComments() {
    comments_div.style.display = "block";
    if (current_comment)
        current_comment.show();
}

function dismissComments() {
    comments_div.style.display = "none";
    return false;
}
    
// more to come here
var Comment = function(element) {
    this.element = element;
    this.id = element.id;
    this.next = null;
}

Comment.prototype.constructor = Comment;

Comment.prototype.nextHandler = function() {
    if (this != this.next) {
        this.next.show();
        this.hide();
        current_comment = this.next;
    }
}

Comment.prototype.show = function() {
    this.element.style.display = "block";
}

Comment.prototype.hide = function() {
    this.element.style.display = "none";
}

function findComments() {
    var comment_elements = document.getDivsByClassName("comment");
    var i;

    for (i = 0; i < comment_elements.length; i++) {
        comments[i] = new Comment(comment_elements[i]);
    }

    for (i = 0; i < comment_elements.length - 1; i++) {
        comments[i].next = comments[i + 1];
    }

    if (comments[i]) {
        comments[i].next = comments[0];
        current_comment = comments[0];
    }

}

function leaveComment(element) {
    element.innerHTML = 'Retrieving Form';
    var formHTML = retrieveCommentForm();
    comment_form.innerHTML = formHTML;
    comment_form.style.display = "block";
}

function submitComment() {
    comment_form.style.display = 'none';
    comment_send.style.display = 'block';
// XXX do some validation first?
    return true;
}

function cancelComment() {
// XXX change back link text to 'Leave Comment'
    comment_form.style.display = 'none';
}

function retrieveCommentForm() {
    var request = new XMLHttpRequest();
    request.open("GET", '/~cdent/mt/send-comment-form.cgi?entry_id='
        + entry_id, false);
    request.send(null);
    return request.responseText;
}


