function submitNewComment(response) {

  var parsed_response = $(response);
  if (parsed_response.find('.error').size() == 0) {

    $('#comment_form').replaceWith(parsed_response.find('#comment_form'));

    var newComment = parsed_response.find('li.comment');
    $('ul.comments').append(newComment);
    newComment.effect('highlight', {color:'#FBEE42'}, 3500);
    newComment.find('a.flag_comment').click(flagComment);
  } else {
    $('#comment_form').replaceWith(parsed_response);
  }

  $("#new_comment").ajaxForm(submitNewComment);
}

function flagComment() {
  var comment = $(this).parents('.comment');
  comment.find('form').ajaxSubmit(function(response) {
    comment.find('p').addClass('flagged').html(response);
    comment.find('.flag_action').remove();
  });
}

$(document).ready(function() {
  $("#new_comment").ajaxForm(submitNewComment);           
  $("a.flag_comment").click(flagComment);
});
