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");
    if ( scriptSrc ) var result = scriptSrc.match(/http:(.*)jquery-comments.js/); // Kretzschmar: If Javascript before jquery-comment.js has no src!
		if ( result ) break;
	}
	ajaxURL = result[0].replace(/jquery-comments.js/, "jquery-ajax.php");

	jQuery("#commentform").submit(function(){
		if ( jQuery(".commentlist") == "" )	jQuery(this).prev().before('<ol class="commentlist"></ol>');

		jQuery(".commentlist").append('<li id="loading"><p>Loading...</p></li>');
		ajaxEffect("#loading");
		jQuery.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("h3").contains("No Responses").fadeOut(1000, function(){
					jQuery("h3").contains("No Responses").remove();
					jQuery(".commentlist").before('<h3 id="ajaxheader">Thank you for placing first comment!</h3>');
					ajaxEffect("#ajaxheader");
				});
				jQuery("#loading").fadeOut(1000, function(){
					jQuery("#loading").remove();
					jQuery(".commentlist").append(data);
					ajaxEffect("#newcomment");
					jQuery("#commentform textarea").val("");
					if (window.AjaxEditComments) {
						 AjaxEditComments.init();
					}
				});
			}
		});
		return false;
	});
	
});
