
jQuery(document).ready(function(){
	var form = jQuery('#comment-form');
	var err = jQuery('#comment-error');
	
	jQuery.fn.slideFadeToggle = function(speed, easing, callback) {
		return this.animate({opacity: 'toggle', height: 'toggle'}, speed, easing, callback);
	};
	
	jQuery('#comment-slider').hide();
	jQuery('#comment-slider-link').click(function() {
		var _target = this.href.substr(this.href.indexOf("#") + 1);
		$('#'+_target).slideFadeToggle("slow");
		return false;
	});
	
	if (form) {
	jQuery('#comment-submit').after('<img src="/blog/wp-content/plugins/simple-ajax-comments/loading.gif" id="loading" alt="loading" />');
	jQuery('#loading').hide();
	
	
	// WP Ajax Edit Comments hook
	if (window.AjaxEditComments) {
   	AjaxEditComments.init();
	} // end if
	
    form.submit(function() { 
  if(form.find('#field-name')[0]) {
      if(form.find('#field-name').val() == '') {
		   err.html("<p>Need your Name...</p>");
		   return false;
	   } // end if
		if(form.find('#field-email').val() == '') {
			err.html("<p>Need your email homie... Don't worry, it will not be made public.</p>");
			return false;
		} // end if
		var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if(!filter.test(form.find('#field-email').val())) {
			err.html( "<p>Need your valid email homie... Don't worry, it will not be made public.</p>");
			return false;
		} // end if
	} // end if
	
	if(form.find('#field-comment').val() == '') {
		err.html("<p>Please enter your comment.</p>");
		return false;
	} // end if
	
    jQuery(this).ajaxSubmit({
						   
		beforeSubmit: function() {
			jQuery('#loading').show();
			jQuery('#comment-submit').attr('disabled','disabled');
		}, // end beforeSubmit

		error: function(request){
				err.empty();
				if (request.responseText.search(/<title>WordPress &rsaquo; Error<\/title>/) != -1) {
					var data = request.responseText.match(/<p>(.*)<\/p>/);
					err.html('<p>'+ data[1] +'</p>');
				} else {
					var data = request.responseText.match(/<p>(.*)<\/p>/);
					err.html('<p>'+ data[1] +'</p>');
				}
				jQuery('#loading').hide();
				jQuery('#comment-submit').removeAttr("disabled");
				return false;
		}, // end error()

        success: function(data) {
            try {
					 form.remove(); // REMOVE THIS IF YOU DON'T WANT THE FORM TO DISAPPEAR
					 err.html('<p>Your comment has been added.</p>');
            } catch (e) {
				jQuery('#loading').hide();
				 jQuery('#submit').removeAttr("disabled");
                 //alert(error+'\n\n'+e);
            } // end try
            
            // WP Ajax Edit Comments hook
				if (window.AjaxEditComments) {
   				AjaxEditComments.init();
				} // end if
						   
			} // end success()
			
		}); // end ajaxSubmit()
		
        return false; 
		
	}); // end form.submit()
	}
}); // end document.ready()
										
