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('#validaciones').addClass('preload');
		    jQuery('#commenterror').hide();
		    var submit='<div id="commentload">Enviando Comentario...</div>';
		    var error='<div id="commenterror"></div>'; 
		    jQuery('#validaciones').html(submit);
//			jQuery('#validaciones').addClass('preload');
		    jQuery('#validaciones').html(error); 
//		    jQuery('#commentload').fadeIn();
		},
		error: function(request) { 
			jQuery('#validaciones').removeClass('preload'); 
		    jQuery('#commentload').fadeOut('normal');
		    var message = request.responseText;
		    jQuery('#commenterror').fadeIn("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('body').empty().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.fadeIn();
				jQuery('#commentform:input').attr('disabled', true);
				jQuery('#commentload').slideUp("slow");
				setTimeout(function() {
			    	jQuery('#commentform:input').removeAttr('disabled');
					}, 10000);
		    	}
			}
	    });
	    return false;
	} );
    }
})
