var MENU = {
	objs:[],
	actual:[],
	idDelay:null,
	intervaloParaDesaparecerMenu:500,
	anchoWindow:0,
	offSetHorizontal: 5,
	offSetVertical: 5,
	
	saludo:function(){
		alert("chau");
	},
	
	start:function(){
		
		/***/
		MENU.anchoWindow = window.getWidth();
		
		/***/		
		
		var rel = "";
		$$(".btnMenu").each(function(obj){
			rel = obj.getProperty("rel")
			MENU.objs[rel] = $(rel);
			obj.addEvent("mouseover",function(){
				MENU.quitarMenu(0);
				var rel = this.getProperty("rel");
				if(MENU.objs[rel]!=null && MENU.objs[rel] != undefined){
					this.addClass('estadoHover');
					var coord = this.getCoordinates();
					//MENU.objs[rel].setStyle("width",(coord.width-2));
					MENU.objs[rel].setStyle("left",coord.left);
					MENU.objs[rel].setStyle("top",((coord.top+coord.height)+2));
					MENU.objs[rel].setStyle("display","block");
					MENU.actual[0] = MENU.objs[rel];
				}
			});
			
			obj.addEvent("mouseout",function(){
				MENU.idDelay =  MENU.quitarMenu.delay(MENU.intervaloParaDesaparecerMenu,MENU,0);
			});
		});
		
		$$(".subMenu").each(function(obj){
			obj.addEvent("mouseover",function(){
				MENU.idDelay = $clear(MENU.idDelay);
			});
			
			obj.addEvent("mouseout",function(){
				MENU.idDelay =  MENU.quitarMenu.delay(MENU.intervaloParaDesaparecerMenu,MENU,0);
			});
		});
		
		
		$$(".btnSubMenu").each(function(obj){
			rel = obj.getProperty("rel")
			MENU.objs[rel] = $(rel);
			obj.addEvent("mouseover",function(){
				pos= $(obj.parentNode.parentNode.parentNode).getProperty("rel");
				
				MENU.quitarMenu(pos);
				
				var rel = this.getProperty("rel");
				if(MENU.objs[rel]!=null && MENU.objs[rel] != undefined){
					this.addClass('estadoHover');
					var coord = this.getCoordinates();
										
					MENU.objs[rel].setStyle("top",coord.top+MENU.offSetVertical);
					MENU.objs[rel].setStyle("left",(coord.left+coord.width-MENU.offSetHorizontal));
					MENU.objs[rel].setStyle("display","block");	
					
					
					
					var coordProx = MENU.objs[rel].getCoordinates();
					
					//alert(coordProx.width);
					
					//alert(MENU.anchoWindow+" - "+(coord.left+coord.width+coordProx.width));
					
					if(MENU.anchoWindow<=(coord.left+coord.width+coordProx.width)){
						//alert("entre");
						MENU.objs[rel].setStyle("left",(coord.left-coord.width+MENU.offSetHorizontal));
						MENU.objs[rel].setStyle("top",coord.top+MENU.offSetVertical);
					}
					
					MENU.actual[pos] = MENU.objs[rel];
				}
			});
		});
	},
	
	quitarMenu:function(pos){
		MENU.idDelay = $clear(MENU.idDelay);
		MENU.actual.filter(function(item, index){
			return index >= pos;
		}).each(function(obj){
			obj.setStyle("display","none");
			$(obj.id+"_btn").removeClass('estadoHover');
			MENU.actual.remove(obj);
		});
	}
}