window.addEvent('domready', function(event){
		addCloseEvents();
		var loginForm = $('loginForm');
		if(loginForm){
			loginForm.addEvent('submit', function(e){
				if(!$defined(this.flag) || !this.flag){
					e.stop();
				}
				new Request({
					
					url: Constants.pathDomain+"registration/checkLogin",
					onSuccess: function(response){
						var data = JSON.decode(response);
						var log = $('loginFormLog').empty();
						log.set('text', data.msj);
						if(!data.error){
							loginForm.flag = true;
							loginForm.submit();
						}else{
							loginForm.flag = false;
						}
					},
					onFailure: function(){
						var log = $('loginFormLog').empty();
						log.set('text', Messages.requestFailed);
						e.stop();
					}
				}).send(loginForm.toQueryString());
			});
		}
		
		if($('subcontent2') && $('contentlink')){
			new Bubble('subcontent2', 'contentlink');
		}
		
		/** Menu Horizontal */
		var submenus = $$('.navmenu-h');
		
		/** Showing/Activating the Submenus*/
		
		$$("a.menu_horizontal").each(function(link){
			/*link.addEvent('click', function(e){
				var subid = link.getAttribute("rel");
				if(subid){
					var submenu = $(subid);
					if(submenu && submenu.getElements("a") && submenu.getElements("a").length > 0){
						e.stop();
					}
				}
				
			});*/
			link.addEvent('mouseover', function(e){
				$$(".menu_horizontal").removeClass("active");
				this.addClass("active");
				var subid = link.getAttribute("rel");
				if(subid){
					var submenu = $(subid);
					if(submenu && submenu.getElements("a") && submenu.getElements("a").length > 0){
						//e.stop();
						submenus.setOpacity(0);
						submenus.setStyle("z-index", -1);
						//submenu.setOpacity(1);
						submenu.setStyle("display", "block");
						submenu.morph({"opacity" : 1});
						submenu.setStyle("z-index", 2);
					}else{
						submenus.setOpacity(0);
						submenus.setStyle("z-index", -1);
						//submenu.setOpacity(1);
						submenu.setStyle("display", "block");
						submenu.morph({"opacity" : 1});
						submenu.setStyle("z-index", 2);
					}
				}
			});
		});
		
		//Activates the submenu link which matches the current location if none matches then it activates the first link
		var flag = false;
		$$(".submenu_horizontal").each(function(link){
			
			if(!flag && link.href == window.location){
				flag= true;
				var par = $("menu-horizontal-"+link.getParent().id.split("-").getLast()); 
				if(par){
					par.addClass("active");
					var submenu = $(par.getAttribute("rel"));
					if(submenu){
						submenus.setOpacity(0);
						submenus.setStyle("z-index", -1);
						link.getParent().setStyle("display", "block");
						link.getParent().setStyle("z-index", 2);
						link.getParent().morph({"opacity" : 1});
					}else{}
				}
				
			}
		});
		
		if(!flag){
			//As no submenus matched the location we now check the parent menu
			$$("a.menu_horizontal").each(function(link){
				if(!flag && link.href == window.location){
					flag= true;
					link.addClass("active");
					var submenu = $(link.getAttribute("rel"));
					if(submenu){
						submenus.setOpacity(0);
						submenus.setStyle("z-index", -1);
						link.getParent().setStyle("display", "block");
						link.getParent().setStyle("z-index", 2);
						link.getParent().morph({"opacity" : 1});
						submenu.setStyle("display", "block");
						submenu.morph({"opacity" : 1});
						submenu.setStyle("z-index", 2);
					}
				}
			});
			
			if(!flag){
				//As no menus matched the location we activate the first menu and its submenu
				try{
					var home = $("conteudo_menu_horizontal").getFirst().getFirst().getFirst().getFirst().getFirst().getFirst();
					home.addClass("active");
					var homesub = $(home.getAttribute("rel"));
					homesub.addClass("active");
					homesub.setStyle("display", "block");
					homesub.setStyle("z-index", 2);
					homesub.morph({"opacity" : 1}); 
				}catch(e){
				}
				
			}
				
		}
		/** End Menu Horizontal */
		
});