$(document).ready(function(){

	Comments.prepare();

});


Comments = {

	template: null,

	prepare: function() {

		// remove the listbox
		$("#serendipity_CommentForm div.commentReplyToMenu").remove();

		// create a replacement hidden-field
		$("#serendipity_CommentForm form").append('<input type="hidden" name="serendipity[replyTo]" id="serendipity_replyTo" value="0" />')

		// clone the form to copy it to needed places
		Comments.template = $("#serendipity_CommentForm").clone();

		// clone is not visible
		$(Comments.template).hide();

		// change reply-links to contain the function to display the comment form
		$("a.reply").click(function(){ return Comments.click(this) });

	},

	click: function(node) {

		// the node of the comment
		var container = $(node).parent().parent();

		// check if the comment form is already there
		if(container.attr("replyVisible") == "visible")
			return false;

		// not let's set the semaphore
		container.attr("replyVisible", "visible");

		// this comment's id is what s9y needs to know
		var id = container.attr("id").substr(1);

		// change the reply-link's appearance
		$(node).addClass("active");

		// clone the reply form template
		node.template = $(Comments.template).clone();

		// make all ids inside the reply form unique
		$(node.template).find("*[@id]").each(function() {
			$(this).attr("id", $(this).attr("id") + "_" + id);
		});

		$(node.template).find("*[@for]").each(function() {
			$(this).attr("for", $(this).attr("for") + "_" + id);
		});

		// hook the reply form into the dom ...
		container.append(node.template);
		// ... and make it appear
		$(node.template).slideDown();

		// set the replyTo hidden field to the current comment id
		document.getElementById('serendipity_replyTo_'+id).value=id;

		return false;
	}


}
