Ext.namespace('Ext.ux.form');

Ext.ux.form.FCKeditor = function(config) {
	this.config = config;
	Ext.ux.form.FCKeditor.superclass.constructor.call(this, config);
	this.instanceLoaded = false;
	this.instanceValue = '';
	this.editorInstance = undefined;
};
Ext.extend(Ext.ux.form.FCKeditor, Ext.form.TextArea, {
			initEvents : function() {
				this.on('destroy', function() {
							if (typeof this.editorInstance != 'undefined') {
								delete this.editorInstance;
							}
						});
			},
			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);
				this.hideMode = "visibility";
				this.hidden = true;
				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.id + "'," + this.config.height + ");", 100); // Change
			},
			setIsLoaded : function(v) {
				this.instanceLoaded = v;
			},
			getIsLoaded : function() {
				return this.instanceLoaded;
			},
			setValue : function(value) {
				this.instanceValue = value;
				if (this.instanceLoaded) {
					this.FCKeditorSetValue(value); // Change this.name
				}
				Ext.form.TextArea.superclass.setValue.apply(this, [value]);
			},
			getValue : function() {
				if (this.instanceLoaded) {
					value = this.FCKeditorGetValue(); // Change this.name to this.id
					Ext.form.TextArea.superclass.setValue.apply(this, [value]);
					return Ext.form.TextArea.superclass.getValue.call(this); // Change getValue(this) to
				} else {
					return this.instanceValue;
				}
			},
			getRawValue : function() {
				if (this.instanceLoaded) {
					value = this.FCKeditorGetValue(); // Change this.name to this.id
					Ext.form.TextArea.superclass.setRawValue.apply(this, [value]);
					return Ext.form.TextArea.superclass.getRawValue.call(this); // Change getValue(this) to
				} else {
					return this.instanceValue;
				}
			},
			FCKeditorSetValue : function(value) {
				if(this.instanceLoaded == false){
					return;
				}
				var runner = new Ext.util.TaskRunner();
				var task = {
					run : function() {
						try {
							var editor = this.editorInstance; // update var editor
							if (editor.EditorDocument.body) {
								editor.SetData(value);
								runner.stop(task);
							}
						} catch (ex) {
						}
					},
					interval : 100,
					scope : this
				};
				runner.start(task); // end fix
			},
			FCKeditorGetValue : function() {
				var data = '';
				if(this.instanceLoaded == false){
					return data;
				}
				data = this.editorInstance.GetData();				
				return data;
			}
		});
Ext.reg('fckeditor', Ext.ux.form.FCKeditor);

function loadFCKeditor(element, height) {
	var oFCKeditor = new FCKeditor(element);
	oFCKeditor.BasePath = oFCKeditorOptions.BasePath;
	oFCKeditor.ToolbarSet = oFCKeditorOptions.ToolbarSet;
	oFCKeditor.Config = oFCKeditorOptions.Config;
	oFCKeditor.Height = height;
	oFCKeditor.ReplaceTextarea();
}
function FCKeditor_OnComplete(editorInstance) {
	var activeEditor = Ext.getCmp(editorInstance.Name);
	activeEditor.editorInstance = editorInstance;
	activeEditor.instanceLoaded = true;
	activeEditor.FCKeditorSetValue(activeEditor.instanceValue);
	editorInstance.Events.AttachEvent('OnBlur', FCKeditor_OnBlur);
	editorInstance.Events.AttachEvent('OnFocus', FCKeditor_OnFocus);
}

function FCKeditor_OnBlur(editorInstance) {
	editorInstance.ToolbarSet.Collapse();
}

function FCKeditor_OnFocus(editorInstance) {
	editorInstance.ToolbarSet.Expand();
}