var tbcFonts = new Array();
tbcFonts[0] = {text:"Verdana",value:"Verdana"};
tbcFonts[1] = {text:"Tahoma",value:"Tahoma"};
tbcFonts[2] = {text:"Times New Roman",value:"Times New Roman"};
tbcFonts[3] = {text:"Arial",value:"Arial"};
tbcFonts[4] = {text:"Courier New",value:"Courier New"};

var tbcColors = new Array();
tbcColors[0] = {text:"Черный       " ,value:"Black"};
tbcColors[1] = {text:"Красный      " ,value:"Red"};
tbcColors[2] = {text:"Желтый       " ,value:"Yellow"};
tbcColors[3] = {text:"Розовый      " ,value:"Pink"};
tbcColors[4] = {text:"Зеленый      " ,value:"Green"};
tbcColors[5] = {text:"Оранжевый    " ,value:"Orange"};
tbcColors[6] = {text:"Пурпурный    " ,value:"Purple"};
tbcColors[7] = {text:"Синий        " ,value:"Blue"};
tbcColors[8] = {text:"Бежевый      " ,value:"Beige"};
tbcColors[9] = {text:"Коричневый   " ,value:"Brown"};
tbcColors[10] = {text:"Бирюзовый    " ,value:"Teal"};
tbcColors[11] = {text:"Фиолетовый   " ,value:"Navy"};
tbcColors[12] = {text:"Темно-красный" ,value:"Maroon"};
tbcColors[13] = {text:"Светло-зеленый",value:"LimeGreen"};

var tbcFontSizes = new Array();
tbcFontSizes[0] = {text:"Очень маленький",value:"1"};
tbcFontSizes[1] = {text:"Маленький",value:"2"};
tbcFontSizes[2] = {text:"Средний",value:"3"};
tbcFontSizes[3] = {text:"Большой",value:"4"};
tbcFontSizes[4] = {text:"Очень большой",value:"5"};
tbcFontSizes[5] = {text:"Самый большой",value:"6"};

/*
return "xx-small";
        case 2: return "x-small";
        case 3: return "small";
        case 4: return "medium";
        case 5: return "large";
        case 6: return "x-large";
        case 7: return "xx-large";
        default: return "medium";

 */

function surroundText(text1, text2, textarea)
{
    // Can a text range be created?
    if (typeof(textarea.caretPos) != "undefined" && textarea.createTextRange)
    {
        var caretPos = textarea.caretPos, temp_length = caretPos.text.length;

        caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text1 + caretPos.text + text2 + ' ' : text1 + caretPos.text + text2;

        if (temp_length == 0)
        {
            caretPos.moveStart("character", -text2.length);
            caretPos.moveEnd("character", -text2.length);
            caretPos.select();
        }
        else
            textarea.focus(caretPos);
    }
        // Mozilla text range wrap.
    else if (typeof(textarea.selectionStart) != "undefined")
    {
        var begin = textarea.value.substr(0, textarea.selectionStart);
        var selection = textarea.value.substr(textarea.selectionStart, textarea.selectionEnd - textarea.selectionStart);
        var end = textarea.value.substr(textarea.selectionEnd);
        var newCursorPos = textarea.selectionStart;
        var scrollPos = textarea.scrollTop;

        textarea.value = begin + text1 + selection + text2 + end;

        if (textarea.setSelectionRange)
        {
            if (selection.length == 0)
                textarea.setSelectionRange(newCursorPos + text1.length, newCursorPos + text1.length);
            else
                textarea.setSelectionRange(newCursorPos, newCursorPos + text1.length + selection.length + text2.length);
            textarea.focus();
        }
        textarea.scrollTop = scrollPos;
    }
        // Just put them on the end, then.
    else
    {
        textarea.value += text1 + text2;
        textarea.focus(textarea.value.length - 1);
    }
}


function initCommentToolBar(toolBarId, toolBarId2, textAreaId,notShowButtons) {
    var panelDiv = document.getElementById(toolBarId);
    //var panelDiv2 = document.getElementById(toolBarId2);
    panelDiv.className = "tbcToolbar";
    var  nsb = notShowButtons || {} 
    //panelDiv2.className = "tbcToolbar";
    commentToolBarDrawButton("Жирный текст", "/htmleditor/toolbar/images/bold.gif", panelDiv, "b", textAreaId, false);
    commentToolBarDrawButton("Наклонный текст", "/htmleditor/toolbar/images/italic.gif", panelDiv, "i", textAreaId, false);
    commentToolBarDrawButton("Подчёркнутый текст", "/htmleditor/toolbar/images/under.gif", panelDiv, "u", textAreaId, false);
    commentToolBarDrawButton("Зачёркнутый текст", "/htmleditor/toolbar/images/t.gif", panelDiv, "s", textAreaId, false);
    commentToolBarDrawSeparator(panelDiv);
    commentToolBarDrawButton("Надстрочечный", "/htmleditor/toolbar/images/sup.gif", panelDiv, "sup", textAreaId, false);
    commentToolBarDrawButton("Подсрочечный", "/htmleditor/toolbar/images/sub.gif", panelDiv, "sub", textAreaId, false);
    commentToolBarDrawSeparator(panelDiv);
    //commentToolBarDrawButton("Без выравнивания", "/htmleditor/toolbar/images/w_align.gif", panelDiv, "pre", textAreaId, false);
    //commentToolBarDrawButton("Выравнивание по левому краю", "/htmleditor/toolbar/images/left.gif", panelDiv, "left", textAreaId, false);
    commentToolBarDrawButton("Выравнивание по центру", "/htmleditor/toolbar/images/center.gif", panelDiv, "center", textAreaId, false);
    //commentToolBarDrawButton("Выравнивание по правому краю", "/htmleditor/toolbar/images/right.gif", panelDiv, "right", textAreaId, false);
    commentToolBarDrawSeparator(panelDiv);
    commentToolBarDrawButton("Горизонтальная черта", "/htmleditor/toolbar/images/line.gif", panelDiv, "hr", textAreaId, true);
    //commentToolBarDrawButton("Изображение", "/htmleditor/toolbar/images/image.gif", panelDiv, "img", textAreaId, false);
   /* if (nsb.link == null){
        commentToolBarDrawButton("Ссылка", "/htmleditor/toolbar/images/link.gif", panelDiv, "url", textAreaId, false);
    }*/
    commentToolBarDrawButton("Цитата", "/htmleditor/toolbar/images/forum.gif", panelDiv, "quote", textAreaId, false);
    //commentToolBarDrawSelect(panelDiv2, "font", textAreaId, tbcFonts, "Шрифт");
    commentToolBarDrawSelect(panelDiv, "color", textAreaId, tbcColors, "Цвет шрифта");
//    commentToolBarDrawSelect(panelDiv2, "size", textAreaId, tbcFontSizes, "Размер шрифта");
}

function initCommentToolBarFun(toolBarId, toolBarId2,toolBarId3, textAreaId) {
    var panelDiv = document.getElementById(toolBarId);
    var panelDiv2 = document.getElementById(toolBarId2);
    //var panelDiv3 = document.getElementById(toolBarId3);
    //var panelDiv4 = document.getElementById(toolBarId4);
    panelDiv.className = "tbcToolbar";
    panelDiv2.className = "tbcToolbar";
    //panelDiv3.className = "tbcToolbar";
    commentToolBarDrawButton("Жирный текст", "/htmleditor/toolbar/images/bold.gif", panelDiv, "b", textAreaId, false);
    commentToolBarDrawButton("Наклонный текст", "/htmleditor/toolbar/images/italic.gif", panelDiv, "i", textAreaId, false);
    commentToolBarDrawButton("Подчёркнутый текст", "/htmleditor/toolbar/images/under.gif", panelDiv, "u", textAreaId, false);
    commentToolBarDrawButton("Зачёркнутый текст", "/htmleditor/toolbar/images/t.gif", panelDiv, "s", textAreaId, false);
    commentToolBarDrawSeparator(panelDiv);
    commentToolBarDrawButton("Надстрочечный", "/htmleditor/toolbar/images/sup.gif", panelDiv, "sup", textAreaId, false);
    commentToolBarDrawButton("Подсрочечный", "/htmleditor/toolbar/images/sub.gif", panelDiv, "sub", textAreaId, false);
    commentToolBarDrawSeparator(panelDiv);
    //commentToolBarDrawButton("Без выравнивания", "/htmleditor/toolbar/images/w_align.gif", panelDiv, "pre", textAreaId, false);
    //commentToolBarDrawButton("Выравнивание по левому краю", "/htmleditor/toolbar/images/left.gif", panelDiv, "left", textAreaId, false);
    commentToolBarDrawButton("Выравнивание по центру", "/htmleditor/toolbar/images/center.gif", panelDiv, "center", textAreaId, false);
    //commentToolBarDrawButton("Выравнивание по правому краю", "/htmleditor/toolbar/images/right.gif", panelDiv, "right", textAreaId, false);
    commentToolBarDrawSeparator(panelDiv);
    commentToolBarDrawButton("Горизонтальная черта", "/htmleditor/toolbar/images/line.gif", panelDiv, "hr", textAreaId, true);
    //commentToolBarDrawButton("Изображение", "/htmleditor/toolbar/images/image.gif", panelDiv, "img", textAreaId, false);
    //commentToolBarDrawButton("Ссылка", "/htmleditor/toolbar/images/link.gif", panelDiv, "url", textAreaId, false);
    commentToolBarDrawButton("Цитата", "/htmleditor/toolbar/images/forum.gif", panelDiv, "quote", textAreaId, false);
    //commentToolBarDrawSelect(panelDiv2, "font", textAreaId, tbcFonts, "Шрифт");
    commentToolBarDrawSelect(panelDiv2, "color", textAreaId, tbcColors, "Цвет шрифта");
//    commentToolBarDrawSelect(panelDiv2, "size", textAreaId, tbcFontSizes, "Размер шрифта");
}



function commentToolBarDrawButton(title, src, panelDiv, bbCod, textAreaId, isOne) {
    var button = document.createElement("DIV");
    button.setAttribute("bbcod", bbCod);
    button.setAttribute("tId", textAreaId);
    if (isOne) {
        button.setAttribute("isOne", "true");
    }
    button.className = "tbcButton";
    button.unselectable = true;
    button.onmouseover = commentToolBarButtonMouseEnter;
    button.onmouseout = commentToolBarButtonMouseOut;
    button.onmousedown = commentToolBarButtonMouseDown;
    button.onmouseup = commentToolBarButtonMouseUp;
    button.title = title;
    panelDiv.appendChild(button);
    button.innerHTML = "<IMG class=tbIcon height=22 src='" + src + "' width=23 alt='" + title + "'>";
}

function commentToolBarDrawSelect(panelDiv, bbCod, textAreaId, options, title) {
    var select = document.createElement("SELECT");
    select.setAttribute("bbcod", bbCod);
    select.setAttribute("tId", textAreaId);
    select.unselectable = true;
    panelDiv.appendChild(select);
    var o1 = document.createElement("OPTION");
    try {
        select.options.add(o1);
    } catch (e) {
        select.options[select.options.length] = o1;
    }
    o1.innerHTML = title;
    o1.value = "";

    for (var i = 0; i < options.length; i++) {
        var o = document.createElement("OPTION");
        try {
            select.options.add(o);
        } catch (e) {
            select.options[select.options.length] = o;
        }
        o.innerHTML = options[i].text;
        o.value = options[i].value;
    }
    select.onchange = function(){
        var value = this.value;
        var bbCod = this.getAttribute("bbcod");
        var sBBCod = "["+bbCod+"="+value+"]";
        var eBBCod = "[/"+bbCod+"]";
        surroundText(sBBCod,eBBCod, document.getElementById(this.getAttribute("tId")));
        this.selectedIndex = 0;
    }
}

function commentToolBarDrawSeparator(panelDiv) {
    var s = document.createElement("DIV");
    s.className = "tbcSeparator";
    panelDiv.appendChild(s);
}

function commentToolBarButtonMouseEnter() {
    var b = this;
    if (b.tagName != "DIV") {
        b = b.parentNode;
    }
    b.className = "tbcButtonMouseOver";
}

function commentToolBarButtonMouseOut() {
    var b = this;
    if (b.tagName != "DIV") {
        b = b.parentNode;
    }
    b.className = "tbcButton"
}
function commentToolBarButtonMouseDown() {
    var b = this;
    if (b.tagName != "DIV") {
        b = b.parentNode;
    }
    b.className = "tbcButtonMouseOverDown";
}
function commentToolBarButtonMouseUp() {
    var b = this;
    if (b.tagName != "DIV") {
        b = b.parentNode;
    }
    b.className = "tbcButtonMouseOver";
    var bbCod = b.getAttribute("bbcod");
    var isOne = b.getAttribute("isOne") == "true";
    surroundText("[" + bbCod + "]", !isOne ? "[/" + bbCod + "]" : "", document.getElementById(b.getAttribute("tId")));
}

