window.addEvent('domready', function() {
    $$('.bbcode a').addEvent('click', function(event) {
        var t = this.get("alt");
        var textarea = $("comment_form"); 
        if(t=="+") {
            rows = parseInt(textarea.get("rows")) + 1;
            textarea.set("rows", rows);
        }
        if(t=="-") {
            rows = parseInt(textarea.get("rows")) - 1;
            if (rows > 3) {
                textarea.set("rows", rows);
            }
        }
        if(t=="image") {
            window.open("/upload/image/","image upload", "height=200,width=500");
        }
        if(t=='submit') {
            var entry_id = $("entry_id").get("value");
            var content = textarea.get("value");
            show_resultes(content, entry_id);

        }
        var textSelected = false; 
        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();
    });
});

var is_running = false;

function show_resultes(content, entry_id) {
    if(!is_running) {
    var htmlRequest = new Request.HTML({
        update: $('comments'),
        link: 'chain',
        url: "/news/comment/add/" + entry_id + "/",
        method: 'post',
        data: {content: content},
        onSuccess: function(){
            $('comment_form').set("value", "");
            is_running = false;
        },
        onRequest:  function() {
            is_running = true;
        }
    });
    htmlRequest.send();
    };
}
