window.addEvent('domready', function() {
    $$('#bbcode a').addEvent('click', function(event) {
        var t = this.title;
        var textSelected = false; 
        var textarea = $("comment_content"); 
        var begin,selection,end; 
        if (Browser.Engine.gecko){ 
            if (textarea.selectionStart != undefined) {  
                begin = textarea.value.substr(0, textarea.selectionStart);  
                selection = textarea.value.substr(textarea.selectionStart, textarea.selectionEnd - textarea.selectionStart);  
                end = textarea.value.substr(textarea.selectionEnd); 
                if (selection.length > 0){ textSelected = true; } 
            } 
        }else{ 
            if (window.getselection){ selection = window.getselection(); 
            }else if (document.getselection){ selection = document.getselection(); 
            }else if (document.selection){ selection = document.selection.createRange().text; } 
            var startPos = textarea.value.indexOf(selection); 
            if (startPos!= 0){ 
                var endPos = textarea.value.indexOf(selection) + selection.length; 
                begin = textarea.value.substr(0,startPos); 
                end = textarea.value.substr(endPos, textarea.value.length); 
                textSelected = true; 
            } 
        } 
        if(textSelected == true){ 
            switch (t){ 
                case "b": 
                    startTag = "[b]"; 
                    endTag = "[/b]"; 
                    break; 

                case "i":
                    startTag = "[i]";
                    endTag = "[/i]";
                    break;

                case "u":
                    startTag = "[u]";
                    endTag = "[/u]";
                    break;

            }
            textarea.value = begin + startTag + selection + endTag + end;
            textarea.focus();
        }
        event.stop();
    });
});

