(function($){
	$(document).ready(function() {

		// Cache the form element and the target for the responses.
		var $commentform = $('#comments-form'),
		$ajaxmessage = $('#ajax-comment').html('<img src="/_images/ajax-loader.gif" border="0" class="loading"/>');

		// Hide the loading image at first.
		$ajaxmessage.find('img.loading').hide();

		// This makes the transition smoother
		$commentform.parent().wrap('<div style="min-height:355px"/>');

		// Set a global variable that will allow a server-side callback
		successful = false;

		// Simple check for the commentform actually being present
		if ($commentform.length > 0) {
			$commentform.ajaxForm({
				target: $ajaxmessage,
				beforeSubmit: function(data, $form, opt){
					$ajaxmessage.html('<img src="/_images/ajax-loader.gif" border="0" class="loading"/>');
					$ajaxmessage.find('img.loading').show();
					$commentform.parent().fadeOut(500);
				},
				success: function(){
					if (successful != true ){
						$commentform.parent().fadeIn(400);
					}
				}
			});
		}
	});
}(jQuery));
