jQuery(function($) {
  $('#blog_posts-show form#new-comment-form').submit(function() {
    var $this = $(this);
    $.ajax({
      beforeSend: function(request) {
        $this.find('div.notice').remove();
        $this.find('div.error').remove();
      },
      complete: function(request, status) {
        $this.find('textarea').attr('value', '');
      },
      data: $this.serialize(),
      dataType: 'script',
      success: function(response, status) {
        $this.prepend(
          "<div class=\"notice\">Your comment has been added!</div>"
        );
        $('#blog_posts-show ol.comments').append(response.toString());
        $('#blog_posts-show p.no-comments-notice').remove();
        // $("html:not(:animated),body:not(:animated)").animate({ 
        //   scrollTop: $('#blog_posts-show ol.comments li:last-child').offset().top 
        // }, 1100 );
      },
      error: function(request, status, exception) {
        $this.prepend(
          "<div class=\"error\">There was a problem saving your comment. Please try again.</div>"
        );
      },
      type: 'POST',
      url: $this.attr('action')
    });
    return false;
  });
});
