var RichTextManager = Class.create({
	doCreate: function(richTextId) {
		var hiddenField = $(richTextId);
		var content = hiddenField.defaultValue;
		var w = $(richTextId + '_iframe').contentWindow;
		var doc = w.document;
		try {
			doc.designMode = "on";
		}
		catch(e) {
			setTimeout("$('" + richTextId + "_iframe').contentWindow.document.designMode = 'on';",100);
		}
		doc.open();
		doc.write('<html><head><title></title></head><body style="font-family: Lucida, Verdana, sans-serif; font-size:11px;">' + content + '</body></html>');
		doc.close();
		
		// Registrar un evento en el submit, para actualizar el campo de texto asociado
		var frm=hiddenField.parentNode;
		while ( frm && frm.nodeName != 'FORM') {
			frm=frm.parentNode;
		}
		if( frm ) {
			Element.observe(frm,'ws:updateText',function(event){ richText.updateTextField(richTextId); });
		}
		w.focus();
	},

	create: function(richTextId) {
		setTimeout('richText.doCreate(\'' + richTextId + '\')',1000);
	},
	
	updateTextField: function(richTextId) {
		var hiddenField = $(richTextId);
		var iframe = $(richTextId + '_iframe');
		var w = iframe.contentWindow;
		hiddenField.value = w.document.body.innerHTML;
	},
	
	updateRichText: function(richTextId) {
		var hiddenField = $(richTextId);
		var iframe = $(richTextId + '_iframe');
		var w = iframe.contentWindow;
		w.document.body.innerHTML = hiddenField.value;
	},
	
	execCommand: function(richText,command,interface,params) {
		var w = $(richText + '_iframe').contentWindow;
		w.document.execCommand(command,interface,params);
		w.focus();
	},
	
	insertImage: function(richTextId) {
		system.getPopUp().setContent('Loading data...'); 
		system.getPopUp().setContentWithURL(system.getLibraryPath() + 'plasticbriqFramework/actions/_rich_text_actions.php',
											{command:'printImagePicker',style:system.getCurrentStyle(),richTextId:richTextId});
		system.getPopUp().open();
	},
	
	insertLink: function(richTextId) {
		system.getPopUp().setContent('Loading data...'); 
		system.getPopUp().setContentWithURL(system.getLibraryPath() + 'plasticbriqFramework/actions/_rich_text_actions.php',
											{command:'printLinkPicker',style:system.getCurrentStyle(),richTextId:richTextId});
		system.getPopUp().open();
	},
	
	insertHTMLClip: function(richTextId) {
		system.getPopUp().setContent('Loading data...');
		system.getPopUp().setContentWithURL(system.getLibraryPath() + 'plasticbriqFramework/actions/_rich_text_actions.php',
											{command:'printHTMLClipPicker',style:system.getCurrentStyle(),richTextId:richTextId});
		system.getPopUp().open();
	},

	insertActionText: function(richTextId,formId,command) {
		var params = $(formId).serialize(true);
		params.command = command;
		params.style = system.getCurrentStyle();
		new Ajax.Request(system.getLibraryPath() + 'plasticbriqFramework/actions/_rich_text_actions.php',{
			method:'post',
			parameters:params,
			onSuccess:function(transport) {
				richText.insertAtEnd(richTextId,transport.responseText);
			}
		});
	},

	insertImageText: function(richTextId,formId) {
		var params = $(formId).serialize(true);
		params.command = 'getInsertImageText';
		params.style = system.getCurrentStyle();
		new Ajax.Request(system.getLibraryPath() + 'plasticbriqFramework/actions/_rich_text_actions.php',{
			method:'post',
			parameters:params,
			onSuccess:function(transport) {
				richText.insertAtEnd(richTextId,transport.responseText);
			}
		});
	},
	
	insertLinkText: function(richTextId,formId) {
		var params = $(formId).serialize(true);
		params.command = 'getInsertLinkText';
		params.style = system.getCurrentStyle();
		new Ajax.Request(system.getLibraryPath() + 'plasticbriqFramework/actions/_rich_text_actions.php',{
			method:'post',
			parameters:params,
			onSuccess:function(transport) {
				richText.insertAtEnd(richTextId,transport.responseText);
			}
		});
	},

	insertAtEnd: function(richText,appendText) {
		var w = $(richText + '_iframe').contentWindow;
		w.document.body.innerHTML = w.document.body.innerHTML + appendText;
		w.focus();
	},
	
	setColor: function(richText,format) {
		var w = $(richText + '_iframe').contentWindow;
		w.document.execCommand('foreColor',false,format);
		w.focus();
	},
	
	setStyle: function(richText,format) {
		var w = $(richText + '_iframe').contentWindow;
		w.document.execCommand('formatBlock',false,format);
		w.focus();
	},

	setFont: function(richText,font) {
		var w = $(richText + '_iframe').contentWindow;
		w.document.execCommand('fontname',false,font);
		w.focus();
	},

	setFontSize: function(richText,size) {
		var w = $(richText + '_iframe').contentWindow;
		w.document.execCommand('fontsize',false,size);
		w.focus();
	},
	
	showText: function(richTextId) {
		this.updateRichText(richTextId);
		$(richTextId + '_iframeContainer').show();
		$(richTextId + '_warningMessage').hide();
		$(richTextId).hide();
		$('richTextTabButonText_' + richTextId).className = 'richTextTabButtonSelected';
		$('richTextTabButonHTML_' + richTextId).className = 'richTextTabButton';
	},
	
	showHTML: function(richTextId) {
		this.updateTextField(richTextId);
		$(richTextId + '_iframeContainer').hide();
		$(richTextId).show();
		$('richTextTabButonText_' + richTextId).className = 'richTextTabButton';
		$('richTextTabButonHTML_' + richTextId).className = 'richTextTabButtonSelected';
	}
});

var richText = new RichTextManager();