(function($) {

$.fn.insertComment = function(commentHtml, commentCountSelector){

  commentCountSelector = commentCountSelector !== undefined ? commentCountSelector : '#commentCount';

  var comment              = $(commentHtml);
  var comment_ctime        = comment.find('.ctime').text();
  var comment_id           = comment.find('.id').text();
  var comment_replyto      = comment.find('.wcommentview_reply_to').text();      // will be updated with new nav links
  var comment_prev_replyto = comment.find('.prev_reply_to').text(); // will be updated with new nav links

  var inserted = false;

  this.children('.comment').each(function(){

	  var next_ctime = $(this).find('.ctime').text();
	  var next_id    = $(this).find('.id').text();

	  if(next_ctime > comment_ctime){
      $(this).find('.comment').css('background-color','transparent');
	    comment.hide().insertBefore($(this)).css('background-color','#F20C76').fadeIn(1000).animate({'backgroundColor':'transparent'}, 20000);
	    inserted = true;
      $(commentCountSelector).increment();
	    return false;
	  }
	  if(next_id == comment_id){
	    inserted = true;
	    return false;
	  }

	});

  if(inserted === false){
    this.children('.comment').css('background-color','transparent');
    comment.appendTo(this).css('background-color','#F20C76').fadeIn(1000).animate({'backgroundColor':'transparent'}, 20000);
    $(commentCountSelector).increment();
  }

  // update parent comment links
  if(comment_replyto !== undefined && comment_replyto !== ''){
    console.log("INSERTED REPLY TO:"+comment_replyto);
    $('#comment'+comment_replyto+' .replylinks').load('/comments/comment/replyLinks/id/'+comment_replyto);
  }

  // update previous reply comment links
  if(comment_prev_replyto !== undefined && comment_prev_replyto !== ''){
    console.log("PREV REPLY IS:"+comment_prev_replyto);
    $('#comment'+comment_prev_replyto+' .replylinks').load('/comments/comment/replyLinks/id/'+comment_prev_replyto);
  }

  return comment;

};

$.fn.removeCommentForm = function(){
  return this.each(function(){
    // $(this).hide("blind",{direction:"up"},1000);
    // $(this).slideUp(1000, function(){console.log("finished sliding up");});
    $(this).hide();
    $(this).remove();
  });
};

$.fn.revealCommentForm = function(){
  return this.each(function(){
    // $(this).show("blind",{direction:"down"},1000);
    // $(this).slideDown(1000, function(){console.log("finished sliding down");});
    $(this).show();
  });
};

$.fn.openCommentReplyForm = function(commentId){

  // clear any open forms
  $('.wcommentreply_replyformcontainer').removeCommentForm();

  // create new form
  var containerId = 'wcommentreply_replyformcontainer_'+commentId;
  $('<div class="hidden" />').attr('id',containerId).addClass('wcommentreply_replyformcontainer').hide().removeClass('hidden').insertAfter(this.parents('.comment:first'));

  // fill new form
  $.get('/comments/comment/reply/parent_id/'+commentId+'.htm',function(data){$('#'+containerId).html(data);},'html');

  // reveal new form
  $('#'+containerId).revealCommentForm();

};

$.fn.closeCommentReplyForm = function(){
  // clear form for specified id
  this.parents('.wcommentreply_replyformcontainer:first').removeCommentForm();
};

$.scrollToComment = function(selector){
  $(selector).stop(true, false); // restart the highlight animation if re-pressed
  $(selector).css('background-color','#F20C76');
  $.scrollTo(selector, 1000);
  $(selector).animate({'backgroundColor':'transparent'},20000);
};

})(jQuery);

function wcommentreply_openReplyForm(commentId){
  jQuery('#wcommentreply_link_'+commentId).openCommentReplyForm(commentId);
}

function submitCommentForm(formId,clearForm,data){
  if(data.errors !== undefined){
	  jQuery('#'+formId).addFormErrors(data.errors);
  }
  else{

    if(clearForm){
	    jQuery('#'+formId).closeCommentReplyForm();
    }
    else{
	    jQuery('#'+formId+' textarea').first().val('');
      jQuery('#'+formId+' .errorMessage').remove();
      jQuery('#'+formId+' .error').removeClass('error');
	  }

	  if(data.message !== undefined){
	    // flash response message
      flashMessage(data.message, 'success');
	  }
	  else{
	    // insert new comment
	    jQuery.scrollTo(jQuery('#wcommentlist_comments').insertComment(data), 1000);
	  }

  }
}


