jQuery.noConflict();

function ajaxEffect(content){
	jQuery(content).css({visibility:"hidden"}).fadeIn(1000).css({visibility:"visible"});
}

jQuery(document).ready(function() {

	var scripts = jQuery(document).find("script");
	for ( i = 0; i <= scripts.length; i++ ) {
		var scriptSrc = jQuery(scripts[i]).attr("src");
		var result = scriptSrc.match(/http:(.*)jquery-comments.js/);
		if ( result ) break;
	}
	ajaxURL = result[0].replace(/jquery-comments.js/, "jquery-ajax.php");

	jQuery("#commentform").submit(function(){
		if ( $.find(".commentlist") == "" )	jQuery(this).prev().before('<ol class="commentlist"></ol>');
		jQuery("h2").contains("Keine Kommentare").fadeOut(1000, function(){
			jQuery("h2").contains("Keine Kommentare").remove();
			jQuery(".commentlist").before('<h2 id="ajaxheader">Kommentare</h2>');
			ajaxEffect("#ajaxheader");
		});
		jQuery(".commentlist").append('<li id="loading"><p>Loading...</p></li>');
		ajaxEffect("#loading");
		$.ajax({
			type: "POST",
			url: ajaxURL,
			data: jQuery("#commentform input, #commentform textarea").serialize(),
			dataType: "html",
			error: function(request){
				if (jQuery("#ajaxerror")) jQuery("#ajaxerror").remove();
				jQuery("#loading").fadeOut(1000, function(){
					jQuery("#loading").remove();
					if (request.responseText.search(/<title>WordPress &rsaquo; Error<\/title>/) != -1) {
						var data = request.responseText.match(/<p>(.*)<\/p>/);
						data = '<li id="ajaxerror"><p>Error: ' + data[1] + '</p></li>';
					} else {
						var data = request.responseText;
					}
					jQuery(".commentlist").append(data);
					ajaxEffect("#ajaxerror");
				});
			},
			success: function(data){
				if (jQuery("#ajaxerror")) jQuery("#ajaxerror").remove();
				jQuery("#loading").fadeOut(1000, function(){
					jQuery("#loading").remove();
					jQuery(".commentlist").append(data);
					ajaxEffect("#newcomment");
					jQuery("#commentform textarea").val("");
				});
			}
		});
		return false;
	});
	
});
