/*
  comments engine
*/

// commit posting validation
function post_comment_onSubmit(frm, errors, custom_errors) {
  // make sure they haven't tried to post a link to another website
  var content = frm.down('textarea').value;
  if (content.toLowerCase().indexOf('http://') > -1) {
    vailidation_failed(errors, 'comment.body', 'has_link')
  }
}

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

  var list = $('commentlist');

  // commenting didn't work?
  if(!result.success)
  {
    switch(result.error_code)
    {
      case 'no_rights':
        result.message = "You do not have permission to do this.";
        break;
      case 'invalid_email':
        result.message = "Your email address is invalid, please correct and try again.";
        break;
      case 'limit_three':
        result.message = "You are only allowed to post once every 3 minutes.";
        break;
      case 'invalid_html':
        result.message = "Your message contains invalid characters (or tags). Please remove before continuing.";
        break;
      default:
        return post_comment_onFailure();
        break;
    }
  } else {
    // 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(); } );  
  }

  display_notification(result.message);  

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

}

function post_comment_onFailure() {
  display_notification('Sorry, there was a problem posting your 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 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?
}

