// Define global variables.
AbEditorGlobal = new Object();
AbEditorGlobal.uploadDirectory = '/data';
AbEditorGlobal.uploadDirectoryURL = '/data';
AbEditorGlobal.iconPath = '/img/common/ab_editor/icons';
AbEditorGlobal.spacerPath = '/img/common/ab_editor/spacer.gif';
AbEditorGlobal.fileUploadURL = '/admin/abeupload/index'
AbEditorGlobal.fileListURL = '/common/abefilelist.html';
AbEditorGlobal.fileDelURL = '/common/abedelete.html'
AbEditorGlobal.useFileUpload = 1;
AbEditorGlobal.maxFileWidth  = 800;
AbEditorGlobal.maxFileHeight = 800;
AbEditorGlobal.maxFileSize   = 10240000;

// Declaring the AbEditor Class.
var AbEditor = Class.create();

// Defining the rest of the class implementation.
AbEditor.prototype = {

    // Constructor
    initialize: function (id) {
        this.id = id;
        this.editor = id + '_editor';
        this.preview = id + '_preview';
        this.tableInfo = id + '_table_info';
        this.colorPallet = id + '_color_pallet';
        this.imageUploader = id + '_image_uploader';
        this.fileList = id + '_file_list';
    },

    // Show Editor
    showEditor: function () {

        var src = '';
        src += '<div class="ab_editor">';
        src += '<ul class="abe_icon">';
        src += '<li><img src="' + AbEditorGlobal.iconPath + '/text_padding_top.png" title="見出し" onclick="Abe.appendTag(\'[H]\', \'[/H]\')" /></li>';
        src += '<li><img src="' + AbEditorGlobal.iconPath + '/text_heading_1.png" title="超特大文字" onclick="Abe.appendTag(\'[XXL]\', \'[/XXL]\')" /></li>';
        src += '<li><img src="' + AbEditorGlobal.iconPath + '/text_heading_2.png" title="特大文字" onclick="Abe.appendTag(\'[XL]\', \'[/XL]\')" /></li>';
        src += '<li><img src="' + AbEditorGlobal.iconPath + '/text_heading_3.png" title="大文字" onclick="Abe.appendTag(\'[L]\', \'[/L]\')" /></li>';
        src += '<li><img src="' + AbEditorGlobal.iconPath + '/text_heading_4.png" title="小文字" onclick="Abe.appendTag(\'[S]\', \'[/S]\')" /></li>';
        src += '<li><img src="' + AbEditorGlobal.iconPath + '/text_bold.png" title="太字" onclick="Abe.appendTag(\'[B]\', \'[/B]\')" /></li>';
        src += '<li><img src="' + AbEditorGlobal.iconPath + '/text_italic.png" title="斜体" onclick="Abe.appendTag(\'[I]\', \'[/I]\')" /></li>';
        src += '<li><img src="' + AbEditorGlobal.iconPath + '/text_underline.png" title="下線" onclick="Abe.appendTag(\'[U]\', \'[/U]\')" /></li>';
        src += '<li><img src="' + AbEditorGlobal.iconPath + '/text_strikethrough.png" title="消線" onclick="Abe.appendTag(\'[DEL]\', \'[/DEL]\')" /></li>';
        src += '<li><img src="' + AbEditorGlobal.iconPath + '/text_align_left.png" title="左揃え" onclick="Abe.appendTag(\'[AL]\', \'[/AL]\')" /></li>';
        src += '<li><img src="' + AbEditorGlobal.iconPath + '/text_align_center.png" title="中央揃え" onclick="Abe.appendTag(\'[AC]\', \'[/AC]\')" /></li>';
        src += '<li><img src="' + AbEditorGlobal.iconPath + '/text_align_right.png" title="右揃え" onclick="Abe.appendTag(\'[AR]\', \'[/AR]\')" /></li>';
        src += '<li><img src="' + AbEditorGlobal.iconPath + '/color_swatch.png" title="色" onclick="Abe.toggleShowHideColorPallet();" /></li>';
        src += '<li><img src="' + AbEditorGlobal.iconPath + '/world_link.png" title="リンク" onclick="Abe.appendLinkTag();" /></li>';
        src += '<li><img src="' + AbEditorGlobal.iconPath + '/layout_delete.png" title="タグ削除" onclick="Abe.removeTags();" /></li>';
        if (AbEditorGlobal.useFileUpload == 1) {
            var js_str = "var info = window.open('" + AbEditorGlobal.fileUploadURL;
            js_str += "?id=" + this.id;
            js_str += "&path=" + AbEditorGlobal.uploadDirectory;
            js_str += "&mw=" + AbEditorGlobal.maxFileWidth;
            js_str += "&mh=" + AbEditorGlobal.maxFileHeight;
            js_str += "&ms=" + AbEditorGlobal.maxFileSize;
            js_str += "', 'abefileupload', 'width=400,height=220,scrollbars=no,toolbar=no,resizable=no,left=350,top=100'); info.focus();";

            src += '<li><img src="' + AbEditorGlobal.iconPath + '/picture.png" title="画像アップロード" onclick="' + js_str + '" /></li>';
        }
        src += '<li><img src="' + AbEditorGlobal.iconPath + '/text_horizontalrule.png" title="線" onclick="Abe.appendHrTag();" /></li>';
        src += '<li><img src="' + AbEditorGlobal.iconPath + '/table_add.png" title="テーブル" onclick="Abe.toggleShowHideTableInfo();" /></li>';

        src += '</ul>';

        src += '<table id="' + this.tableInfo + '" style="display: none;">';
        src += '<tr>';
        src += '<td>行<input type="text" id="' + this.id + '_rows" value="1" size="2" /> × 列<input type="text" id="' + this.id + '_cols" value="2" size="2" />';
        src += '</td>';
        src += '</tr>';
        src += '<tr>';
        src += '<td><input type="checkbox" id="' + this.id + '_border" value="1" />枠線を付ける';
        src += '</td>';
        src += '</tr>';
        src += '<tr>';
        src += '<td><input type="button" value="OK" onclick="Abe.appendTableTag(\'' + this.id + '_rows\', \'' + this.id + '_cols\', \'' + this.id + '_border\');" />';
        src += '</td>';
        src += '</tr>';
        src += '</table>';

        src += '<table id="' + this.colorPallet + '" class="abe_color_pallet" style="display: none;">';
        src += '<tr>';

        src += '<td style="background-color: #000;"><img src="' + AbEditorGlobal.spacerPath + '" title="#000" onclick="Abe.appendColorTag(\'000\');" /></td>';
        src += '<td style="background-color: #00f;"><img src="' + AbEditorGlobal.spacerPath + '" title="#00f" onclick="Abe.appendColorTag(\'00f\');" /></td>';

        src += '<td style="background-color: #030;"><img src="' + AbEditorGlobal.spacerPath + '" title="#030" onclick="Abe.appendColorTag(\'030\');" /></td>';
        src += '<td style="background-color: #03f;"><img src="' + AbEditorGlobal.spacerPath + '" title="#03f" onclick="Abe.appendColorTag(\'03f\');" /></td>';

        src += '<td style="background-color: #060;"><img src="' + AbEditorGlobal.spacerPath + '" title="#060" onclick="Abe.appendColorTag(\'060\');" /></td>';
        src += '<td style="background-color: #06f;"><img src="' + AbEditorGlobal.spacerPath + '" title="#06f" onclick="Abe.appendColorTag(\'06f\');" /></td>';

        src += '<td style="background-color: #090;"><img src="' + AbEditorGlobal.spacerPath + '" title="#090" onclick="Abe.appendColorTag(\'090\');" /></td>';
        src += '<td style="background-color: #09f;"><img src="' + AbEditorGlobal.spacerPath + '" title="#09f" onclick="Abe.appendColorTag(\'09f\');" /></td>';

        src += '<td style="background-color: #0c0;"><img src="' + AbEditorGlobal.spacerPath + '" title="#0c0" onclick="Abe.appendColorTag(\'0c0\');" /></td>';
        src += '<td style="background-color: #0cf;"><img src="' + AbEditorGlobal.spacerPath + '" title="#0cf" onclick="Abe.appendColorTag(\'0cf\');" /></td>';

        src += '<td style="background-color: #0f0;"><img src="' + AbEditorGlobal.spacerPath + '" title="#0f0" onclick="Abe.appendColorTag(\'0f0\');" /></td>';
        src += '<td style="background-color: #0ff;"><img src="' + AbEditorGlobal.spacerPath + '" title="#0ff" onclick="Abe.appendColorTag(\'0ff\');" /></td>';

        src += '</tr>';

        src += '<tr>';

        src += '<td style="background-color: #900;"><img src="' + AbEditorGlobal.spacerPath + '" title="#900" onclick="Abe.appendColorTag(\'900\');" /></td>';
        src += '<td style="background-color: #90f;"><img src="' + AbEditorGlobal.spacerPath + '" title="#90f" onclick="Abe.appendColorTag(\'90f\');" /></td>';

        src += '<td style="background-color: #930;"><img src="' + AbEditorGlobal.spacerPath + '" title="#930" onclick="Abe.appendColorTag(\'930\');" /></td>';
        src += '<td style="background-color: #93f;"><img src="' + AbEditorGlobal.spacerPath + '" title="#93f" onclick="Abe.appendColorTag(\'93f\');" /></td>';

        src += '<td style="background-color: #960;"><img src="' + AbEditorGlobal.spacerPath + '" title="#960" onclick="Abe.appendColorTag(\'960\');" /></td>';
        src += '<td style="background-color: #96f;"><img src="' + AbEditorGlobal.spacerPath + '" title="#96f" onclick="Abe.appendColorTag(\'96f\');" /></td>';

        src += '<td style="background-color: #990;"><img src="' + AbEditorGlobal.spacerPath + '" title="#990" onclick="Abe.appendColorTag(\'990\');" /></td>';
        src += '<td style="background-color: #99f;"><img src="' + AbEditorGlobal.spacerPath + '" title="#99f" onclick="Abe.appendColorTag(\'99f\');" /></td>';

        src += '<td style="background-color: #9c0;"><img src="' + AbEditorGlobal.spacerPath + '" title="#9c0" onclick="Abe.appendColorTag(\'9c0\');" /></td>';
        src += '<td style="background-color: #9cf;"><img src="' + AbEditorGlobal.spacerPath + '" title="#9cf" onclick="Abe.appendColorTag(\'9cf\');" /></td>';

        src += '<td style="background-color: #9f0;"><img src="' + AbEditorGlobal.spacerPath + '" title="#9f0" onclick="Abe.appendColorTag(\'9f0\');" /></td>';
        src += '<td style="background-color: #9ff;"><img src="' + AbEditorGlobal.spacerPath + '" title="#9ff" onclick="Abe.appendColorTag(\'9ff\');" /></td>';

        src += '</tr>';
        src += '<tr>';

        src += '<td style="background-color: #f00;"><img src="' + AbEditorGlobal.spacerPath + '" title="#f00" onclick="Abe.appendColorTag(\'f00\');" /></td>';
        src += '<td style="background-color: #f0f;"><img src="' + AbEditorGlobal.spacerPath + '" title="#f0f" onclick="Abe.appendColorTag(\'f0f\');" /></td>';

        src += '<td style="background-color: #f30;"><img src="' + AbEditorGlobal.spacerPath + '" title="#f30" onclick="Abe.appendColorTag(\'f30\');" /></td>';
        src += '<td style="background-color: #f3f;"><img src="' + AbEditorGlobal.spacerPath + '" title="#f3f" onclick="Abe.appendColorTag(\'f3f\');" /></td>';

        src += '<td style="background-color: #f60;"><img src="' + AbEditorGlobal.spacerPath + '" title="#f60" onclick="Abe.appendColorTag(\'f60\');" /></td>';
        src += '<td style="background-color: #f6f;"><img src="' + AbEditorGlobal.spacerPath + '" title="#f6f" onclick="Abe.appendColorTag(\'f6f\');" /></td>';

        src += '<td style="background-color: #f90;"><img src="' + AbEditorGlobal.spacerPath + '" title="#f90" onclick="Abe.appendColorTag(\'f90\');" /></td>';
        src += '<td style="background-color: #f9f;"><img src="' + AbEditorGlobal.spacerPath + '" title="#f9f" onclick="Abe.appendColorTag(\'f9f\');" /></td>';

        src += '<td style="background-color: #fc0;"><img src="' + AbEditorGlobal.spacerPath + '" title="#fc0" onclick="Abe.appendColorTag(\'fc0\');" /></td>';
        src += '<td style="background-color: #fcf;"><img src="' + AbEditorGlobal.spacerPath + '" title="#fcf" onclick="Abe.appendColorTag(\'fcf\');" /></td>';

        src += '<td style="background-color: #ff0;"><img src="' + AbEditorGlobal.spacerPath + '" title="#ff0" onclick="Abe.appendColorTag(\'ff0\');" /></td>';
        src += '<td style="background-color: #fff;"><img src="' + AbEditorGlobal.spacerPath + '" title="#fff" onclick="Abe.appendColorTag(\'fff\');" /></td>';

        src += '</tr>';

        src += '</table>';
        src += '</div>';

        $(this.editor).innerHTML = src;

        var field = $(this.id);
        this.parse();
        Event.observe(field, 'focus', this.parse.bind(this));
        Event.observe(field, 'click', this.parse.bind(this));
        Event.observe(field, 'keyup', this.parse.bind(this));

//         var icons = $(this.editor).getElementsByTagName('li');
//         for (var i = 0; i < icons.length; ++i) {
//             icons[i].onmousedown = this.iconmousedown;
//             icons[i].onmouseup = this.iconmouseup;
//             icons[i].onmouseover = this.iconmouseover;
//             icons[i].onmouseout = this.iconmouseout;
//         }
    },

    iconmousedown: function (event) {
        this.firstChild.style.left = '2em';
        this.firstChild.style.top = '2em';
        this.style.border = 'inset 2px';
    },

    iconmouseup: function (event) {
        this.firstChild.style.left = '1em';
        this.firstChild.style.top = '1em';
        this.style.border = 'outset 2px';
    },

    iconmouseover: function (event) {
        this.style.border = 'outset 2px';
    },

    iconmouseout: function (event) {
        this.style.border = 'solid 2px #c0c0c0';
    },

    // Parse the text and change it to HTML
    parse: function (event) {
        var text = $(this.id).value;
        $(this.preview).innerHTML = AbEditorParser.parse(text);
    },

    // Parse the text and change it to HTML
    parse2: function (event) {
        var text = $(this.id).innerHTML;
        $(this.id).innerHTML = AbEditorParser.parse(text);
    },

    // Append certein ABE tag to the selected text.
    appendTag: function (beforeTag, afterTag) {

        var element= this._getSelectedRange();
        var text = element.value;
        var textPre = text.substr(0, element.selectionStart);
        var textSelected = text.substr(element.selectionStart, element.selectionEnd - element.selectionStart);
        var textPost = text.substr(element.selectionEnd, text.length - element.selectionEnd);

        if (textSelected == '') {
            alert('テキストを選択してください。');
            return;
        }

        if (textSelected.match(/^\n/)) {
            textSelected =  textSelected.substr(1, textSelected.length);
            textPre = textPre + "\n";
        }
        if (textSelected.match(/\n$/)) {
            textSelected =  textSelected.substr(0, textSelected.length - 1);
            textPost = "\n" + textPost;
        }

        var selectedFirstPart = textSelected.substr(0, beforeTag.length);
        var selectedLastPart = textSelected.substr(-(afterTag.length));

        if (beforeTag == selectedFirstPart &&
            afterTag == selectedLastPart) {
            text = textPre + textSelected.substr(beforeTag.length, textSelected.length - beforeTag.length - afterTag.length) + textPost;

            if (document.all) {
                var start = textPre.replace(/\r/g,'').length;
                var end = - (textPost.replace(/\r/g,'').length);

                element.value = text;
                var range = element.createTextRange();

                range.moveStart('character', start);
                range.moveEnd('character', end);
                range.select();
            } else {
                var start = element.selectionStart;
                var end = start + textSelected.length - beforeTag.length - afterTag.length;
                element.value = text;
                element.setSelectionRange(start, end);
            }
        } else {
            text = textPre + beforeTag + textSelected + afterTag + textPost;

            if (document.all) {
                var start = textPre.replace(/\r/g,'').length;
                var end = - (textPost.replace(/\r/g,'').length);

                element.value = text;
                var range = element.createTextRange();

                range.moveStart('character', start);
                range.moveEnd('character', end);
                range.select();
            } else {
                var start = element.selectionStart;
                var end = start + beforeTag.length + textSelected.length + afterTag.length;
                element.value = text;
                element.setSelectionRange(start, end);
            }
        }

        Element.hide(this.colorPallet);
        element.focus();
    },

    // Remove ABE tags in the selected text except [IMG] tag.
    removeTags: function () {
        var element= this._getSelectedRange();
        var text = element.value;
        var textPre = text.substr(0, element.selectionStart);
        var textSelected = text.substr(element.selectionStart, element.selectionEnd - element.selectionStart);
        var textPost = text.substr(element.selectionEnd, text.length - element.selectionEnd);

        element.value = textPre + textSelected.replace(/\[[^IMG]+?\]/gm, '') + textPost;
        this.parse();
        element.focus();
    },

    // Get current selected range in the textarea.
    _getSelectedRange: function (p) {
        if (p == 'opener') {
            var element= window.opener.parent.$(this.id);
            if (window.opener.parent.document.selection) {
                element.focus();
                var range = window.opener.parent.document.selection.createRange();
                var clone = range.duplicate();
                clone.moveToElementText(element);
                clone.setEndPoint('EndToEnd', range);
                element.selectionStart = clone.text.length - range.text.length;
                element.selectionEnd = element.selectionStart + range.text.length;
            }
        } else if (p == 'parent') {
            var element= parent.$(this.id);
            if (parent.document.selection) {
                element.focus();
                var range = parent.document.selection.createRange();
                var clone = range.duplicate();
                clone.moveToElementText(element);
                clone.setEndPoint('EndToEnd', range);
                element.selectionStart = clone.text.length - range.text.length;
                element.selectionEnd = element.selectionStart + range.text.length;
            }
        } else {
            var element= $(this.id);
            if (document.selection) {
                element.focus();
                var range = document.selection.createRange();
                var clone = range.duplicate();
                clone.moveToElementText(element);
                clone.setEndPoint('EndToEnd', range);
                element.selectionStart = clone.text.length - range.text.length;
                element.selectionEnd = element.selectionStart + range.text.length;
            }
        }
        return element;
    },

    // Show/Hide the color pallet.
    toggleShowHideColorPallet: function () {
        Element.toggle(this.colorPallet);
    },

    // Append color tag.
    appendColorTag: function (color) {
        if (color == '') {
            return;
        }
        this.appendTag('[C=#' + color + ']', '[/C]');
    },

    // Append link tag.
    appendLinkTag: function () {
        var u = window.prompt('URL', 'http://');
        if (u == 'undefined' ||
            u == '' ||
            u.match(/^https?:\/\/$/) != null) {
            return;
        }
        this.appendTag('[LN=' + u + ']', '[/LN]');
    },

    // Append hr tag.
    appendHrTag: function () {
        var element= this._getSelectedRange();
        var text = element.value;
        var textPre = text.substr(0, element.selectionStart);
        var textSelected = text.substr(element.selectionStart, element.selectionEnd - element.selectionStart);
        var textPost = text.substr(element.selectionEnd, text.length - element.selectionEnd);

        element.value = textPre + '[HR]' + textSelected + textPost;
        element.focus();
    },

    // Append an img tag.
    appendImgTag: function (name, align, width) {
        if (name == '' ||
            align == '') {
            return;
        }
        var element= this._getSelectedRange('opener');
        var text = element.value;
        var textPre = text.substr(0, element.selectionStart);
        var textSelected = text.substr(element.selectionStart, element.selectionEnd - element.selectionStart);
        var textPost = text.substr(element.selectionEnd, text.length - element.selectionEnd);

        element.value = textPre + '[IMG=' + name + ':' + align + ':' + width + ']' + textSelected + textPost;
        element.focus();
    },

    // Append an img tag.
    appendImgTag2: function (name, align, width) {
        if (name == '' ||
            align == '') {
            return;
        }
        var element= this._getSelectedRange();
        var text = element.value;
        var textPre = text.substr(0, element.selectionStart);
        var textSelected = text.substr(element.selectionStart, element.selectionEnd - element.selectionStart);
        var textPost = text.substr(element.selectionEnd, text.length - element.selectionEnd);

        element.value = textPre + '[IMG=' + name + ':' + align + ':' + width + ']' + textSelected + textPost;
        element.focus();
    },

    // Remove any img tag. This method is called only from iframe;
    removeImgTag: function (name) {
        if (name == '') {
            return;
        }
        var element= this._getSelectedRange('parent');
        var text = element.value;
        var tag = '\\[IMG=' + name + ':(?:fl|fc|fr|l|c|r):([0-9]{1,3})\\](?:\r?\n)*';
        var regex = new RegExp(tag, 'ig')

        element.value = text.replace(regex, '');
        element.focus();
    },

    showFileList: function () {

        var url = AbEditorGlobal.fileListURL
        url += '?path=' + AbEditorGlobal.uploadDirectory + '&t=' + Math.random();

        new Ajax.Request(url,
                         {
                             method: 'get',
                                 requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
                                 onComplete: function(request) {
                                 var o = request.responseText.split('#');

                                 var text = '';
                                 for (var i = 0; i < o.length; ++i) {
                                     text += '<input type="checkbox" name="id_list[]" value="' + o[i] + '" /><img src="' + AbEditorGlobal.uploadDirectoryURL + '/' + o[i] + '" width="100" onclick="var info = window.open(\'' + AbEditorGlobal.fileInfoURL + '?name=' + o[i] + '&path=' + AbEditorGlobal.uploadDirectory + '\', \'fileinfo\', \'width=400,height=300,scrollbars=no,toolbar=no,resizable=no,left=350,top=100\'); info.focus();" /><br />';
                                 }

                                 if (window.opener) {
                                     window.opener.document.getElementById('abe_file_list').innerHTML = text;
                                 } else {
                                     $('abe_file_list').innerHTML = text;
                                 }
                             }
                          });


    },

    delfiles: function() {
        var ret = $('abe_del_files').getElementsByTagName('input');
        var filelist = new Array;

        var id_list = {};
        for (var i = 0; i < ret.length; ++i) {
            if (ret[i].type != 'checkbox') {
                continue;
            }
            if (ret[i].checked) {
                filelist.push(ret[i].value);
                if (id_list[ret[i].name] == undefined) {
                    id_list[ret[i].name] = new Array;
                }
                id_list[ret[i].name].push(ret[i].value);
            }
        }

        if (filelist.length < 1) {
            alert('削除したい画像にチェックを入れてください。');
            return;
        }

        if (!window.confirm('削除してもよろしいですか？\n(この操作は取り消せません!)')) {
            return;
        }

        for (var k = 0; k < filelist.length; ++k) {
            this.removeImgTag(filelist[k]);
        }

        var url = AbEditorGlobal.fileDelURL

        id_list['path'] = AbEditorGlobal.uploadDirectory;
        var postBody = $H(id_list).toQueryString()
        alert(postBody);
        new Ajax.Request(url,
                         {
                             method: 'post',
                             postBody: postBody,
                             onComplete: this.showFileList
                          });
    },

    showImageUploader: function() {
        $(this.imageUploader).style.display = 'block';
    },

    uploadImage: function(f) {
        var org = f;
        f.action = AbEditorGlobal.fileUploadURL;
        f.target = this.id + '_iframe';
        f.enctype = 'multipart/form-data';

        f.submit();

        f.action = org.action;
        f.target = org.target;
        f.enctype = org.enctype;
    },

    toggleShowHideTableInfo: function() {
        Element.toggle(this.tableInfo);
    },

    appendTableTag: function(r, c, b) {
        r = $(r).value - 0;
        c = $(c).value - 0;
        if (r < 1 || c < 1) {
            return;
        }

        var src = '';

        if ($(b).checked) {
            src = "[TBL:1]\n";
        } else {
            src = "[TBL]\n";
        }
        for (var i = 0; i < r; i++) {
           src += "[TR]\n";
           for (var j = 0; j < c; j++) {
               src += "[TD]ここに内容が入ります[/TD]\n";
           }
           src += "[/TR]\n";
        }
        src += "[/TBL]\n";

        var element= this._getSelectedRange();
        var text = element.value;
        var textPre = text.substr(0, element.selectionStart);
        var textSelected = text.substr(element.selectionStart, element.selectionEnd - element.selectionStart);
        var textPost = text.substr(element.selectionEnd, text.length - element.selectionEnd);

        element.value = textPre + src + textSelected + textPost;
        element.focus();

        Element.hide(this.tableInfo);
    }

};

var AbEditorParser = new Object;

// Parse the text and change it to HTML
AbEditorParser.parse = function(text) {
    if (text == '') {
        return '';
    }

    text = text.replace(/"/g, '&quot;');
    text = text.replace(/</g, '&lt;');
    text = text.replace(/>/g, '&gt;');

    text = text.replace(/\[TBL\]\r?\n/g, '<table class="abe_table_noborder">');
    text = text.replace(/\[TBL:1\]\r?\n/g, '<table class="abe_table_border">');
    text = text.replace(/\[\/TBL\](?:\r?\n)?/g, '</table>');
    text = text.replace(/\[TR\]\r?\n/g, '<tr>');
    text = text.replace(/\[\/TR\]\r?\n/g, '</tr>');
    text = text.replace(/\[TH\]/g, '<th>');
    text = text.replace(/\[\/TH\]\r?\n/g, '</th>');
    text = text.replace(/\[TD\]/g, '<td>');
    text = text.replace(/\[\/TD\]\r?\n/g, '</td>');

    text = text.replace(/(\r?\n){3}/g, '<br style="clear: both;" /><br />');
    text = text.replace(/\r?\n/g, '<br />');
    text = text.replace(/\\/g, '&yen;');

    text = text.replace(/\[AL\](.*?)\[\/AL\]/gm, '<div class="abe_align_left">$1</div>');
    text = text.replace(/\[AC\](.*?)\[\/AC\]/gm, '<div class="abe_align_center">$1</div>');
    text = text.replace(/\[AR\](.*?)\[\/AR\]/gm, '<div class="abe_align_right">$1</div>');

    text = text.replace(/\[H\](.*?)\[\/H\]/gm, '<div class="abe_heading">$1</div>');

    text = text.replace(/\[XXL\](.*?)\[\/XXL\]/gm, '<span class="abe_font_size_xxl">$1</span>');
    text = text.replace(/\[XL\](.*?)\[\/XL\]/gm, '<span class="abe_font_size_xl">$1</span>');
    text = text.replace(/\[L\](.*?)\[\/L\]/gm, '<span class="abe_font_size_l">$1</span>');
    text = text.replace(/\[S\](.*?)\[\/S\]/gm, '<span class="abe_font_size_s">$1</span>');

    text = text.replace(/\[B\](.*?)\[\/B\]/gm, '<span class="abe_font_style_bold">$1</span>');
    text = text.replace(/\[I\](.*?)\[\/I\]/gm, '<span class="abe_font_style_italic">$1</span>');

    text = text.replace(/\[U\](.*?)\[\/U\]/gm, '<span class="abe_text_decoration_underline">$1</span>');
    text = text.replace(/\[DEL\](.*?)\[\/DEL\]/gm, '<span class="abe_text_decoration_line-through">$1</span>');

    text = text.replace(/\[LN=(.*?)\]/gm, '<a href="$1" target="_blank">');
    text = text.replace(/\[\/LN\]/gm, '</a>');
    text = text.replace(/\[C=(.*?)\]/gm, '<span style="color: $1;">');
    text = text.replace(/\[\/C\]/gm, '</span>');

    text = text.replace(/\[IMG=([^\]]+?):fl:([0-9]{1,3})\]/g, '<div class="abe_img_float_left"><img width="$2" src="' + AbEditorGlobal.uploadDirectoryURL + '/$1" /></div>');
    text = text.replace(/\[IMG=([^\]]+?):fc:([0-9]{1,3})\]/g, '<img width="$2" src="' + AbEditorGlobal.uploadDirectoryURL + '/$1" />');
    text = text.replace(/\[IMG=([^\]]+?):fr:([0-9]{1,3})\]/g, '<div class="abe_img_float_right"><img width="$2" src="' + AbEditorGlobal.uploadDirectoryURL + '/$1" /></div>');
    text = text.replace(/\[IMG=([^\]]+?):l:([0-9]{1,3})\]/g, '<div class="abe_img_left"><img width="$2" src="' + AbEditorGlobal.uploadDirectoryURL + '/$1" /></div>');
    text = text.replace(/\[IMG=([^\]]+?):c:([0-9]{1,3})\]/g, '<div class="abe_img_center"><img width="$2" src="' + AbEditorGlobal.uploadDirectoryURL + '/$1" /></div>');
    text = text.replace(/\[IMG=([^\]]+?):r:([0-9]{1,3})\]/g, '<div class="abe_img_right"><img width="$2" src="' + AbEditorGlobal.uploadDirectoryURL + '/$1" /></div>');

    text = text.replace(/\[HR\](?:\r?\n)?/g, '<hr style="clear: both; border: solid 1px #ccc; height: 1px;" />');


    return text;
}
