function ajaxEffect(content){
	$(content).css({visibility:"hidden"}).fadeIn(1000).css({visibility:"visible"});
}

$(document).ready(function() {

	$(".post img").each(function(){
		$(this).attr('class',$(this).attr('align'));
	});

	ajaxURL = "http://gadzetomania.pl/wp-content/plugins/jquery-ajax-comments/jquery-ajax.php";
	$("form#commentform").submit(function(){
		if ( $.find("#commentlist") == "" )	{
			$(this).before('<div id="comments_area"><ul id="commentlist"></ul></div>');
		}
		$("h2#comments").fadeOut(1000, function(){
			$(this).html("Wszystkie komentarze");
			ajaxEffect(this);
		});
		$("#commentlist").append('<li id="loading"><p>Proszę czekać... trwa publikowanie komentarza</p></li>');
		ajaxEffect("#loading");
		$.ajax({
			type: "POST",
			url: ajaxURL,
			data: $("#commentform input, #commentform textarea").serialize(),
			dataType: "html",
			error: function(request){
				if ($("#ajaxerror")) {$("#ajaxerror").remove();}
				$("#loading").fadeOut(1000, function(){
					$("#loading").remove();
					if (request.responseText.search(/<title>WordPress &rsaquo;<\/title>/) != -1) {
						var data = request.responseText.match(/<p>(.*)<\/p>/);
						data = '<li id="ajaxerror"><p>' + data[1] + '</p></li>';
					} else {
						var data = request.responseText;
					}
					$("#commentlist").append(data);				
					ajaxEffect("#ajaxerror");
				});
			},
			success: function(data){
				if ($("#ajaxerror")) { $("#ajaxerror").remove(); }
				$("#loading").fadeOut(1000, function(){
					$("#loading").remove();
					$("#commentlist").append(data);
					$("#commentform textarea").val("");
					ajaxEffect("#newcomment");
				});
			}
		});
		return false;
	});
	
});
