/* use in this way

    {
	    xtype:'fckeditor',
	    name:'content_1',
	    id:'content_1',
	    fieldLabel:'Content',
	    height:270
	}

*/

Ext.form.FCKeditor = function(config){
    this.config 	= config;
    Ext.form.FCKeditor.superclass.constructor.call(this, config);
    this.MyisLoaded = false;
    this.MyValue	= '';
    this.fckInstance= undefined; // to avoid using FCKAPI, this is a reference to instance created on FCKeditor_OnComplete
};

Ext.extend(Ext.form.FCKeditor, Ext.form.TextArea,  {
    onRender : function(ct, position){
        if(!this.el){
            this.defaultAutoCreate = {
                tag: "textarea",
                style:"width:100px;height:60px;",
                autocomplete: "off"
            };
        }
        Ext.form.TextArea.superclass.onRender.call(this, ct, position);
        if(this.grow){
            this.textSizeEl = Ext.DomHelper.append(document.body, {
                tag: "pre", cls: "x-form-grow-sizer"
            });
            if(this.preventScrollbars){
                this.el.setStyle("overflow", "hidden");
            }
            this.el.setHeight(this.growMin);
        }
        setTimeout("loadFCKeditor('"+this.name+"',"+ this.config.height + ", '" + this.config.editorWidth + "', '" + this.config.toolbarSet + "');",100);
    },

    /**
     *
     * @param {String} value
     */
    setValue : function(value,boolResetIsDirty) { //added boolResetIsDirty
        this.instanceValue = value;
        if (this.instanceLoaded) {
            this.FCKeditorSetValue(value,boolResetIsDirty); // Change this.name //added boolResetIsDirty
        }
        Ext.form.TextArea.superclass.setValue.apply(this, [value,boolResetIsDirty]);
    },
    /**
      * ??FCKeditor????
      * @param1 {String} value
      * @param2 {Boolean} boolResetIsDirty
      */
     FCKeditorSetValue : function(value,boolResetIsDirty) {
         if(this.instanceLoaded == false){
             return;
         }
         // fix IE No Permission Denied errors
         var runner = new Ext.util.TaskRunner();
         var task = {
             run : function() {
                 try {
                     var editor = this.editorInstance; // update var editor
                     if (editor.EditorDocument.body) {
                         //modified with boolResetIsDirty
                         if(typeof boolResetIsDirty != 'undefined'){
                             editor.SetData(value,boolResetIsDirty);
                         }else{
                             editor.SetData(value);
                         }
                         runner.stop(task);

                     }
                 } catch (ex) {
                     //Ext.logf('????(info):{0}', ex);
                 }
             },
             interval : 100,
             scope : this
         };
         runner.start(task); // end fix
     },
     //isDirty check
     isDirty:function(){
         return this.editorInstance.IsDirty();
     }
     //reset isDirty bit
     ,resetIsDirty:function(){
         this.editorInstance.ResetIsDirty();
     }
     ,

    getValue : function(){
        if (this.MyisLoaded){
            value = FCKeditorGetValue(this.name);
            Ext.form.TextArea.superclass.setValue.apply(this,[value]);
            return Ext.form.TextArea.superclass.getValue(this);
        } else {
            return this.MyValue;
        }
    },
    getRawValue : function(){
        if (this.MyisLoaded){
            value=FCKeditorGetValue(this.name);
            Ext.form.TextArea.superclass.setRawValue.apply(this,[value]);
            return Ext.form.TextArea.superclass.getRawValue(this);
        } else {
            return this.MyValue;
        }
    }
});
Ext.reg('fckeditor', Ext.form.FCKeditor);

function loadFCKeditor(element, height, editorWidth, toolbarSet){
    oFCKeditor 						= new FCKeditor(element);
    oFCKeditor.Height 				= height;
    oFCKeditor.Width = editorWidth;
    oFCKeditor.ToolbarSet = toolbarSet;
    Ext.apply(oFCKeditor,oFCKeditorOptions)
	oFCKeditor.ReplaceTextarea();
}

function FCKeditor_OnComplete(editorInstance){
	if (Ext.getCmp(editorInstance.Name)) {
		Ext.getCmp(editorInstance.Name).MyisLoaded  = true;
		Ext.getCmp(editorInstance.Name).fckInstance = editorInstance;
	}
}
function FCKeditorSetValue(name,value){
    if (name != undefined){
        var extCmp = Ext.getCmp(name);
        if (extCmp != undefined) {
        	extCmp.fckInstance.SetData(value);
        }
    }
}
function FCKeditorGetValue(name){
    if (name != undefined){
        var data = '';
        var extCmp = Ext.getCmp(name);
        if (extCmp != undefined) {
        	data = extCmp.fckInstance.GetData();
        }
        return data;
    }
}
