/* WebPages
 * Este fichero contiene la declaración de la clase WebPage, que será la que contenga
 * la estructura básica de las páginas HTML.
 */

/* Constructor
 * El constructor requerirá el div que la contendrá, la clase que la gestiona y la dirección del HTML asociado.
 * Cabe tener en cuenta que el div que la contenga será formateado cuando la página se borre.
 * Así pues, el div no debe contener NADA, sólo la página en un futuro.
 *
 * div_cont:		Div que contendrá la Página.
 * handler:			La clase que controla su comportamiento
 * html_src:		Dirección del archivo HTML.
 */
function WebPage(div_cont, handler, html_src)
{
	this.div_cont = div_cont;
	this.handler = handler;
	this.html_src = html_src;

	// Contenido del HTML (<html> ... </html>)
	this.html_content = false;

	this.loading = false;
	this.ready = false;

	this.events =
	{
		onLoadContentStart:			new Array,
		onLoadContentFinish:		new Array,
		onLoadPage:					new Array,
		onUnloadPage:				new Array
	}

}


WebPage.prototype =
{
	callEvents:		function(event)
	{
		// Se llamará la función con el objeto this como primer parámetro.
		if(this.events[event])
		{
			for(var e in this.events[event])
				this.events[event][e].call(this);
		}
	},

	callHandler:	function(func)
	{
		if(this.handler && this.handler[func])
			this.handler[func].call(this.handler, this);
	},

	
	attachEvent:	function(event, func)
	{
		this.events[event].push(func);
	},


	loadContent:	function()
	{
		if(this.html_src && !this.html_content)
		{
			this.callEvents("onLoadContentStart");
			this.loading = true;

			new net.ContentLoader(this.html_src, function(req)
			{
				this.html_content = req.responseText;
				
				this.loading = false;
				this.ready = true;
				
				this.callEvents("onLoadContentFinish");
			}, false, "", this);
		}
	},

	loadPage:		function()
	{
		if(this.div_cont && !this.loading)
		{
			if(this.ready)
			{
				this.div_cont.innerHTML = this.html_content;

				this.callHandler("onLoadPage");
				this.callEvents("onLoadPage");
			}
			else
			{
				this.attachEvent("onLoadContentFinish", function() { this.loadPage(); });
				this.loadContent();
			}
		}
	},

	unloadPage:		function()
	{
		if(this.div_cont)
		{
			while(this.div_cont.firstChild)
				this.div_cont.removeChild(this.div_cont.firstChild);

			this.callHandler("onUnloadPage");
			this.callEvents("onUnloadPage");
		}
	}
}




