//////////////////////////////////////////////
// Kasseler CMS: Content Management System  //
// ===========================              //
// Copyright (c)2007-2008 by Igor Ognichenko//
// http://www.kasseler-cms.net/             //
//////////////////////////////////////////////

var abort;
var count = 0;
var ids = new Array();
var blockwidth = 15;
    var blockheight = 15;

var forum_editor = {
    fonts : new Array("Arial", "Sans Serif", "Tahoma", "Verdana", "Courier New", "Georgia", "Times New Roman", "Impact", "Comic Sans MS"),
    font_sizes : new Array("1", "2", "3", "4", "5", "6", "7"),
    font_sizes_px : new Array("8", "10", "12", "14", "18", "24", "36"),
    
    pallete : new Array (
            '#FFFFCC',    '#FFFF66',    '#FFCC66',    '#F2984C',    '#E1771E',    '#B47B10',    '#A9501B',    '#6F3C1B',    '#804000',    '#CC0000',    '#940F04',    '#660000',
            '#C3D9FF',    '#99C9FF',    '#66B5FF',    '#3D81EE',    '#0066CC',    '#6C82B5',    '#32527A',    '#2D6E89',    '#006699',    '#215670',    '#003366',    '#000033',
            '#CAF99B',    '#80FF00',    '#00FF80',    '#78B749',    '#2BA94F',    '#38B63C',    '#0D8F63',    '#2D8930',    '#1B703A',    '#11593C',    '#063E3F',    '#002E3F',
            '#FFBBE8',    '#E895CC',    '#FF6FCF',    '#C94093',    '#9D1961',    '#800040',    '#800080',    '#72179D',    '#6728B2',    '#6131BD',    '#341473',    '#400058',
            '#FFFFFF',    '#E6E6E6',    '#CCCCCC',    '#B3B3B3',    '#999999',    '#808080',    '#7F7F7F',    '#666666',    '#4C4C4C',    '#333333',    '#191919',    '#000000'
    ),
    
    bb_tool_bar_lis : {
        "seperator":      ['seperator',            2,  20],
        "b":              ['Bold',                 20, 20],
        "i":              ['Italic',               20, 20],
        "u":              ['Underline',            20, 20],
        "s":              ['Strikethrough',        20, 20],
        "left":           ['Justifyleft',          20, 20],
        "center":         ['Justifycenter',        20, 20],
        "right":          ['Justifyright',         20, 20],
        "justify":        ['JustifyFull',          20, 20],
        "li":             ['InsertUnorderedList',  20, 20],
        "blockquote":     ['Indent',               20, 20],
        "sub":            ['Subscript',            20, 20],
        "sup":            ['Superscript',          20, 20],
        "hr":             ['InsertHorizontalRule', 20, 20],
        "font":     	  ['SelectFont',           55, 20],
        "size":           ['SelectSize',           53, 20],
        "color":          ['ForeColor',            20, 20],
        "smiles":         ['Smiles',               20, 20],
        "img":            ['InsertImage',          20, 20],
        "link":           ['CreateLink',           20, 20],
        "code":           ['InsertCode',           20, 20],
        "cite":           ['InsertQuote',          20, 20],
        "hide":           ['InsertHide',           20, 20],
        "translit":       ['Translit',             20, 20],
        "attach":         ['Attach',               20, 20],
        "preview":        ['Preview',              20, 20],
        "charset":        ['CharMap',              20, 20],
        "backcolor":      ['BackColor',            20, 20]
    },
    
    bbtoolbar1 : new Array("b", "i", "u", "s", "seperator", "left", "center", "right", "justify", "font", "size", "seperator", "color", "backcolor", "seperator", "li", "seperator", "blockquote", "seperator", "sub", "sup", "seperator", "translit", "seperator", "code", "cite", "hide", "seperator", "preview"),

    rows : 5,
    cols : 12,

    // Images Directory
    patch : 'includes/images/bbimages/',
    // Popups Directory
    popups : 'includes/popups/',
    
    init : function(idElm){
        this.idElm = idElm;
        this.toolbar1bb = '';
        this.SearchButton(this.bbtoolbar1);
	var toolbar = "<table style='margin: 0px; background-image: url("+ this.patch +"background_silver.jpg);' unselectable='on' border='0' cellpadding='0' cellspacing='0' width='100%' class='toolbar_forum'><tr><td><table unselectable='on' border='0' cellpadding='0' cellspacing='0' \><tr>"+this.toolbar1bb+"</tr></table></td></tr></table>"+'<span id="Insert' + idElm + '"></span>';
        document.getElementById(idElm).insertAdjacentHTML("beforeBegin", toolbar, 'toolbar1');
        document.getElementById('preview'+idElm).onclick = function(){
	    var WindowView = window.open('index.php?ajaxed=preview&id='+idElm+'&ajax=true', '', 'left='+center_screen(500, 200, 'left')+',top='+center_screen(500, 200, 'top')+',location=0,status=0,scrollbars=0,resizable=0,width=500,height=200');
	    var content = document.getElementById(idElm).value;
	    return false;
        };
        this.font_selectbb(idElm);
        this.size_selectbb(idElm);
        this.color_selectbb(idElm);
        this.backcolor_selectbb(idElm);
    },
    
    SearchButton : function(bar){
        for (var i = 0; i < bar.length; i++) {
            if (bar[i]) {
                var buttonObj = this.bb_tool_bar_lis[bar[i]];
                var tag = bar[i];
                var buttonID = buttonObj[0];
                var w = buttonObj[1];
                var h = buttonObj[2];
                this.add_tool_button(tag, buttonID, w, h);
            }
        }
    },
    
    ColNav : function(button_name, button_id, w, h){
         if (button_name != 'seperator')
	     var ret = '<td><ul id="menu"><li class="'+button_id+'" id="block'+ button_name + this.idElm +'"><a id="'+ button_name + this.idElm +'" href="#" unselectable="on" onclick="forum_editor.insert_tag(\''+ button_name +'\', \''+this.idElm+'\', \'\', true, \''+button_id+'\'); return false;" title="'+ button_id +'"></a></li></ul></td>';
         else var ret = '<td valign="center" width="5" align="center" ><img width="'+w+'" height="'+h+'" border="0" unselectable="on" src="'+ this.patch + 'seperator.gif" title=""></td>';
	 return ret;
    },
    
    add_tool_button : function(button_name, button_id, w, h){
        var begin = '<table unselectable="on" border="0" cellpadding="0" cellspacing="0"><tr><td><ul id="menu"><li><img width="3" height="20" border="0" style="margin-left: 3px;" src="'+ this.patch +'seperator2.gif"></li></ul></td>';
        this.toolbar1bb = (this.toolbar1bb == '') ? this.toolbar1bb=begin+this.ColNav(button_name, button_id, w, h): this.toolbar1bb+=this.ColNav(button_name, button_id, w, h);
    },
    
    font_selectbb : function(n) {
        var FontSelectDropDown = new Array();
        FontSelectDropDown[n] = '<table unselectable="on" border="0" cellpadding="0" cellspacing="0"><tr><td>';
        FontSelectDropDown[n] += '<span id="Fonts' + n + '" class="dropdown" style="position: absolute; width: 160px; height: 140px;">';
        for (var i = 0; i <= this.fonts.length; i++) {
            if (this.fonts[i]){
                FontSelectDropDown[n] += '<div onclick="forum_editor.insert_tag(\'family\', \''+n+'\', \'' + this.fonts[i] + '\', true, \'\'); return false;" unselectable="on" onMouseOver="this.className=\'hower_button\'" onMouseOut="this.className=\'ower_button\'" class="ower_button" style="width: 130px; cursor: pointer"><font style="font-family:' + this.fonts[i] + '; font-size: 14px;">' + this.fonts[i] + '</font></div>';
            }
        }
        FontSelectDropDown[n] += '</span></td></tr></table>';
        document.getElementById('Insert' + n).insertAdjacentHTML("afterBegin", FontSelectDropDown[n]);
        document.getElementById('font' + n).onclick = function(){forum_editor.ShowElement('Fonts'+n, n, 'font', 53, 0); return false;};
    },

    size_selectbb : function(n){
        var SizeSelectDropDown = new Array;
        SizeSelectDropDown[n] = '<table unselectable="on" border="0" cellpadding="0" cellspacing="0"><tr><td>';
        SizeSelectDropDown[n] += '<span id="Sizes' + n + '" class="dropdown" style="position: absolute; width: 160px; height: 140px;">';
        var sizepx;
        for (var i = 0; i <= this.font_sizes.length-1; i++) {
            switch (this.font_sizes[i]){
                case "1": sizepx = "8"; break;
                case "2": sizepx = "10"; break;
                case "3": sizepx = "12"; break;
                case "4": sizepx = "14"; break;
                case "5": sizepx = "18"; break;
                case "6": sizepx = "24"; break;
                case "7": sizepx = "36"; break;
            }
            if (this.font_sizes[i]) {
                SizeSelectDropDown[n] += '<div onclick="forum_editor.insert_tag(\'size\', \''+n+'\', \'' + sizepx + '\', true, \'\'); return false;" unselectable="on" onMouseOver="this.className=\'hower_button\'" onMouseOut="this.className=\'ower_button\'" class="ower_button" style="width: 130px; cursor: pointer"><font style="font-size: '+sizepx+'px;" size="'+this.font_sizes[i]+'">' + this.font_sizes[i] + '('+sizepx+')</font></div>';
            }
        }
        SizeSelectDropDown[n] += '</span></td></tr></table>';
        document.getElementById('Insert' + n).insertAdjacentHTML("afterBegin", SizeSelectDropDown[n]);
        document.getElementById('size' + n).onclick = function(){forum_editor.ShowElement('Sizes'+n, n, 'size', 55, 0); return false;};
    },

    color_selectbb : function(n){
        var color_index = 0;
        table = new Array();
        table[n] = '<table unselectable="on" border="0" cellpadding="0" cellspacing="0"><tr><td valign="center">';
        table[n] += '<span id="ColorFore' + n + '" style="position: absolute; height: 97px; width: 235px; overflow: hidden; padding: 1px; display: none;">';
        table[n] += '<table unselectable="on" border="0" cellpadding="1" cellspacing="0" align="center" style="border: 1px #000 solid;">';
        for (var i=1, c=0; i<=this.rows; i++){
            table[n] += '<tr>';
            for (var j=1; j<=this.cols; j++){
                table[n] += '<td bgcolor="#CCCCCC"><div onclick="forum_editor.insert_tag(\'color\', \''+n+'\', \''+this.pallete[color_index]+'\', true, \'\'); return false;" unselectable="on" onmousemove="this.style.border = \'1px #FFFFCC solid\'" onmouseout="this.style.border = \'1px #000000 solid\'" style="font-size: 4px; cursor: pointer; background:'+this.pallete[color_index]+'; float: left; border: 1px #000000 solid; width:'+blockwidth+'px; height:'+blockheight+'px;"><img border="0" src="'+this.patch+'backbroundpalete.gif" alt="" title=""></div></td>';
                color_index++;
            }
            table[n] += '</tr>';
        }
        table[n] += '</table></span></td></tr></table>';
        document.getElementById('Insert' + n).insertAdjacentHTML("afterBegin", table[n]);
        document.getElementById('color' + n).onclick = function(){forum_editor.ShowElement('ColorFore'+n, n, 'color', 20, 0); return false;};
    },

    backcolor_selectbb : function(n){
        var color_index = 0;
        table = new Array();
        table[n] = '<table unselectable="on" border="0" cellpadding="0" cellspacing="0"><tr><td valign="center">';
        table[n] += '<span id="ColorBack' + n + '" style="position: absolute; height: 97px; width: 235px; overflow: hidden; padding: 1px; display: none;">';
        table[n] += '<table unselectable="on" border="0" cellpadding="1" cellspacing="0" align="center" style="border: 1px #000 solid;">';
        for (var i=1, c=0; i<=this.rows; i++){
            table[n] += '<tr>';
            for (var j=1; j<=this.cols; j++){
                table[n] += '<td bgcolor="#CCCCCC"><div onclick="forum_editor.insert_tag(\'backcolor\', \''+n+'\', \''+this.pallete[color_index]+'\', true, \'\'); return false;" unselectable="on" onmousemove="this.style.border = \'1px #FFFFCC solid\'" onmouseout="this.style.border = \'1px #000000 solid\'" style="font-size: 4px; cursor: pointer; background:'+this.pallete[color_index]+'; float: left; border: 1px #000000 solid; width:'+blockwidth+'px; height:'+blockheight+'px"><img border="0" src="'+this.patch+'backbroundpalete.gif" alt="" title=""></div></td>';
                color_index++;
            }
            table[n] += '</tr>';
        }
        table[n] += '</table></span></td></tr></table>';
        document.getElementById('Insert' + n).insertAdjacentHTML("afterBegin", table[n]);
        document.getElementById('backcolor' + n).onclick = function(){forum_editor.ShowElement('ColorBack'+n, n, 'backcolor', 20, 0); return false;};
    },
    
    ShowElement : function (Obg, n, button, ButtonWidth, MarginLeft){
        var elem = document.getElementById(Obg);
        var Pleft = find_pos(document.getElementById(button + n), 'left');
        var Ptop = find_pos(document.getElementById(button + n), 'top');
        var vc = document.getElementsByTagName((document.compatMode && document.compatMode=="CSS1Compat") ? "HTML":"BODY")[0];
        var vcwidth = vc.clientWidth ? vc.clientWidth + vc.scrollLeft : window.innerWidth + window.pageXOffset;
        var vcheight = vc.innerHeight ? window.innerHeight + window.pageYOffset : vc.clientHeight + vc.scrollTop;
        Ptop = (((Ptop + 22) > vcheight)) ? Ptop - 48 : Ptop;
        Pleft = (((Pleft) > vcwidth)) ? Pleft - ((Pleft)-vcwidth) : Pleft;
        if (elem.style.display == 'block') hidden_selectbb(n);
        else {
            hidden_selectbb(n);
            elem.style.display = 'block';
        }
        elem.style.left = (Pleft-MarginLeft)+'px';
        elem.style.top = (Ptop+20)+'px';
        document.onclick = function(){
            abort = window.setTimeout("hidden_selectbb('"+n+"');", 10000);
        };
        elem.onmousemove = function(){
            clearTimeout(abort);
        };
    },
    
    insert_tag : function(element_id, id, pramerttag, closetag, tagname){
        if (element_id!='smiles' && element_id!='link' && element_id!='attach' && element_id!='translit' && element_id!='charset'){
            var tag = element_id;
            var txta = document.getElementById(id);
            txta.focus();
            pramerttag = (pramerttag) ? '='+pramerttag : "";
            if (typeof document.selection != 'undefined') { //Opera and MSIE
                var range = document.selection.createRange();
                var sel = range.text;
                if (closetag!=false){
                    var slash;
	            if (eval("this."+ tag +"_open")==1 && !sel.length){
	                slash = "/";
                        pramerttag = "";
	                eval("this."+ tag + "_open = 0");
	                if (tagname)document.getElementById('block'+element_id+id).className = tagname;
                    } else if(!sel.length){
	                slash = "";
	                eval("this."+ tag + "_open = 1");
	                if (tagname)document.getElementById('block'+element_id+id).className = tagname+'click';
	            }
	            range.text = (sel.length) ? "[" + tag + pramerttag + "]" + sel + "[/" + tag + "]" : "["+ slash + tag + pramerttag + "]";
	        } else range.text = "["+ tag + "]";
                range = document.selection.createRange();
                range.select();
            } else if (typeof txta.selectionStart != 'undefined') {   //Mozzila
                var scroll = txta.scrollTop;
                var start  = txta.selectionStart;
                var end    = txta.selectionEnd;
                var before = txta.value.substring(0, start);
                var sel    = txta.value.substring(start, end);
                var after  = txta.value.substring(end, txta.textLength);
                var caret = sel.length == 0 ? start + tag.length + 2 : start + tag.length + 2 + sel.length + tag.length + 3;
                if (closetag!=false){
                    var slash;
                    if (eval("this."+ tag +"_open")==1 && start==end){
	                slash = "/";
	                eval("this."+ tag + "_open = 0");
                        caret++;
                        pramerttag = "";
                        if (tagname) document.getElementById('block'+element_id+id).className = tagname;
                    } else if(start==end){
	                slash = "";
	                eval("this."+ tag + "_open = 1");
	                caret = (pramerttag) ? caret+pramerttag.length : caret;
	                if (tagname) document.getElementById('block'+element_id+id).className = tagname+'click';
	            }
                    txta.value = (start!=end) ? before + "[" + tag + pramerttag + "]" + sel + "[/" + tag + "]" + after : before + "["+ slash + tag + pramerttag + "]" + after;
                } else txta.value = before + "[" + tag + "]" + after;
                txta.selectionStart = caret;
                txta.selectionEnd = caret;
                txta.scrollTop = scroll;
            }
        } else if (element_id=='link'){
            var WindowLink = window.open(this.popups+'insert_hyperlinkbb.html?editor='+id, '', 'left='+center_screen(300, 135, 'left')+',top='+center_screen(300, 135, 'top')+',location=0,status=0,scrollbars=0,resizable=0,width=300,height=135');
            } else if (element_id=='translit'){
                var WindowTranslit = window.open(this.popups+'convert_translitbb.html?editor='+id, '', 'left='+center_screen(520, 390, 'left')+',top='+center_screen(520, 390, 'top')+',location=0,status=0,scrollbars=0,resizable=0,width=520,height=390');
                } else if (element_id=='charset'){
                    var WindowCharset = window.open(this.popups+'insert_charsetbb.html?editor='+id, '', 'left='+center_screen(490, 400, 'left')+',top='+center_screen(490, 400, 'top')+',location=0,status=0,scrollbars=0,resizable=0,width=490,height=400');
                }
                hidden_selectbb(id);
    }
}

function hidden_selectbb(n){
    clearTimeout(abort);
    document.getElementById('Fonts' + n).style.display = 'none';
    document.getElementById('Sizes' + n).style.display = 'none';
    document.getElementById('ColorFore' + n).style.display = 'none';
    document.getElementById('ColorBack' + n).style.display = 'none';
    document.onclick = function(){}
}

find_pos = function(obj, type_pos){
    var pos = new Array();
    if (obj.offsetParent) {
        pos[0] = obj.offsetTop
        pos[1] = obj.offsetLeft
        while (obj = obj.offsetParent) {
            pos[0] += obj.offsetTop
            pos[1] += obj.offsetLeft
        }
    }
    if (type_pos=='left') return pos[1];
    if (type_pos=='top') return pos[0];
}

function InsertTag(tag, n){
    var txta = document.getElementById(n);
    txta.focus();
    if (typeof document.selection != 'undefined') {
        var range = document.selection.createRange();
        var sel = range.text;
        range.text = tag;
        range = document.selection.createRange();
        range.select();
    } else if (typeof txta.selectionStart != 'undefined'){
        var scroll = txta.scrollTop;
        var start  = txta.selectionStart;
        var end    = txta.selectionEnd;
        var before = txta.value.substring(0, start);
        var sel    = txta.value.substring(start, end);
        var after  = txta.value.substring(end, txta.textLength);
        txta.value = before + tag + after;
        var caret = sel.length == 0 ? start + tag.length  : start + tag.length + sel.length + tag.length;
        txta.selectionStart = caret;
        txta.selectionEnd = caret;
        txta.scrollTop = scroll;
    }
    hidden_selectbb(n);
}
