/**
 * подгрузка редактора tinyMCE на требуемый элемент интерфейса
 */
var SDG_tinyMCE = function (psElementId) {
    this.oTextarea = _$(psElementId);
    this.oEditor = tinyMCE;
    this.oEditor.init ( {
            mode     : "exact",
            elements : psElementId,
            theme : "advanced",
            // todo сделать красивое подключение стилей
            plugins : "safari,spellchecker,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",
            theme_advanced_toolbar_location : "top",
            theme_advanced_toolbar_align : "left",
            theme_advanced_statusbar_location : "bottom"
    });
};
/**
 * подгрузка редактора FCKEditor на требуемый элемент интерфейса
 */
var SDG_FCKEditor = function (psElementId) {
    

    this.oTextarea = _$(psElementId);
    this.oEditor = new FCKeditor (psElementId);
    //this.oEditor.EditorAreaCSS = document.location.protocol + '//' + document.location.host + '/resources/css/design/style.css';
    this.oEditor.BasePath = "/gnu_source/editors/fckeditor/";
    this.oEditor.InstanceName = this.oTextarea.name;
    this.oEditor.ToolbarSet = this.oTextarea.getAttribute('mode');
    this.oEditor.Width = this.oTextarea.clientWidth;  //todo сделать правильный расчет длин и высот
    this.oEditor.Height = this.oTextarea.clientHeight / (this.oTextarea.getAttribute('mode') == 'Basic' ? 2 : 1);
    this.oEditor.ReplaceTextarea();
};

var SDG_CKEditor = function (psElementId) {
    this.oTextarea = _$(psElementId);
    delete (CKEDITOR.instances[psElementId]);
    var editor = CKEDITOR.replace( psElementId,
                    {
                    skin : 'v2'
                    , toolbar : this.oTextarea.getAttribute('mode')
                    } );
    AjexFileManager.init({returnTo: 'ckeditor', editor: editor, skin:'light'});
;
};

/**
 * SDG_Editor - контроллер внешних редакторов
 */
var SDG_Editor = {
    /**
     * доступные редакторы
     */
    _oEditors : {           /* этот справочник можно будет наполнять */
                'tinymce' : {
                        'file': '/gnu_source/editors/tiny_mce/tiny_mce.js',
                        'object' : SDG_tinyMCE,
                        'loaded' : 1 },
                'fckeditor' :  {
                        'file': '/gnu_source/editors/fckeditor/fckeditor.js',
                        'object' : SDG_FCKEditor,
                        'loaded' : 0 },
                'ckeditor' :  {
                        'file': '/gnu_source/editors/ckeditor/ckeditor.js',
                        'object' : SDG_CKEditor,
                        'loaded' : 1 }
                },
    _oObjects : {},
    /**
     * подгрузить редактор
     */
    load : function (psEditor) {
         if ( ! this._oEditors[psEditor]['loaded'] ) {
            SDG_Core.loadJS( this._oEditors[psEditor]['file'] );
            this._oEditors[psEditor]['loaded'] = 1; 
        }
    },

    attach : function (psElementId, psEditor) {
        if (this._oEditors[psEditor] && !this._oObjects[ psElementId ]) {
            this.load(psEditor);
            this._oObjects[ psElementId ] = new this._oEditors[ psEditor ]['object'] ( psElementId );
        }
    }
    
}
