/*
  forum engine similar to comment engine
*/

function post_comment_onSuccess(t) {
  var result = t.responseJSON;

  //We have failed display message
  if(!result.success)
  {
    switch(result.error_code)
    {
      case 'no_rights':
        display_notification('You must be logged in to post on the forums.');
        break;
      case 'spam_block':
      	display_notification('Your comment was marked as spam.');
        break;
      default:
        display_notification('You must be logged in to post on the forums.');
        break;
    }
  }
//{ change:'NEWID',           to:result.comment.comment_id },
  var to_replace = [
      { change:'BODY',            to:result.comment.comment_body.gsub('\\n','<br/>') },
      { change:'CREATED_AT',      to:result.comment.comment_created_at }
    ];
    
  // create a new comment
  var new_comment = $('duplicatable_forum_post').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);
  });

  // figure out which member made the comment
  var actor = can_act_as[result.comment.actor_id];
  
  // set the properties to match the user that made the comment
  new_comment.select('.comment_avatar_img')[0].src = actor.small_thumb;
  new_comment.select('.member_details a')[0].href = actor.url;
  new_comment.select('.member_details a')[0].innerHTML = actor.name;
	
  // display it by adding it to the comments list
  $('forum_posts').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 textarea').each( function(r) { r.clear(); } );

}
function post_comment_onFailure(t) {
  display_notification('There was a problem posting the comment');

	// re-enable the form
	Form.enable('post_comment');
} 

function post_comment_onValidationFailed(t) 
{
  display_notification('Your comment post is empty, you must enter a message before you can post on the comment.');
}
