/*
  comments 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 do not have permission to do this. You need to be a friend of the content owner in order to make a comment.');
        break;
      case 'invalid_email':
        display_notification('The email you entered was not in the correct format.');
        break;
      case 'no_URLs':
        display_notification('You are not allowed to post URLs in a comment.');
        break;
      case 'spam_block':
        display_notification('Your comment was marked as spam.');
        break;
      default:
        display_notification('There was an error when trying to post the comment.');
        break;
    }
  }

  if(result.comment.actor_id != 1)
  {
    var to_replace = [
      { change:'NEWID',           to:result.comment.comment_id },
      { change:'BODYRAW',         to:result.comment.comment_bodyraw },
      { change:'BODY',            to:result.comment.comment_body },
      { change:'CREATED_AT',      to:result.comment.comment_created_at },
			{ change:'REMOVE_ID',				to:result.comment.comment_id }
    ];    
  }
  else if(result.comment.actor_id = 1)
  {
    var to_replace = [
      { change:'NEWID',           to:result.comment.comment_id },
      { change:'BODYRAW',         to:result.comment.comment_bodyraw },
      { change:'BODY',            to:result.comment.comment_body },
      { change:'CREATED_AT',      to:result.comment.comment_created_at },
			{ change:'REMOVE_ID',				to:result.comment.comment_id },	
			{ change:'MEMBER_NAME',			to:result.comment.comment_poster_name }
    ];
  }
    
  // 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);
  });

  // 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;
  
  if(result.comment.actor_id == 1){
    new_comment.select('.member_details')[0].hide();
    new_comment.select('.member_details_anon')[0].show();
  }
	
  // display it by adding it to the comments list
  $('comments').appendChild(new_comment);
	new_comment.show();

	var remove_link = new_comment.down('.remove-link');
	comment_add_link_events(remove_link, new_comment);

	// 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();
  }
  
  //hide no content sign
  $$('.nocontent')[0].hide();
}
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.');
}

// 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 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();
}

// attach events to all the comment posts in the page
Event.observe(document, 'dom:loaded', function(){
  $$('.comment_post').each( function( post ){
    var remove_link = post.down('.remove-link');
    comment_add_link_events(remove_link,  post);
  });
});

// now add the functions used for show/hide the remove link
function comment_add_link_events(remove, post) {
  if (post.down('.remove-link')) {
  	Event.observe(post, 'mouseover', function(){ comment_show_remove( remove ); });
  	Event.observe(post, 'mouseout', function(){ comment_hide_remove( remove ); });
  	Event.observe(remove, 'click', function(e){ comment_kill_comment(e, remove, post); });
  }
}  
  
function comment_show_remove(remove) {
	remove.show();
}
		      
function comment_hide_remove(remove) { 
	remove.hide();      
}


function comment_kill_comment(e, link, post) {
	// try to stop the normal linking (though allow it to fall through without javascript)
	Event.stop(e);
	// use ajax instead
	var aj_link = link.href + '.json';
	var the_post = post;
	new Ajax.Request(aj_link, {asynchronous:true, onSuccess:function(t){ kill_post_onSuccess(t, the_post); }, onFailure:kill_post_onFailure});
}

function kill_post_onSuccess(t, post) {
  // get rid of the note
  Effect.toggle(post, 'appear', { duration:0.3, afterFinishInternal:function(){
    // get rid of it from the dom
    post.remove();
  } });
}
function kill_post_onFailure() {
  // do we care?
}

