$.fn.ajaxSubmit = function(e) {
	/* Change a form's submission type to ajax */
	this.submit(function(){

		var params = {};
		$(this)
		.find(":input")
		.each(function() {
			if(!$(this).attr('disabled'))
				params[ this.name || this.id || this.parentNode.name || this.parentNode.id ] = this.value;
		});
		$("body").addClass("curWait");

		$(".submit-comment input").attr('disabled', true);

		var btns_html = $(".submit-comment").html();
		var sb_height = $(".submit-comment").height();

		$(".submit-comment").html('Триває завантаження...');
		$(".submit-comment").css('text-align', 'center');
		$(".submit-comment").height(sb_height);

		if(comments_count >= json.comments_pp)
			var next_page = parseInt(last_page) + 1;
		else
			var next_page = parseInt(last_page);

		$("textarea").attr('disabled', true);

		if(crop_data && crop_data.w != 0 && crop_data.h != 0)
		{
			params['data[Crop][x]'] = crop_data.x;
			params['data[Crop][y]'] = crop_data.y;
			params['data[Crop][width]'] = crop_data.w;
			params['data[Crop][height]'] = crop_data.h;
		}

		$.post(this.getAttribute("action") + '/' + lastCommentId + '/' + next_page, params, function(data){

			$("#hidden-commn").html(data);

			$("textarea").removeAttr('disabled');

			for (var x in json.comments)
			{
				if(comments_count < json.comments_pp)
				{
					drawComment( json.comments[x] );
					lastCommentId = json.comments[x].Comment.id;
					comments_count++;
				}
				else
				{
					if (jQuery.browser.safari)
					{
						var page = 0;
						page = parseInt(last_page) + 1;
						newhash = page;
					}
					else
					{
						var page = 0;
						page = parseInt(last_page) + 1;
						newhash = '#' + page;
					}

					location.hash = newhash;

					comments_count = 0;
					drawPager(parseInt(last_page) + 1, $("#CommentPhotoId").val());
					$("#comments_inner").html('');
					drawComment( json.comments[x] );
				}
			}

			if(rated_form == 1)
			{
				$(".add_rating").remove();
				$("#switch_mark").remove();
				$("#rating-good").remove();
				$("#rating-bad").remove();
				$('#tabs-2').load('/ratings/ajax_ratings/' + $("#CommentPhotoId").val(), function(){});
			}

			if(json.flash_msg != '')
			{
				$("#flashMessage").remove();
				$("#flashMessageContainer").append('<div class="message ' + json.flash_class + '" id="ajaxFlashMessage">' + json.flash_msg + '</div>');
				$(".message").delay(5000).fadeOut(1000);
				clearRates();
			}

			$(".crop_added_icon").remove();
			initAnnotate();
			initRatings();
			initCommentControls();
			$("#comment-to").hide();
			initCommentReply();

			$(".submit-comment").html(btns_html);
			$(".submit-comment input").removeAttr('disabled');
			$(".submit-comment").css('text-align', 'right');
			$("#CommentParentId").val('');
			$("#CommentToUserId").val('');
			
			if(rated_form == 1)	
				clearRates();
			
			$("#CommentComment").val('');
			$("#pagination").css('padding-bottom', '20px');

		});

		$(".crop_added_icon").remove();
		initAnnotate();

		return false;
	});

	return this;
}
