/*  Main
 *  Este fichero contiene la declaración de la clase Main.
 *  Supune a su vez el hilo principal JavaScript de la página Web.
 */


// Constantes

// Dirección de las WebPages
var WEB_PAGES_SRC =
	{
		HOME:			"home.html",
		CONTACTO:		"contacto.html",
		PORTAFOLIO:		"portafolio.html",
		BLOG:			"blog.html"
	};



// Evento onLoad -> Main.onLoad
EH.addEventHandler(window, "load", function() { new Main });

/* Constructor 
 * En esta función se define el comportamiento JS, cuando se ha cargado la página (body.onLoad)
 */
function Main()
{
	var self = this;
	this.xlayer = false;
	this.div_cont = document.getElementById("rbcontent");

	this.web_pages =
		{
			home:			new WebPage(this.div_cont, new Home, WEB_PAGES_SRC.HOME),
			portafolio:		new WebPage(this.div_cont, false, WEB_PAGES_SRC.PORTAFOLIO),
			contacto:		new WebPage(this.div_cont, new Contacto, WEB_PAGES_SRC.CONTACTO),
			blog:			new WebPage(this.div_cont, false, WEB_PAGES_SRC.BLOG)
		};

	this.current_web_page = this.web_pages.home;

	for(var wp in this.web_pages)
	{
		this.web_pages[wp].attachEvent("onLoadContentStart", function() { self.loadingStart(); });
		this.web_pages[wp].attachEvent("onLoadContentFinish", function() { self.loadingStop(); });
	}

	this.web_pages.home.loadPage();

	this.setEvents();

}

Main.prototype =
{
	setEvents:			function()
	{
		var self = this;
		EH.addEventHandler(document.getElementById("linker"), "click", function() { self.changePage(EH.getEvent()); }, 50);
	},

	loadingStart:		function()
	{
		this.xlayer = new XLayer(document.body, "xlayer", 50);
	},

	loadingStop:		function()
	{
		this.xlayer.stop(5);
	},

	changePage:			function(event)
	{
		if(EH.getActive(document.getElementById("linker")) && event.target.parentNode.getAttribute("id"))
		{
			this.current_web_page.unloadPage();
			switch(event.target.parentNode.getAttribute("id"))
			{
				case "link_home":
					this.current_web_page = this.web_pages.home;
					break;

				case "link_portafolio":
					this.current_web_page = this.web_pages.portafolio;
					break;

				case "link_contacto":
					this.current_web_page = this.web_pages.contacto;
					break;

				case "link_blog":
					this.current_web_page = this.web_pages.blog;
					break;
			}
			this.current_web_page.loadPage();
		}
	}
}

