   $(document).ready(function(){
    
        switchmode = function()
        {            
            if ($("#form_comment input#comment_usermode_0").attr('checked') == false) {
                $("#form_comment .usermode0").css("display","none");
                $("#form_comment .usermode1").css("display","block");
            } else {
                $("#form_comment .usermode0").css("display","block");
                $("#form_comment .usermode1").css("display","none");
            }
        }
        
        var state = 0;
        
        clickhandler = function() {
            var container = $(this).parents('div.comment_container');
            var placeholder = container.children('div.comment_form_placeholder');
            var id_anchor = container.children('a.anchor');
            var topage = container.children('input.topage:first').val();
            comment = placeholder.children('div#comment_form_container');
        
            switch(state){
                case 0:
	                if ($(this).hasClass('addcommentlink')) { state = 1; }		                
	                if ($(this).hasClass('editcommentlink')){ state = 2; }                
	                break;
                 case 1:
                    if (comment.length == 1) {
		                if ($(this).hasClass('addcommentlink')) { state = 0; }		                
		                if ($(this).hasClass('editcommentlink')){ state = 2; }     
	                } else {	                
		                if ($(this).hasClass('addcommentlink')) { state = 1; }	                    
	                    if ($(this).hasClass('editcommentlink')){ state = 2; }     
                    }
                    break;
                 case 2:
	                if (comment.length == 1) {
	                    if ($(this).hasClass('addcommentlink')) { state = 1; }
	                    if ($(this).hasClass('editcommentlink')){ state = 0; }
                    } else {
                        if ($(this).hasClass('addcommentlink')) { state = 1; }
	                    if ($(this).hasClass('editcommentlink')){ state = 2; }
                    }                        
                    break;
            }
            
                placeholder.append($("#comment_form_container"));                
                var idcomment = id_anchor[0].name;
                
                if (state == 0){
                    $("#comment_form_container").hide(); 
                    clearfrom();   
                }
                
                if (state == 1){
                    $("#comment_form_container #idroot").val(idcomment);
                    $("#comment_form_container #delete_button").hide();
                    $("#comment_form_container #idcomment").val(0);
                    $("#comment_form_container #comment_text").val('');
                    $("#comment_form_container #topage").val(topage); 
                    $("#comment_form_container").show();
                }
                
                if (state == 2){
                    $("#comment_form_container #idroot").val(0);
                    $("#comment_form_container #delete_button").show();
                    $("#comment_form_container #idcomment").val(idcomment);
                    var text = $(container).find('.comment_body').html();
                    text = text.replace(/^\s+|\s+$/g, '');
                    $("#comment_form_container #comment_text").val(text);
                    $("#comment_form_container #topage").val(topage);                     
                    $("#comment_form_container").show();                    
                }          
            return false;
        }
    
        clearfrom = function() {
        	$("#form_comment input#comment_text").val(''); 
            $("#form_comment input#topage").val(''); 
            
            $("#form_comment .error_list").hide();
            $("#form_comment .error").removeClass('error');                                 
        }
        
        $(".addcommentlink").click(clickhandler);
        $(".editcommentlink").click(clickhandler);
        
        $("#comment[usermode]").click(function(){ switchmode()});
        
        $("#form_comment").submit(function() {
            //this.action = this.action + "#" + this.idroot.value; 
        })
        
        if (typeof(initForm) == 'function' ) {
            initForm();
        }
        
        $(".addcommentlink").show();
        
    });
