/****************************************************
*    CKEditor Extension
*****************************************************/

isValueBlank = function(value) {
    var result = false;

    if(value == 'undefined' || value == undefined || value == null || value == '' || value == ' '){
        result = true;
    }

    return result;
};

Ext.form.CKEditor = function(config){
    this.config = config;
    Ext.form.CKEditor.superclass.constructor.call(this, config);
};

Ext.extend(Ext.form.CKEditor, Ext.form.TextArea,  {
    destroyInstance: function(){
        if (CKEDITOR.instances[this.id]) {
            delete CKEDITOR.instances[this.id];
        }
    },
    
    onRender : function(ct, position){
        if (CKEDITOR.instances[this.id]) {
            CKEDITOR.remove(CKEDITOR.instances[this.id]);
         }
        if(!this.el){
            this.defaultAutoCreate = {
                tag: "textarea",
                autocomplete: "off"
            };
        }
        Ext.form.TextArea.superclass.onRender.call(this, ct, position);
        var editor = CKEDITOR.replace(this.id,
        {
          filebrowserBrowseUrl : '/ckfinder/ckfinder.html',
          filebrowserImageBrowseUrl : '/ckfinder/ckfinder.html?type=Images',
          filebrowserFlashBrowseUrl : '/ckfinder/ckfinder.html?type=Flash',
          filebrowserUploadUrl : '/ckfinder/core/connector/cfm/connector.cfm?command=QuickUpload&type=Files',
          filebrowserImageUploadUrl : '/ckfinder/core/connector/cfm/connector.cfm?command=QuickUpload&type=Images',
          filebrowserFlashUploadUrl : '/ckfinder/core/connector/cfm/connector.cfm?command=QuickUpload&type=Flash',
          //skin: 'chris',
          toolbar: 'CS2editor'
        });
        this.ckEditorInstance = editor;
        this.on('resize', this.textAreaResized, this);
    },
    
    textAreaResized : function(textarea, adjWidth, adjHeight, rawWidth, rawHeight){
      if(!isValueBlank(this.ckEditorInstance)){
        if(!isValueBlank(rawWidth) && !isValueBlank(rawHeight)){
          var el = document.getElementById('cke_contents_' + this.id);
            if(el){
              el.style.height = rawHeight - 111 + 'px';
              if (Ext.isChrome || Ext.isSafari){
                var innerFrame = el.getElementsByTagName('iframe');
                innerFrame[0].style.width = rawWidth - 20 + 'px';
              }
            }
        }else{
          this.ckEditorInstance.config.height = rawHeight - 111;
        }
      }
    },
    
    setValue : function(value){
        Ext.form.TextArea.superclass.setValue.apply(this,[value]);
		CKEDITOR.instances[this.id].setData( value );
    },

    getValue : function(){
		CKEDITOR.instances[this.id].updateElement();
		return Ext.form.TextArea.superclass.getValue(this);
    },

    getRawValue : function(){
		CKEDITOR.instances[this.id].updateElement();	
		return Ext.form.TextArea.superclass.getRawValue(this);
    } 

});
Ext.reg('ckeditor', Ext.form.CKEditor);
