// Francisco Javier Martínez
// francisco_javier_martinez@hotmail.com
// www.javiscript.com
   
// *********************************

// Clase que crea un CONTROLADOR (handler) para manejar el FCKEditor

function LIB_fckeditor_object ()
{
	var FCK_EDITOR_ID = "fckEditor_container";
	var FCK_EDITOR_WIDTH = "100%";
	var FCK_EDITOR_HEIGHT = "300px";
	
	var actual_post_id = null;
	/* **************************************************************************************************
		INIT - Constructor del a clase
	*/
	
	this.init = function (POST_ID)
	{
		jQuery(
			function()
			{
				jQuery("#openEditor_" + POST_ID).click(
					function(event)
					{
						// Eliminamos al activación del Evento del enlace (para el burbujeo de eventos)
						event.preventDefault();
						
						// Si no existe el contenedor del editor FCK Editor, lo creamos
						if (!actual_post_id)
						{
							create_FCKeditor_container(POST_ID);
							jQuery("#" + FCK_EDITOR_ID).slideToggle("slow", LIB_fckeditor.instance_FCKeditor);
						}
						else
						{
							// Si estamos pinchando para abrir el MISMO editof FCK Editor...
							if (POST_ID == actual_post_id)
							{
								// Cerramos el editor FCK Editor
								jQuery("#" + FCK_EDITOR_ID).slideUp("slow", 
									function ()
									{
										delete_FCKeditor_container();
									}
								);
							}
							else // Si estamos pinchando para abrir OTRO editor FCK Editor
							{
								// Cerramos el Actual
								jQuery("#" + FCK_EDITOR_ID).slideUp("slow", 
									function ()
									{
										// Eliminamos la referencia sobre el editor
										delete_FCKeditor_container();

										// Creamos de nuevo el contenedor del editor FCK Editor
										create_FCKeditor_container(POST_ID);
										jQuery("#" + FCK_EDITOR_ID).slideToggle("slow", LIB_fckeditor.instance_FCKeditor);
									}
								);
							}
						}
					}
				);
			}
		);
	}
	
	
	/* **************************************************************************************************
		Función privada que CREA el contenedor del FCK Editor
	*/
	function create_FCKeditor_container (POST_ID)
	{
		var actual_LI = document.getElementById("li_post_" + POST_ID);
		if (actual_LI)
		{
			var FCKEditor_container = document.createElement("DIV");
			FCKEditor_container.id = FCK_EDITOR_ID;
			FCKEditor_container.style.width = FCK_EDITOR_WIDTH;
			FCKEditor_container.style.height = FCK_EDITOR_HEIGHT;
			FCKEditor_container.style.display = "none";
			actual_LI.appendChild(FCKEditor_container);
			
			// Asignamos el ID del POST actual al controlador de Capas
			actual_post_id = POST_ID;
		}
	}
	
	/* **************************************************************************************************
		Función privada que ELIMINA el contenedor del FCK Editor
	*/
	function delete_FCKeditor_container ()
	{
		var actual_FCKeditor_container = document.getElementById(FCK_EDITOR_ID);
		if (actual_FCKeditor_container)
		{
			var actual_LI_container = actual_FCKeditor_container.parentNode;
			actual_LI_container.removeChild(actual_FCKeditor_container);
			
			actual_post_id = null;
		}
	}
	
	/* **************************************************************************************************
		Función pública que CARGA por medio de AJAX el editor FCK Editor
	*/
	this.instance_FCKeditor = function ()
	{
		var url = "fckeditor_AJAX_controller.php?flagControl=instanceEditor";
		var myAjax = new Ajax.Updater(FCK_EDITOR_ID, url, {
																method:"get"
															}
										);
	}
	
	/* **************************************************************************************************
		Función privada que lanza el Cargador del FCK Editor
	*/
	function FCKeditor_init_loader ()
	{
		// Cargamos el Loader
		var url = "fckeditor_AJAX_controller.php?flagControl=initEditorLoader";
		var myAjax = new Ajax.Updater(FCK_EDITOR_ID, url, {
																method:"get"
															}
										);
	}
}

var LIB_fckeditor = new LIB_fckeditor_object();