jQuery.noConflict(); 

function ajaxEffect(content){
	jQuery(content).css({visibility:"hidden"}).fadeIn(1000).css({visibility:"visible"}).animate({opacity: 1.0}, 3000).fadeOut(1000);
}

function ajaxEffectIn(content){
	jQuery(content).css({visibility:"hidden"}).fadeIn(1000).css({visibility:"visible"});
}

jQuery(document).ready(function() {

  jQuery('#nav li').hover(function() {
    jQuery(this).addClass('sfhover');
  }, function() {
    jQuery(this).removeClass('sfhover'); 
  });

  jQuery('#nav a').removeAttr("title");
  
  jQuery('#toggleheadline').show();
  jQuery('#oldcomments').hide();
  
  jQuery('a#toggle').click(function() {
    jQuery('#oldcomments').toggle(500);
    jQuery('#toggleheadline').fadeOut(1000);
    return false;
  });

  jQuery('#comment').one('focus',function() { //Live Comments
    jQuery('#comment').parent().after('<div id="live-preview"></div>');
  });
  
  var $comment = ''; // that's two single quotation-marks at the end
  jQuery('#comment').keyup(function() {
    $comment = jQuery(this).val();
    $comment = $comment.replace(/\n/g, "<br />").replace(/\n\n+/g, '<br /><br />').replace(/(<\/?)script/g,"$1noscript").replace(/\[quote comment=\".*\"\]/, "<blockquote>").replace(/\[\/quote\]/, "</blockquote>");
    jQuery('#live-preview').html( $comment );
  });

	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:(.*)dkret2.js(.*)/);
		if ( result ) break;
	}
	ajaxURL = result[0].replace(/dkret2.js/, "jquery-comments.php");

	jQuery("#commentform").submit(function(){
		if ( jQuery.find("#commentlist") == "" )	jQuery(".comments_div").append('<ol id="commentlist"></ol>');
		jQuery("#submit").before('<p id="comment-loading">&nbsp;</p>');
		ajaxEffectIn("#comment-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("#comment-loading").fadeOut(1000, function(){
					jQuery("#comment-loading").remove();
					if (request.responseText.search(/<title>WordPress &rsaquo; Error<\/title>/) != -1) {
						var data = request.responseText.match(/<p>(.*)<\/p>/);
						data = '<p id="ajaxerror">Error: ' + data[1] + '</p>';
					} else {
						var data = request.responseText;
					}
					jQuery("#respond").after(data);
					ajaxEffect("#ajaxerror");
				});
			},
			success: function(data){
				if (jQuery("#ajaxerror")) jQuery("#ajaxerror").remove();
				jQuery("#comment-loading").fadeOut(1000, function(){
					jQuery("#comment-loading").remove();
					jQuery(".comments_div").append(data);
					ajaxEffectIn("#newcomment");
					jQuery(".comment_div").animate({opacity: 1.0}, 1500).fadeOut(2000);
					if (window.AjaxEditComments) {
					 AjaxEditComments.init();
					}
				});
			}
		});
		return false;
	});

});
