/**
* Редактор BB code;
*
* название - FlEditorBBcode;
*
* автор/ы - Кирилл Беляев (kshpigel@gmail.com);
*
* todo:
**/
function FlEditorBBcode(){
    this.classname = null;
    this.textarea = null;
    this.url = null;

    this.width = "auto";
    this.height = "20px";

    this.icons = new Array();
    // Иконки кнопок
    this.icons['button'] = new Array();
    this.icons['button']['b']   = "bold.jpg";
    this.icons['button']['i']   = "italic.jpg";
    this.icons['button']['u']   = "underline.jpg";
    this.icons['button']['url']   = "link.jpg";
    this.icons['button']['img'] = "image.jpg";

    this.initToolsPanel = function(){
        if (this.classname == null) return false;
        if (this.textarea == null) return false;
        if (this.url == null) return false;
        var panel = document.createElement("DIV");
        panel.style.height = this.height;
        panel.style.width = this.width;
        this.textarea.parentNode.insertBefore(panel,this.textarea);
        var elem = null;
        for (i in this.icons['button']){
            elem = document.createElement("IMG");
            elem.src = this.url+"themes/fl_editor_bbcode/images/buttons/"+this.icons['button'][i];
            elem.alt = i;
            elem.style.cursor = "pointer";
            elem.onclick = new Function(this.classname+".formatString('"+i+"')");
            panel.appendChild(elem);
        }
    }
    this.formatString = function(a){
        if (!a) return false;
        if (document.selection){ // IE
            var str = document.selection.createRange().text;
            this.textarea.focus();
            str.text = "["+a+"]"+str+"[/"+a+"]";
            return true;
        }else if(this.textarea.selectionEnd){ // Mozilla
            var strLength = this.textarea.value.length;
            var start = this.textarea.selectionStart;
            var end = this.textarea.selectionEnd;
            var s1 = this.textarea.value.substring(0,start);
            var s2 = this.textarea.value.substring(start,end);
            var s3 = this.textarea.value.substring(end,strLength);
            this.textarea.value = s1+"["+a+"]"+s2+"[/"+a+"]"+s3;
            this.textarea.focus();
        }else{ // no selection
            this.textarea.value += "["+a+"][/"+a+"]";
        }
        return false;
    }
}
