function dCommentsInit (thread_id, facebook_id)
{
	var container = $(".commentthread"+thread_id).get(0);
	var avatarimg = $("IMG.comments_avatar", container).get(0);
	
	$("DIV.comments_facebookwrapper", container).show();
	
	$("FORM.comments_form", container).submit(function()
	{
		if (!$("TEXTAREA[name=comment]", container).val())
		{
			alert("Bitte geben Sie Ihren Kommentar ein."); 
			$("TEXTAREA[name=comment]", container).focus();
			return false;
		}
	});

	// Facebook Connect Implementation
	
	if (facebook_id)
	{
		var fbbutton = $(".fb_button", container).get(0);
		var hasFacebook = function ()
		{
			FB.api('/me', function (profile)
			{
				$(".comments_facebookname,SPAN.comments_facebook_loggedin_name", container).text(profile.name);
				$("INPUT[name=fbemail]").val(profile.email);
				$(avatarimg).attr("src", "http://graph.facebook.com/"+profile.id+"/picture?type=square");
				$("DIV.comments_anonymous", container).hide();
				$("DIV.comments_facebook", container).show();
				$(".comments_facebook_login", container).hide();
				$(".comments_facebook_loggedin", container).show();
				$("INPUT[name=authorclass]", container).val("dFacebookUser");
			});
		};
		
		var leaveFacebook = function ()
		{
			$("INPUT[name=email]", container).blur();
			$("DIV.comments_anonymous", container).show();
			$("DIV.comments_facebook", container).hide();
			$(".comments_facebook_login", container).show();
			$(".comments_facebook_loggedin", container).hide();
			$("INPUT[name=authorclass]", container).val("dCommentGuestAuthor");
		};
		
		var startupFacebook = function()
		{
			FB.getLoginStatus(function(response)
			{
				if (response.session)
					hasFacebook();
			});
		};
		
		$(fbbutton).click(function()
		{
			FB.login(function(response)
			{
				if (!response.session) return;
				hasFacebook();
			}, {perms:'email'});
			return false;
		});
		
		$(".comments_logoutbutton", container).click(function()
		{
			leaveFacebook();
			return false;
		});
		
		if (!window.fbAsyncInit)
		{
			window.fbAsyncInit = function()
			{
				FB.init(
				{
					appId: facebook_id,
					status: true,
					cookie: true
				});
				
				for (var i = 0; i < dCommentsInit.fbStack.length; i++)
					dCommentsInit.fbStack[i]();
			};
			
			var fbroot = $('<div id="fb-root" />').appendTo("BODY");
			$('<script/>').attr("src", document.location.protocol+'//connect.facebook.net/de_DE/all.js').attr("async", "true").appendTo(fbroot);
			
			dCommentsInit.fbStack = [];
			dCommentsInit.fbStack.push(startupFacebook);
		}
		else
			dCommentsInit.fbStack.push(startupFacebook);
	
		var fb_mailhelper = "Ihre Standard Facebook E-Mail";
	}
	
	// Guest Implementation
	var mail = $("INPUT[name=email]", container).get(0);
	$(mail).blur(function()
	{
		var gurl = "http://www.gravatar.com/avatar/"+dFrame.md5($(this).val())+".jpg?s=50&d="+encodeURIComponent(location.protocol+"//"+location.host+"/design/default_avatar.png");
		$(avatarimg).attr("src", gurl);
	}).blur();
	
	$("FORM.comments_form", container).submit(function()
	{
		if ($("INPUT[name=authorclass]", container).val() != "dCommentGuestAuthor")
			return true;
			
		if (!$("INPUT[name=name]", container).val())
		{
			alert("Bitte geben Sie einen Namen ein, der als Autor des Kommentars angezeigt werden soll."); 
			$("INPUT[name=name]", container).focus();
			return false;
		}
		if (!$("INPUT[name=email]", container).val().match(/^.+@.+\.\w+$/))
		{
			alert("Bitte geben Sie eine gültige E-Mail Adresse ein."); 
			$("INPUT[name=email]", container).focus();
			return false;
		}
	});

	// Highlighting of the anchored comment
	if (window.location.hash)
	{
		var res = window.location.hash.match(/^#comment(\d+)/);
		if (res)
		{
			var $highlight = $("LI#dcomment"+res[1]+" DIV.comments_authorblock IMG", container);
			window.setTimeout(function()
			{
				$highlight.animate({width: 80, height: 80, left: -15, top: -15}).animate({width: 50, height: 50, left: 0, top: 0});
			}, 1000);
		}
	}
	
	// Feedback for the abort button
	$(".comments_abortanswer", container).click(function()
	{
		if (!$("#comment"+thread_id, container).val())
			return true;

		var aa = this;
		LABEL("comments.msg_abortanswer", function(lbl)
		{
			if (confirm(lbl))
				location.href = $(aa).attr("href");
		});
		return false;
	});

	// Thread admin: edit comment
	$(".comments_adminedit", container).each(function()
	{
		var cid = $(this).attr("href");
		$(this).attr("href", "#");
		$(this).click(function()
		{
			dFrame.getJSON("/go/comments/admin/raw/"+thread_id+"/"+cid, null, function(data)
			{
				if (!data) return;

				var textbox = $("<textarea />").val(data.comment).addClass("comments_adminedit").get(0);
				var cbedit = function ()
				{
					dFrame.closeModal();
					dFrame.getJSON("/go/comments/admin/save/"+thread_id+"/"+cid, {comment: $(textbox).val()}, function(data2)
					{
						$("#dcomment"+cid+" DIV.comments_content").html(data2.comment);
					});
				};
				dFrame.showModal(textbox,
				{
					label: "comments.adminedittitle",
					draggable: true,
					shadow: true,
					buttons:
					[
						{label: "website.prompt_ok", callback: cbedit}
					]
				});
			});
			return false;
		});
	});

	// Thread admin: delete comment
	$(".comments_admindelete", container).each(function()
	{
		var cid = $(this).attr("href");
		$(this).attr("href", "#");
		$(this).click(function()
		{
			dFrame.showYesNo("comments.msg_reallydeletecomment", function(yesno)
			{
				if (!yesno) return;
				dFrame.getJSON("/go/comments/admin/delete/"+thread_id+"/"+cid, null, function(data)
				{
					if (!data || !data.success) return;
					$("#dcomment"+cid).remove();
				});
			});
			
			return false;
		});
	});

	// Thread admin: switch comment
	$(".comments_adminswitch", container).each(function()
	{
		var button = this;
		var cid = $(this).attr("href");
		$(this).attr("href", "#");
		$(this).click(function()
		{
			dFrame.getJSON("/go/comments/admin/switch/"+thread_id+"/"+cid, null, function(data)
			{
				if (!data || !data.success) return;

				if (data.accepted)
				{
					$("#dcomment"+cid).removeClass("comments_notaccepted");
					LABEL("comments.accept_no", button);
				}
				else
				{
					$("#dcomment"+cid).addClass("comments_notaccepted");
					LABEL("comments.accept_yes", button);
				}
			});
			return false;
		});
	});
}
