/*
  comments engine
*/

function post_comment_onSubmit(formid) {
	// disable the form
	Form.disable(formid);
}
function post_comment_onSuccess(t) {
  var result = t.responseJSON;

  var to_replace = [
      { change:'NEWID',           to:result.id },
      { change:'BODYRAW',         to:result.bodyraw },
      { change:'BODY',            to:result.body },
      { change:'SUBJECT',         to:result.subject },
      { change:'MEMBER_NAME',     to:result.creator_name },
      { change:'MEMBER_ID',       to:result.member_id },
      { change:'http://'+document.domain+'/images/missing/profile_photo/medium.gif',  to:result.memberphotourl },
      { change:'CREATED_AT',      to:result.created_at }
    ];
    
  // create a new comment
  var new_comment = $('duplicatablecomment').cloneNode(true);

  // update with the new values
  to_replace.each( function(rep) {
    new_comment.innerHTML = new_comment.innerHTML.replace(new RegExp(rep.change, "g"), rep.to);
  });

  // display it by adding it to the comments list
  $('comments').appendChild(new_comment);
  new_comment.show();

	// re-enable the form
	Form.enable('post_comment');

  // clear so that they can post another comment
  $$('#post_comment .clearable input').each( function(r) { r.clear(); } );
  $$('#post_comment .clearable textarea').each( function(r) { r.clear(); } );
  
  //run the word wrap function for ff
  if(navigator.appName.indexOf("Netscape")>-1){
  	wordWrapJS();
  } 
}
function post_comment_onFailure(t) {
  display_notification('there was a problem posting the comment');

	// re-enable the form
	Form.enable('post_comment');
}

// reply to a particular post
function reply_to(link) {
  // build the reply text
  var post = Element.up(link, '.replyabletop');
  if (post.down('.postedby')) {
    var member = post.down('.postedby').innerHTML;
    var body = post.down('.bodyraw').innerHTML;
    var reply_with = '[quote]In reply this post by ' + member  + ':\n\n' + body + '[/quote]\n\n';
  } else {
    var body = post.down('.bodyraw').innerHTML;
    var reply_with = '[quote]In reply to the original blog entry:\n\n' + body + '[/quote]\n\n';
  }

  // check to see that they're not part way through another reply
  var go_ahead = false;
  var comment_body = $$('.postreply textarea')[0];
  if (comment_body.value.length > 0) {
    if (confirm('Discard your current reply?'))
      go_ahead = true;
  } else {
    go_ahead = true;
  }

  // update the reply form
  if (go_ahead)
    comment_body.value = reply_with;
  
  // and focus on it
  comment_body.focus();
}
