jQuery(document).ready(function() {
	var url = jQuery('#commentform').attr('action');
    if (jQuery('#commentform').length) {
	jQuery('#commentform').submit(function(){
	    jQuery.ajax({
		url: url,
		data: jQuery('#commentform').serialize(),
		type: 'POST',
		beforeSend: function() {
		    jQuery('#commenterror').hide();
		    var submit='<div id="commentload" style="display: none;">Submitting Comment...</div>';
		    var error='<div id="commenterror" style="display: none;margin: 0 auto;"></div>'; 
		    jQuery('#comments').after(submit);
		    jQuery('#comments').after(error); 
		    jQuery('#commentload').slideDown();
		},
		error: function(request) { 
		    jQuery('#commentload').slideUp('normal');
		    var message = request.responseText;
		    jQuery('#commenterror').slideDown("normal").html(message.substring(message.indexOf('<p>')+3,message.indexOf('</p>'))); 
		},
		success: function(data) {
		    if(!/<li/.test(data)){
			jQuery('#commentload').hide('fast');
			jQuery('#commenterror').show('slow').html(data);
		    }else{
			jQuery('textarea').each(function(){
			    this.value='';
			});
			jQuery('#commenterror').hide().html();
			if (!jQuery('#commentlist').length) {
			    jQuery('#comments').prepend('<ol id="commentlist" class="commentlist">');
			}
			jQuery('#commentlist').append(data);
			var new_comment = jQuery('#commentlist li:last').hide();
			jQuery('#commentlist li:last').find('.comment-number a').text('#'+jQuery('#commentlist').children().length);
			jQuery('.post-comments a').text(jQuery('#commentlist').children().length+' '+'comments');
			new_comment.slideDown(1000);
			jQuery('#commentform:input').attr('disabled', true);
			jQuery('#commentload').slideUp("slow");
			setTimeout(function() {
			    jQuery('#commentform:input').removeAttr('disabled');
			}, 10000);
		    }
		}
	    });
	    return false;
	} );
    }
})