// JavaScript Document

var Menu = new Class({
	Implements: [Options, Events],
	options:
	{
		currHash:false
	},
	initialize:function(options)
	{
		this.setOptions(options)
		this.hashChange();
		this.menuOver();
	//	this.mainBackBtn();
	},
	mainBackBtn:function()
	{
		document.id("mainBackBtn").addEvent("click",function(){
			this.goBack();
		}.bind(this));
	},
	menuOver:function()
	{
		$$("#menuContainer .menuItem").each(function(m){
			m.addEvent("mouseover",function(){
				var coord = m.getCoordinates(document.id("menuContainer"));
				document.id("menuOver").morph({"margin-left":(coord.left-5)});
				document.id("menuOver").removeEvents();
				document.id("menuOver").addEvent("click",function(){
					this.changeHash("#/"+m.id.substr(4));
				}.bind(this));
			}.bind(this));
		}.bind(this));
	},
	setCurrentSec:function(sec)
	{
		c("menu"+sec);
		var coord = document.id("menu"+sec).getCoordinates(document.id("menuContainer"));
		document.id("menuOverActiveLine").setStyles({"margin-left":coord.left-5});
		document.id("menuOverActiveBox").setStyles({"margin-left":coord.left-5});
	},
	hashChange:function()
	{
		window.addEvent('hashchange',function(hash){
				this.goToSection(hash,'h');
		//alert(hash);
			}.bind(this));
			
		window.addEvent('domready',function(){
				var hash = window.location.hash;
				this.goToSection(hash,'dr');
			}.bind(this));
	},
	changeHash:function(hash)
	{
		window.sethash(hash);
	},
	reReloadHash:function()
	{
		c("-------------------------------------------------");
		var h = window.location.hash;
		window.sethash("#");
		window.sethash.delay(1,null,[h]);
	},
	goToSection:function(h,from)
	{
		//this.clearSection();
		var inicio = new Inicio();
		if(h==="" || h==="#" || h==="#/")
		{
			inicio.hi();
			this.setCurrentSec("");
		}
		else
		{
//			this.menuHide();
			inicio.bye();
			if(h.test("productos"))
			{
				var productos = new Productos();
				if(h.test("cat"))
				{
				//	catId=h.substr(16);
				//	productos.getCategorias(catId);
				}
				this.setCurrentSec("productos");
			}
			else
			{
				
			}
			if(h.test("nosotros"))
			{
				var nosotros = new Nosotros();
				this.setCurrentSec("nosotros");
			}
			if(h.test("prensa"))
			{
				var prensa = new Prensa();
				this.setCurrentSec("prensa");
			}
			if(h.test("clientes"))
			{
				var clientes = new Clientes();
				this.setCurrentSec("clientes");
			}
			if(h.test("encuentra"))
			{
				var encuentra = new Encuentra();
				this.setCurrentSec("encuentra");
			}
			if(h.test("contactenos"))
			{
				var contactenos = new Contacto();
				this.setCurrentSec("contactenos");
			}
		}
		c("hash: "+h);
		c("from: "+from);
		this.options.currHash=h;
		
	},
	menuHide:function()
	{
		document.id("menuBg").removeEvents();
		document.id("footerBg").removeEvents();

		document.id("menuOver").setStyles({"opacity":0});
		document.id("menuOverActiveBox").morph({"opacity":0});
		document.id("footerBg").morph({"background-color":"#000"});
		$$("#menuContainer .menuItem,.menuSeparador").each(function(m){
			m.morph({"opacity":0});
		});
		
		document.id("menuBg").addEvent("mouseover",function(){
			this.menuShow();
		}.bind(this));
		document.id("footerBg").addEvent("mouseover",function(){
			this.menuShow();
		}.bind(this));
		
		
	},
	menuShow:function()
	{
		$$("#menuContainer .menuItem,.menuSeparador").each(function(m){
		m.morph({"opacity":1});
		});
		document.id("menuOver").setStyles({"opacity":1});
		document.id("menuOverActiveBox").morph({"opacity":1});
		document.id("footerBg").morph({"background-color":"#F00"});
		
			document.id("menuBg").addEvent("mouseout",function(){
				this.menuHide();
			}.bind(this));
			document.id("footerBg").addEvent("mouseout",function(){
				this.menuHide();
			}.bind(this));
	},
	loadCaja:function(msj)
	{
		$("contenido").set("html","<div class='cajaLoading'>"+msj+"</div>");
	},
	setCaja:function(d)
	{
		document.id("contenido").set("html",d);
	},
	clearSection:function()
	{
		this.setCaja("");
	//	document.id("menuRight").set("html","");
	},
	showMenuBtnEvent:function()
	{
		this.menuShow('menuLeft');	
	},
	goBack:function(op)
	{
		if(!op)
		{
			op = -1
		}
		history.go(op);
	},
	goBackBtn:function()
	{
		document.id("goBackBtn").addEvent("click",function(){
			this.goBack();
			}.bind(this));
	},
	goto:function(u)
	{
		window.document.location=u;
	},
	rsReload:function()
	{
		window.location.reload(true);
	},
	meterCargando:function(obj)
	{
		document.id(obj).set("html","<div class='cargando'></div>");
	}
});
