var Fck = Class.create()
Fck.extend({
	config: { Config: {} }, // default configuration
	path: '/public/js/editor/fck.latest/', // default path to FCKEditor directory
	list: [], // holds a list of instances
	loaded: false, // flag indicating whether FCK script is loaded
	intercepted: null, // variable to store intercepted method(s)

	// utility method to read contents of FCK editor
	content: function(i, v){
		try{
			var x = FCKeditorAPI.GetInstance(i);
			if(v) x.SetHTML(v);
			return x.GetXHTML(true);
		}catch(e){ return ''; };
	}, // fck.content function

	insertHTML:function (i,v){
		var oEditor = FCKeditorAPI.GetInstance(i) ;
		if ( oEditor.EditMode == FCK_EDITMODE_WYSIWYG )
		{
			oEditor.InsertHtml(value) ;
		}
	},

	setHTML: function(i, v){
		if(typeof i=='object'){
			v = i.html;
			i = i.InstanceName || i.instance;
		};
		return this.content(i, v);
	},
	clear:function(){
		var e = this.list;
		for(var i=0;i<e.length;i++){
			this.content(e[i].InstanceName, '&nbsp;');
		}
	},
	// utility method to update textarea contents before ajax submission
	update: function(){
		// Update contents of all instances
		var e = this.list;
		for(var i=0;i<e.length;i++){
			var ta = e[i].textarea;
			var ht = this.content(e[i].InstanceName);
			ta.set('value',ht);
//			if(ht!=ta.get('value'))
			//alert('Critical error in FCK plugin:'+'\n'+'Unable to update form data');
		}
	}, // fck.update

	// utility method to create instances of FCK editor (if any)
	create: function(o/* options */){
		o = $extend(this.config || {}, o || {});
		// Plugin options
		$extend(o,{
			selector: (o.selector || 'textarea.fck, textarea.fckeditor'),
			BasePath: (o.path || o.BasePath || this.path)
		});
		// Find fck.editor-instance 'wannabes'
		e = o.e;
		// Load script and create instances
		if(this.loaded){
			this.editor(e,o);
		}
		else{
			var f = this;
			var head = document.getElementsByTagName("head")[0];
			var script = document.createElement("script");
			script.src = o.BasePath+'fckeditor.js';
			var done = false;
			script.onload = script.onreadystatechange = function(){
				if ( !done && (!this.readyState ||
				this.readyState == "loaded" || this.readyState == "complete") ) {
					done = true;
					f.loaded = true;
					f.editor(e,o);
				}
			}
			head.appendChild(script);

		};
		// Return matched elements...

		return e;
	},



	// utility method to create an instance of FCK editor
	editor: function(e /* elements */, o /* options */){
		o = $extend(this.config || {}, o || {});
		// Default configuration
		$extend(o,{
			Width: (o.width || o.Width || '100%'),
			Height: (o.height || o.Height|| '500px'),
			BasePath: (o.path || o.BasePath || this.path),
			ToolbarSet: (o.toolbar || o.ToolbarSet || 'YHBasic'),
			Config: (o.config || o.Config || {})
		});
		e = $(e);
		if($chk(e)){
			// Local array to store instances
			var a = (this.list || []);
			// Go through objects and initialize fck.editor
			t=e;
			t.name = (t.name || 'fck'+(this.list.length+1));
			t.id = (t.id || t.name);
			if(t.id&&!t.fck){
				var n = a.length;
				a[n] = new FCKeditor(t.id);
				$extend(a[n], o);
				a[n].ReplaceTextarea();
				a[n].textarea = t;
				t.fck = a[n];
			}
			// Store instances in global array
			this.list = a;
		};
		// return jQuery array of elements
		return e;
	}, // fck.editor function

	// start-up method
	initialize: function(o/* options */){
	// Create FCK editors
		return this.create(o);
	} // fck.start

}) // fck object
;


