/****************************************************

* CKEditor Extension

*****************************************************/

Ext.form.CKEditor = function(config){

    this.config = config;

    Ext.form.CKEditor.superclass.constructor.call(this, config);
    this.on('destroy', function (ct) {
    	ct.destroyInstance();
    });

};



Ext.extend(Ext.form.CKEditor, Ext.form.TextArea,  {

    onRender : function(ct, position){

        if(!this.el){

            this.defaultAutoCreate = {

                tag: 'textarea',

                autocomplete: 'off'

            };

        }

        Ext.form.TextArea.superclass.onRender.call(this, ct, position);
        if (!this.config.CKConfig) this.config.CKConfig = {};
        var defConfig = {
			 resize_enabled : false,
             toolbar : [
//    { name: 'document',    items : [ 'Source','-','Save','NewPage','DocProps','Preview','Print','-','Templates' ] },
    { name: 'clipboard',   items : [ 'Cut','Copy','Paste','PasteText','PasteFromWord','-','Undo','Redo' ] },
    { name: 'editing',     items : [ 'Find','Replace','-','SelectAll','-','SpellChecker', 'Scayt' ] },
//    { name: 'forms',       items : [ 'Form', 'Checkbox', 'Radio', 'TextField', 'Textarea', 'Select', 'Button', 'ImageButton', 'HiddenField' ] },
    '/',
    { name: 'basicstyles', items : [ 'Bold','Italic','Underline','Strike','Subscript','Superscript','-','RemoveFormat' ] },
    { name: 'paragraph',   items : [ 'NumberedList','BulletedList','-','Outdent','Indent','-','Blockquote','CreateDiv','-','JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock','-','BidiLtr','BidiRtl' ] },
    { name: 'links',       items : [ 'Link','Unlink','Anchor' ] },
//    { name: 'insert',      items : [ 'Image','Flash','Table','HorizontalRule','Smiley','SpecialChar','PageBreak' ] },
    '/',
    { name: 'styles',      items : [ 'Styles','Format','Font','FontSize' ] },
    { name: 'colors',      items : [ 'TextColor','BGColor' ] }//,
//    { name: 'tools',       items : [ 'Maximize', 'ShowBlocks','-','About' ] }
],
			 on : {
	         // maximize the editor on startup
	         'instanceReady' : function( evt ) {
	            evt.editor.resize((evt.editor.element.$.style.width ? evt.editor.element.$.style : '100%'), parseInt(evt.editor.element.$.style.height));
	            evt.editor.is_instance_ready = true;
	         }
	      }
        }
        Ext.apply(this.config.CKConfig, defConfig);
        CKEDITOR.replace(this.id, this.config.CKConfig);

    },

    onResize: function(width, height) {
    	Ext.form.TextArea.superclass.onResize.call(this, width, height);
    	if (CKEDITOR.instances[this.id].is_instance_ready) {
    		CKEDITOR.instances[this.id].resize(width, height);
    	}
    },

    setValue : function(value){
		try{               //fix for ie
			if (!value) value = '&nbsp;';

			Ext.form.TextArea.superclass.setValue.apply(this,arguments);

			if (CKEDITOR.instances[this.id]) CKEDITOR.instances[this.id].setData( value );
		}catch(e){}
    },



    getValue : function(){
		try{          //fix for ie
            if (CKEDITOR.instances[this.id]) CKEDITOR.instances[this.id].updateElement();
            return Ext.form.TextArea.superclass.getValue.call(this);
		}catch(e){}
    },



    getRawValue : function(){
		try{         //fix for ie
        	if (CKEDITOR.instances[this.id]) CKEDITOR.instances[this.id].updateElement();

        	return Ext.form.TextArea.superclass.getRawValue.call(this);
		}catch(e){}

    },

	destroyInstance: function(){
        if (CKEDITOR.instances[this.id]) {
            delete CKEDITOR.instances[this.id];
        }
    }


});

Ext.reg('ckeditor', Ext.form.CKEditor);
