/**
 * @class Compass.ErpApp.Shared.CKeditor
 * @extends Ext.form.TextArea Converts a textarea into a CkEditor Instance
 * 
 * @author Russell Holmes - http://www.portablemind.com
 * 
 * @additional config options ckEditorConfig - configuration for CkEditor
 *             Instance
 */

Ext.ns("Compass.ErpApp.Shared");

Compass.ErpApp.Shared.CKeditor = Ext
		.extend(
				Ext.form.TextArea,
				{
					ckEditorInstance : null,

					constructor : function(config) {
						config = Ext.apply({
							grow : true
						}, config);
						Compass.ErpApp.Shared.CKeditor.superclass.constructor
								.call(this, config);
					},

					isBlank : function(value) {
						var result = false;

						if (value == 'undefined' || value == undefined
								|| value == null || value == '' || value == ' ') {
							result = true;
						}

						return result;
					},

					onRender : function(ct, position) {
						Compass.ErpApp.Shared.CKeditor.superclass.onRender
								.call(this, ct, position);
						this.setupCkEditor();
						this.on('resize', this.textAreaResized, this);
					},

					setupCkEditor : function() {
						Ext.applyIf(this.initialConfig.ckEditorConfig, {
							resize_enabled : false,
							base_path : '/js/ckeditor/',
							toolbarStartupExpanded : false,
							skin : 'office2003'
						});
						var editor = CKEDITOR.replace(this.el.dom.name,
								this.initialConfig.ckEditorConfig);
//						editor.ui
//								.addButton(
//										'CompassUploadFile',
//										{
//											label : 'Upload File',
//											icon : '/images/erp_app/desktop/image_add.png',
//											click : function(editor) {
//												var uploadWindow = new Compass.ErpApp.Shared.UploadWindow();
//												uploadWindow.show();
//											}
//										});
						this.ckEditorInstance = editor;
					},

					textAreaResized : function(textarea, adjWidth, adjHeight,
							rawWidth, rawHeight) {

						if (!this
								.isBlank(this.ckEditorInstance)) {
							if (!this.isBlank(rawWidth)
									&& !this
											.isBlank(rawHeight)) {
								var el = document
										.getElementById('cke_contents_'
												+ this.id);

								if (!this.isBlank(el)) {
									var toolBoxDiv = document.getElementById(
											'cke_top_' + this.id)
											.getElementsByTagName('div')[0];
									var toolBoxEl = Ext.get(toolBoxDiv);
									var displayValue = toolBoxEl
											.getStyle('display');
									if (displayValue != 'none') {
										this.ckEditorInstance
												.execCommand('toolbarCollapse');
										el.style.height = rawHeight - 51 + 'px';
										this.ckEditorInstance
												.execCommand('toolbarCollapse');
									} else {
										el.style.height = rawHeight - 51 + 'px';
									}

								} else {
									this.ckEditorInstance.config.height = rawHeight - 51;
								}
							}
						}
					},

					setValue : function(value) {
						this.MyValue = value;
						this.ckEditorInstance.setData(value);
						Compass.ErpApp.Shared.CKeditor.superclass.setValue.apply(this, [ value ]);
					},

					getValue : function() {
						var value = this.ckEditorInstance.getData();
						Ext.form.TextArea.superclass.setValue.apply(this, [ value ]);
//						return Compass.ErpApp.Shared.CKeditor.superclass.getValue(this);
						return value;
					},

					getRawValue : function() {
						var value = this.ckEditorInstance.getData();
						Ext.form.TextArea.superclass.setRawValue(this, [ value ]);
						return Compass.ErpApp.Shared.CKeditor.superclass.getRawValue(this);
					},

					insertHtml : function(html) {
						this.ckEditorInstance.insertHtml(html);
					}
				});
Ext.reg('ckeditor', Compass.ErpApp.Shared.CKeditor);
