function addComment()
{
  var link,
      list,
      displayNewComment;

  displayNewComment = function(data, statusText, xhr, form) {

    var comments = link.closest('div.comments'),
        list     = comments.children('ul');

    list.append(data);

    if (jQuery('.more:visible', comments).size() != 0 && list.children(':visible').size() < 4) {
      jQuery('.more:visible', comments).slideUp();
    }

    if (jQuery('.reply_form').hasClass('refresh-me')) {
      location.reload();
    }
    link.prev().val('');
    link.show().next().hide();
  };
  
  jQuery('.reply_form').ajaxForm({
    cache: false,
    success: displayNewComment,
    error: function (formData, form, options) {
    },
    beforeSubmit: function (formData, form, options) {
    }
  }); 

  jQuery('.button_validate').unbind('click').click(function (e) {
    e.preventDefault();

    var textarea = jQuery(this).prevAll('textarea'),
        value    = textarea.val();

    value = value.replace(/^\s+|\s+$/g, "");
    if (value.length > 0)
    {
      link = jQuery(this);
      link.hide().next().show();
      link.closest('form').submit();
    }
  });

  jQuery('.all-comments').unbind('click').click(function (e) {
    e.preventDefault();

    list = jQuery(this).closest('ul');
    var url_for = list.find('.more a').attr('url');

    list.closest('.more').html('<div class="loader small horizontal"></div>');

    jQuery.ajax({
      url: url_for,
      success: function (data) {
        list.html(data);
      }
    });
  });

  jQuery('.comment-preview a').click(function(e)
  {
    var preview = jQuery(this).closest('span');
    var full = preview.next();

    preview.hide();
    full.show();

    e.preventDefault();
  });

  jQuery('.comment-full a').click(function(e)
  {
    var full = jQuery(this).closest('span');
    var preview = full.prev();

    full.hide();
    preview.show();

    e.preventDefault();
  });
}
jQuery(document).ready(function(){
  addComment();
});
