var editing = 0;
var replying = 0;

function commentWindow(url) {
  cWin = window.open(url, '_blank', 'width=500,height=255,resizable=yes');
}

function toggleComment(id, ref_id) {
  commentNode = document.getElementById("comment"+id);
  if(commentNode.className == 'comment_expanded')
    collapseComment(commentNode);
  else {
    expandComment(id, ref_id);
    Element.scrollTo($('comment'+id));
  }
}

function collapseComment(commentNode) {
  var commentBody = commentNode.getElementsByTagName('div').item(2);
  commentNode.className = 'comment_collapsed';
  commentBody.style.display = "none";
}

function expandComment(id, ref_id) {
  var commentNode = document.getElementById("comment"+id);
  var commentBody = commentNode.getElementsByTagName('div').item(2);
  commentNode.className = 'comment_expanded';
  commentNode.setAttribute("expanded", 1);
  commentBody.style.display = "block";
  
  var commentNode = document.getElementById("comment"+id);
}

function getComments(node_id, comment_id) {
  var req = getXMLHttpRequest();
  req.open("GET", "comment_req.php?action=get_comments&node_parent="+node_id+"&comment_parent="+comment_id, true);
  req.onreadystatechange = function() {
    if(req.readyState == 4) {
      var prevNode;
      if(comment_id == 0)
        prevNode = document.getElementById("cstart");
      else 
        prevNode = document.getElementById("comment"+comment_id);
      var clist = document.createElement("div");
      clist.innerHTML = req.responseText;
      prevNode.appendChild(clist);
    }
  }
  req.send(null);
}

function cancelReply(id) {
  var commentNode = $('comment'+id);
  var replyForm = $('replyForm');
  var replyLink = $('replyLink'+id);
  replyLink.innerHTML = 'reply';
  replyLink.setAttribute('href', 'javascript:replyForm('+id+')');
  Element.remove(replyForm);
  replying = 0;
}

function replyForm(id, ref_id) {
  if(editing || replying) return;
  replying = 1;
  expandComment(id);
  var commentNode = $("comment"+id);
  if(commentNode.getElementsByTagName('div').item(3)) 
    return;

  var showForm = function(req) {
    var replyLink = $('replyLink'+id);
    replyLink.setAttribute('href', 'javascript:cancelReply('+id+')');
    replyLink.innerHTML = 'cancel reply';
    var replyHTML = '<div id="replyForm" class="reply_form" display="block">'+req.responseText+'</div>';
    new Insertion.Bottom(commentNode, replyHTML);
    // initInputs again to protect the newly created text input areas
    initInputs();
    // Jump to the newly inserted form
    Element.scrollTo($('replyForm'));
    Field.focus('comment_title');
  }

  var url = "reply_form.php";
  var pars = "action=get_form&parent_id="+id;
  var req = new Ajax.Request(url, {method: 'get', parameters: pars, onComplete: showForm});
}

function editComment(cid, rid) {
  if(editing || replying) return;
  editing = 1;

  expandComment(cid, rid);
  var comment = $('comment'+cid);
  var cTitle = $('ctitle'+cid);
  var cBody = $('cbody'+cid);
  var eLink = $('editLink'+cid);
  
  // Change the eLink
  eLink.innerHTML = 'cancel editing';
  eLink.setAttribute('href', 'javascript:cancelEdit('+cid+', '+rid+')');

  // Setup the title editor
  Element.hide(cTitle);
  var titleEdit = '<input type="text" width="140px" value="'+cTitle.innerHTML+'" id="titleEdit'+cid+'" name="node_name"/>';
  new Insertion.After(cTitle, titleEdit);

  // Setup the body editor
  Element.hide(cBody);
  //FIXME: Need to strip out tags here
  var bodyEdit = '<div class="comment_body" id="cBodyEdit'+cid+'"><textarea rows="5" style="width: 100%" id="bodyEdit'+cid+'" name="node_body">'+cBody.innerHTML.replace(/<[Bb][Rr]\/?>/g, '')+'</textarea>';
  bodyEdit += '<br/><a href="#" id="saveLink'+cid+'">save</a>';
  bodyEdit += ' | <a href="#" id="cancelLink'+cid+'">cancel</a>';
  new Insertion.After(cBody, bodyEdit);

  // Protect newly created text elements
  protectElement($('bodyEdit'+cid));
  protectElement($('titleEdit'+cid));

  // Create the save and cancel links
  Event.observe($('saveLink'+cid), 'click', function() { updateComment(cid, rid); });
  Event.observe($('cancelLink'+cid), 'click', function() { cancelEdit(cid, rid); });
}

function updateComment(cid, rid) {
  editing = 0;
  var comment = $('comment'+cid);
  var cTitle = $('ctitle'+cid);
  var cBody = $('cbody'+cid);
  var eLink = $('editLink'+cid);

  var comment_body = $F('bodyEdit'+cid);
  var comment_title = $F('titleEdit'+cid);

  if(!comment_body) {
    alert('Your comment is empty, dunce. '+comment_body);
    return;
  }

  // Build the URL
  url = 'comment.php';
  params = 'action=update&cid='+cid
           +'&comment_title='+escape(comment_title)
           +'&comment_body='+escape(comment_body);

  location.href = url+'?'+params;
  return;
}

function cancelEdit(cid, rid) {
  var comment = $('comment'+cid);
  var cTitle = $('ctitle'+cid);
  var cBody = $('cbody'+cid);
  var eLink = $('editLink'+cid);

  // Change the eLink
  eLink.innerHTML = 'edit';
  eLink.setAttribute('href', 'javascript:editComment('+cid+', '+rid+')');

  // Remove editing components
  Element.remove($('titleEdit'+cid));
  Element.remove($('cBodyEdit'+cid));

  // Show the hidden original elements
  Element.show(cTitle);
  Element.show(cBody);

  // Set the global editing flag to zero
  editing = 0;
}

function closeCommentWindow(parentId, commentId) {
  var newUrl = 'view_node.php?id='+parentId+'&cid='+commentId+'#comment'+commentId;
  window.opener.location.href = newUrl;
  window.close();
}
