var theSelection = false;

var clientPC = navigator.userAgent.toLowerCase(); // Get client info
var clientVer = parseInt(navigator.appVersion); // Get browser version

var is_ie = ((clientPC.indexOf("msie") != -1) && (clientPC.indexOf("opera") == -1));
var is_nav = ((clientPC.indexOf('mozilla')!=-1) && (clientPC.indexOf('spoofer')==-1)
    && (clientPC.indexOf('compatible') == -1) && (clientPC.indexOf('opera')==-1)
    && (clientPC.indexOf('webtv')==-1) && (clientPC.indexOf('hotjava')==-1));
var is_moz = 0;

var is_win = ((clientPC.indexOf("win")!=-1) || (clientPC.indexOf("16bit") != -1));
var is_mac = (clientPC.indexOf("mac")!=-1);

var baseHeight = 0;
var toAdd = "";

var formName = "";
var elementName = "";
/**
 * @deprecated, to insert BBCode text call function with params instead…
 */
function initBBCode(form_name, element_name) {
    alert("@deprecated, to insert BBCode text call function with params instead…");
    formName = form_name;
    elementName = element_name;
}

function initInsertions() {
    elementId ? document.getElementById(elementName + elementId).focus() : document.forms[formName].elements[elementName].focus();
    if (is_ie && typeof(baseHeight) != 'number') baseHeight = document.selection.createRange().duplicate().boundingHeight;
}

function emoticon(text, el) {
    setText(" " + text + " ", el);
}

function setText(text, el){
    var obj = grObj(el);
    obj.focus();
    if(document.selection) {
        var s = document.selection.createRange();
        s.text = text;
        s.select();
    } else if(typeof(obj.selectionStart) == "number") {
        var start = obj.selectionStart;
        var end = obj.selectionEnd;
        obj.value = obj.value.substr(0, start) + text + obj.value.substr(end);
        obj.setSelectionRange(end + text.length, end + text.length);
    } else {
        obj.value += text;
    }
    return false;
}

function setWrap(open, close, el){
    var obj = grObj(el);
    obj.focus();
    if(document.selection) {
        var s = document.selection.createRange();
        s.text = open + s.text + close;
        s.select();
    } else if(typeof(obj.selectionStart) == "number") {
        var start = obj.selectionStart;
        var end = obj.selectionEnd;
        var text = obj.value.substring(start, end);
        text = open + text + close;
        obj.value = obj.value.substr(0, start) + text + obj.value.substr(end);
        obj.setSelectionRange(start + text.length, start + text.length);
    } else {
        obj.value += open+close;
    }
    return false;
}

function BBCbold(el) {
    var obj = grObj(el);
    if((document.selection && document.selection.createRange().text != "") || (typeof(obj.selectionStart) == "number" && obj.selectionStart < obj.selectionEnd)) {
        setWrap("[b]", "[/b]", el);
    } else {
        var bold = document.getElementById("bold"+el);
        if (bold.src.match("bold.gif")) {
            toAdd = "[b]";
            bold.src = "/img/forum/bold1.gif";
        } else {
            toAdd = "[/b]";
            bold.src = "/img/forum/bold.gif";
        }
        setText(toAdd, el);
    }
}

function BBCitalic(el) {
    var obj = grObj(el);
    if((document.selection && document.selection.createRange().text != "") || (typeof(obj.selectionStart) == "number" && obj.selectionStart < obj.selectionEnd)) {
        setWrap("[i]","[/i]", el);
    } else {
        var italic = document.getElementById("italic"+el);
        if (italic.src.match("italic.gif")) {
            toAdd = "[i]";
            italic.src = "/img/forum/italic1.gif";
        } else {
            toAdd = "[/i]";
            italic.src = "/img/forum/italic.gif";
        }
        setText(toAdd, el);
    }
}

function BBCunder(el) {
    var obj = grObj(el);
    if((document.selection && document.selection.createRange().text != "") || (typeof(obj.selectionStart) == "number" && obj.selectionStart < obj.selectionEnd)) {
        setWrap("[u]","[/u]", el);
    } else {
        var under = document.getElementById("under"+el);
        if (under.src.match("under.gif")) {
            toAdd = "[u]";
            under.src = "/img/forum/under1.gif";
        } else {
            toAdd = "[/u]";
            under.src = "/img/forum/under.gif";
        }
        setText(toAdd, el);
    }
}

function BBCquote(el) {
    var obj = grObj(el);
    if((document.selection && document.selection.createRange().text != "") || (typeof(obj.selectionStart) == "number" && obj.selectionStart < obj.selectionEnd)) {
        setWrap("[quote]","[/quote]", el);
    } else {
        var quote = document.getElementById("quote"+el);
        if (quote.src.match("quote.gif")) {
            toAdd = "[quote]";
            quote.src = "/img/forum/quote1.gif";
        } else {
            toAdd = "[/quote]";
            quote.src = "/img/forum/quote.gif";
        }
        setText(toAdd, el);
    }
}

function BBCul(el) {
    var obj = grObj(el);
    if((document.selection && document.selection.createRange().text != "") || (typeof(obj.selectionStart) == "number" && obj.selectionStart < obj.selectionEnd)) {
        setWrap("\n[ul]","[/ul]\n", el);
    } else {
        var ul = document.getElementById("ul"+el);
        if (ul.src.match("ul.gif")) {
            toAdd = "\n[ul]";
            ul.src = "/img/forum/ul1.gif";
        } else {
            toAdd = "[/ul]\n";
            ul.src = "/img/forum/ul.gif";
        }
        setText(toAdd, el);
    }
}

function BBCol(el) {
    var obj = grObj(el);
    if((document.selection && document.selection.createRange().text != "") || (typeof(obj.selectionStart) == "number" && obj.selectionStart < obj.selectionEnd)) {
        setWrap("\n[ol]","\n[/ol]", el);
    } else {
        var ol = document.getElementById("ol"+el);
        if (ol.src.match("ol.gif")) {
            toAdd = "\n[ol]";
            ol.src = "/img/forum/ol1.gif";
        } else {
            toAdd = "[/ol]\n";
            ol.src = "/img/forum/ol.gif";
        }
        setText(toAdd, el);
    }
}

function BBCli(el) {
    setText("[li]", el);
}

function BBCparagraph(el) {
    var obj = grObj(el);
    if((document.selection && document.selection.createRange().text != "") || (typeof(obj.selectionStart) == "number" && obj.selectionStart < obj.selectionEnd)) {
        setWrap("[p]","\n", el);
    } else {
        setText("\n[p]", el);
    }
/*
    var obj = document.forms[formName].elements[elementName];
    if((document.selection && document.selection.createRange().text != "") || (typeof(obj.selectionStart) == "number" && obj.selectionStart < obj.selectionEnd)) {
    } else {
        var paragraph = document.getElementById("paragraph"+el);
        if (paragraph.src.match("p.gif")) {
            toAdd = "[p]";
            paragraph.src = "/img/forum/p1.gif";
        } else {
            toAdd = "[/p]";
            paragraph.src = "/img/forum/p.gif";
        }
        setText(toAdd);
    }
    */
}

function BBCurl(el) {
    var obj = grObj(el);
    if((document.selection && document.selection.createRange().text != "") || (typeof(obj.selectionStart) == "number" && obj.selectionStart < obj.selectionEnd)) {
        setWrap("[url=\"\u0410\u0434\u0440\u0435\u0441 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b\"]","[/url]", el);
    } else {
        toAdd = "[url=\"\u0410\u0434\u0440\u0435\u0441 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b\"]\u0422\u0435\u043a\u0441\u0442 \u0441\u0441\u044b\u043b\u043a\u0438[/url]";
        setText(toAdd, el);
    }
}

function BBCurld(el) {
    var obj = grObj(el);
    if((document.selection && document.selection.createRange().text != "") || (typeof(obj.selectionStart) == "number" && obj.selectionStart < obj.selectionEnd)) {
        setWrap("[urld=\"\u0410\u0434\u0440\u0435\u0441 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b \u043f\u0440\u044f\u043c\u043e\u0439 \u0441\u0441\u044b\u043b\u043a\u0438\"]","[/urld]", el);
    } else {
        toAdd = "[urld=\"\u0410\u0434\u0440\u0435\u0441 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b \u043f\u0440\u044f\u043c\u043e\u0439 \u0441\u0441\u044b\u043b\u043a\u0438\"]\u0422\u0435\u043a\u0441\u0442 \u043f\u0440\u044f\u043c\u043e\u0439 \u0441\u0441\u044b\u043b\u043a\u0438[/urld]";
        setText(toAdd, el);
    }
}

function BBCimg(el) {
    setText("\n[img]\u0410\u0434\u0440\u0435\u0441 \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0438[/img]\n", el);
}

function BBCTextInsert(url, alt, title, link, el, d){
    alt = alt.replace(/"/g,'');
    title = title.replace(/"/g,'');
    var altT = ' alt="' + ( alt ? alt : title ) + '"', text = title.length > 0 ? "[span][img" + altT + "]" + url + "[/img]" + title + "[/span]" : "[img" + altT + "]" + url + "[/img]";
    if (link.length > 0) {
        text = '[url="'+link+'"]'+text+'[/url]';
    }
    if (!jQuery.browser.msie) {
        setText(text + '\n', el);
        d.dialog('close');
    } else {
        d.dialog('close');
        //var pos = parseInt($("#caretPos"+el).val());
        //$("#bbc" + el).caret(pos);
        setText(text + '\n', el);
    /*
        var before = obj.value.substr(0, pos);
        var after = obj.value.substr(pos);
        //alert(before + "\n" + pos);
        //$(obj).caret(pos).css("border", "solid 1px red");
        //setText(text + '\n');
        $(obj).html('\n' + before + text + '\n' + after)
            //.css("border", "solid 1px red")
            .caret(text);
        */
    }
    return false;
}

function BBCyoutube(el) {
    setText("\n[youtube]youtube video id[/youtube]\n", el);
}

function BBCvsvideo(el) {
    setText("\n[vsvideo]ÐšÐ¾Ð´ Ð²Ð¸Ð´ÐµÐ¾[/vsvideo]\n", el);
}

function initCaretPos(el) {
    jQuery("#bbc" + el)
    .blur(function(){
        jQuery("#caretPos"+el).val(jQuery(this).caret().end)
        })
    .focus(function(){
        jQuery("#caretPos"+el).val(jQuery(this).caret().end)
        })
    .mousemove(function(){
        jQuery("#caretPos"+el).val(jQuery(this).caret().end)
        })
}

function linkToggle(obj){
    var el = "#" + jQuery(obj).attr('id').replace(/linkCbCheck/,"linkCbP");
    // alert(el);
    jQuery(obj).attr('checked') ? jQuery(el).show() : jQuery(el).hide();
}

function grObj(el){
    var obj = document.getElementById("bbc" + el);
    /*
    if (!obj || obj =='undefined') {
        obj = document.forms[formName];
        if (!obj || obj == 'undefined' || !document.forms[formName].elements[elementName] || document.forms[formName].elements[elementName]=='undefined') {
            obj = jQuery("form[name="+formName+"] *[name=" + elementName + "]").get(0);
        } else {
            obj = document.forms[formName].elements[elementName];
        }
    }
    */
    return obj;
}
