

function Editor() {
    this.oldHTML = "";
    this.user = "";
    this.id = "";
    this.lang = "";
    this.CLEditor;
}

Editor.prototype.init = function(user,id,lang) {
    this.user = user;
    this.id = id;
    this.lang = lang;
}

Editor.prototype.staticText = function(action,section) {

    if(action == "edit") {
        this.showCLEditor(section,this.lang);
    }
    if(action == "save") {
        //var ed = tinyMCE.get('Core_Text_textarea_' + section);
        //var text = ed.getContent();
        var text = $("#Core_Text_textarea_" + section).val();
        var query = '{"action":"saveStaticText","section":"'+section+'","lang":"'+this.lang+'","text":"'+delNL(text.replace(/\"/g,'|'))+'"}';
        var user = this.user;
        var id = this.id;
        var server = "/server.Static.php";
        var successFunc = "editor.CLEditorClose('"+section+"')";
        var successMsg = i18n.translate("staticText:success");
        var errorMsg = i18n.translate("staticText:error");

        disp.send(server,query,user,id,successFunc,successMsg,"",errorMsg);
        
    }
}

Editor.prototype.showEditor = function(section,lang) {

    this.oldHTML = $("#Core_Text_"+section).html();

    var editorForm = '';
    editorForm += '<textarea id="Core_Text_textarea_'+section+'" style="width: 99%; height: 500px;">';
    editorForm += this.oldHTML;
    editorForm += '</textarea>';
    editorForm += '<br/><br/>';

    var buttons = '';
    buttons += '<a href="javascript:editor.editorCancel(\''+section+'\')">Cancel</a>';
    buttons += '<a href="javascript:editor.editorReset(\''+section+'\')">Reset</a>';
    buttons += '<a href="javascript:editor.staticText(\'save\',\''+section+'\',\''+lang+'\')">Save</a>';

    $("#Core_Text_"+section).html(editorForm);
    $("#LMUI_buttonBar_"+section).html(buttons);

    tinyMCE.init({
        mode : "textareas",
        theme: "advanced",
        plugins : "pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,advlist",

        // Theme options
        theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,formatselect,fontselect,fontsizeselect,|,cut,copy,paste,pastetext,pasteword",
        theme_advanced_buttons2 : "search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
        theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl",
        theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak,|,fullscreen",
        theme_advanced_toolbar_location : "top",
        theme_advanced_toolbar_align : "left",
        theme_advanced_statusbar_location : "bottom",
        theme_advanced_resizing : true,
        document_base_url : core.baseURL + "/",
        relative_urls : false,
        remove_script_host : false,

        theme_advanced_toolbar_location : "top",
        entity_encoding : "raw"

    });
}
Editor.prototype.editorCancel = function(section) {
    $("#Core_Text_"+section).html(this.oldHTML);
    var buttons = '';
    buttons += '<a href="javascript:editor.staticText(\'edit\',\''+section+'\')">Edit</a>';
    $("#LMUI_buttonBar_"+section).html(buttons);
}
Editor.prototype.editorReset = function(section) {
    $("#Core_Text_textarea_"+section).val("");
}
Editor.prototype.editorClose = function(section) {
    var ed = tinyMCE.get('Core_Text_textarea_' + section);
    var text = ed.getContent();
    var buttons = '';
    buttons += '<a href="javascript:editor.staticText(\'edit\',\''+section+'\')">Edit</a>';
    $("#LMUI_buttonBar_"+section).html(buttons);
    $("#Core_Text_"+section).html(text);
}


Editor.prototype.showCLEditor = function(section,lang) {

    this.oldHTML = $("#Core_Text_"+section).html();

    var editorForm = '';
    editorForm += '<textarea id="Core_Text_textarea_'+section+'" style="width: 99%; height: 500px;">';
    editorForm += this.oldHTML;
    editorForm += '</textarea>';
    editorForm += '<br/><br/>';

    var buttons = '';
    buttons += '<a href="javascript:editor.editorCancel(\''+section+'\')">Cancel</a>';
    buttons += '<a href="javascript:editor.CLEditorReset(\''+section+'\')">Reset</a>';
    buttons += '<a href="javascript:editor.staticText(\'save\',\''+section+'\',\''+lang+'\')">Save</a>';

    $("#Core_Text_"+section).html(editorForm);
    $("#LMUI_buttonBar_"+section).html(buttons);

    this.CLEditor = $("#Core_Text_textarea_"+section).cleditor({width:"100%", height:"100%"})[0].focus();

}
Editor.prototype.CLEditorClose = function(section) {
    var text = $("#Core_Text_textarea_"+section).val();
    var buttons = '';
    buttons += '<a href="javascript:editor.staticText(\'edit\',\''+section+'\')">Edit</a>';
    $("#LMUI_buttonBar_"+section).html(buttons);
    $("#Core_Text_"+section).html(text);

}
Editor.prototype.CLEditorReset = function(section) {
    this.CLEditor.clear();
}


var editor = new Editor();
