/**
 * The options for the vv_editor init method are:
 * 
 * width {string} - optional
 * 	the width to assign to the iframe that contains the editor
 * 	Default: 640
 * 
 * height {string}  - optional
 * the heigth to assign to the iframe that contains the editor
 * Default: 400
 * 
 * editorPath {string} - optional
 * 	the path of the editor to load into the iframe (editor.jsp)
 * 	Default: /html/js/editor/editor.jsp
 * 
 * plid {string} - required
 * 	html p_l_id parameter for the fckeditor.jsp
 *  
 * pMainPath {string} - required
 * 	html p_main_path parameter for the fckeditor.jsp
 * 
 * doAsUserId {string} - required
 * 	html p_main_path parameter for the fckeditor.jsp
 * 
 * cssPath {string} - required
 * 	html p_main_path parameter for the fckeditor.jsp
 * 
 * cssClasses {string} - required
 * 	html p_main_path parameter for the fckeditor.jsp
 *  
 * toolbarSet {string} - optional
 * 	html p_main_path parameter for the fckeditor.jsp
 *  Default: liferay
 *  
 * editorType {string} - required
 * 	can be one of the following values:
 * 		- text: 
 * 			simple editor
 * 		- html: 
 * 			fck editor
 * 		- id attribute of a select box
 * 			the select box values must be 'text' and 'html'.
 * 			The user can change the editor type selecting the 
 * 			corresponding value.  
 * 
 * loadOnStart {boolean} - optional
 * 	true if the editor must be automatically loaded after creating the widget;
 * 	false otherwise
 * 	Default: true
 * 
 * initMethod {function} - optional 
 * 	the function that must be invoked as the initEditor function
 * 
 * onChangeMethod {function} - optional
 * 	the function that must be invoked as the initEditor function
 * 
 */

(function($){

	$.widget("ui.vv_editor", {
		
		init: function() {
			var self = this;
			var iframe = this.element; 
			if(iframe.attr('tagName').toLowerCase() != 'iframe')
				return;
			var name = iframe.attr('name');
			var o = self.options;
			iframe.attr({
				id			: name,
				src			: '',
				frameborder	: '0',
				scrolling	: 'no',
				width		: o.width,
				height		: (parseInt(o.height) + 5) + 'px'
			});
			if (o.editorType == 'text' || o.editorType == 'html') {
				o.type = o.editorType;
				o.editorType = null;
			}
			else {
				var select = $('#' + o.editorType)
					.change(self._getOnChangeEditorType());
			}
			if(o.loadOnStart)
				self.load();
		},
		
		destroy: function() {
			var select = $('#' + o.editorType).unbind();
			$.widget.prototype.destroy.call(self);
		},
		
		getHTML: function() {
			var self = this;
			var contentWindow = self.element.get(0).contentWindow;
			if (!$.isFunction(contentWindow.getHTML))
				throw new Error('getHTML not defined!');
			return contentWindow.getHTML();
		},

		setHTML: function(content) {
			var self = this;
			var contentWindow = self.element.get(0).contentWindow;
			if (!$.isFunction(contentWindow.setHTML))
				throw new Error('setHTML not defined!');
			contentWindow.setHTML(content);
		},

		/**
		 * @param type {string} html | text
		 * 
		 */
		load: function() {
			var self = this;
			var o = self.options;
			var src = o.editorPath;
			var iframe = self.element;
			var name = iframe.attr('name');
			var initMethod =  name + 'InitMethod';
			window[initMethod] = self._getOnInitMethod();
			switch(self._getEditorType()) {
				case 'text':
					src += '?editorImpl=simple';
					src += '&initMethod=' + initMethod;
					if(o.onChangeMethod)
						src += '&onChangeMethod=' + o.onChangeMethod; 
					break;
				case 'html':
					src += '?p_l_id=' + o.plid;
					src += '&p_main_path=' + o.p_main_path;
					src += '&doAsUserId=' + o.doAsUserId;
					src += '&editorImpl=fckeditor';
					src += '&cssPath=' + o.cssPath;
					src += '&cssClasses=' + o.cssClasses;
					src += '&initMethod=' + initMethod;
					if(o.toolbarSet)
						src += '&toolbarSet=' + o.toolbarSet;
					if (o.onChangeMethod) {
						var onChangeMethod = name + 'OnChangeMethod';
						window[onChangeMethod] = o.onChangeMethod;
						src += '&onChangeMethod=' + onChangeMethod;
					} 
					break;
			}
			iframe.attr('src', src);
		},
		
		_getEditorType: function() {
			var o = this.options;
			var type = o.type;
			if (o.editorType != null) {
				var select = $('#' + o.editorType);
				if (select.length > 0) 
					type = select.val();
			} 
			return type == 'html' ? type : 'text';
		},
		
		_getOnChangeEditorType: function() {
			var iframe = this.element;
			return function() {
				iframe.vv_editor('load');
			}
		}, 
		
		_initMethod: function() {
			return '';
		},
		
		_getOnInitMethod: function() {
			var self = this;
			var initMethod = self.getData('initMethod');
			if ($.isFunction(initMethod)) 
				return initMethod;
			return function() {return '';}
		}
	});
	
	$.extend($.ui.vv_editor, {
		getter: ['getHTML'],
		defaults: {
			editorPath	: '/html/js/editor/editor.jsp',
			width		: '640',
			height		: '400',
			toolbarSet	: 'liferay',
			type		: 'text',
			loadOnStart	: true
		}
	});

	
 })(jQuery);