var JbbMenu = {
	
	init: function(){
		//We try to find a occurence of the current href in the menu to activate it
		var mainas = $ES('a', 'mainnav');
		var currhref = window.location.href;
		for (var i=0; i<mainas.length; ++i) {
			if(mainas[i].href == currhref){
				this.setCurrentsFromElement(mainas[i].getParent());
				break;
			}
		}
		
		this.initnav = this.currentnav;
		this.initsubnav = this.currentsubnav;
		this.initsubnavitem = this.currentsubnavitem;
		
		this.activeInitCurrent();
		
		var mouseOverHandler = function(el){
			this.hideSubNav();
			this.setCurrentsFromElement(el);
			this.showSubNav();
			this.clearTimeOut(this.timeoutid);
		};
		
		var mouseOutHandler = function(event) {
			this.clearTimeOut(this.timeoutid);
			this.timeoutid = setTimeout( this.restore.bind(this), 2000);
		};
		
		$A($ES('li.nav', 'mainnav')).each(function(currLi){
			currLi.addEvent('mouseover', mouseOverHandler.pass(currLi,this));
			currLi.addEvent('mouseout', mouseOutHandler.bindWithEvent(this));
			$A($ES('ul.subnav', currLi)).each(function(subLi){
				subLi.addEvent('mouseover', mouseOverHandler.pass(currLi, this));
				subLi.addEvent('mouseout', mouseOutHandler.bindWithEvent(this));
			}, this);
		}, this);
	},

	setCurrentsFromElement: function(element){
		if(element.className.indexOf("subnavitem") >= 0){
			this.currentsubnavitem = element;
		 	this.currentsubnav = this.currentsubnavitem.getParent();
			if(this.currentsubnav !== null){
				this.currentnav = this.currentsubnav.getParent();
			}
		}
		else if(element.className.indexOf('subnav') >= 0){
			this.currentsubnav = element;
			this.currentnav = this.currentsubnav.getParent();
			this.currentsubnavitem = null;
		}
		else if(element.className.indexOf('nav') >= 0){
			this.currentnav = element;
			this.currentsubnav = $E('ul', this.currentnav);
			this.currentsubnavitem = null;
		}
	},
	
	activeInitCurrent: function(){
		this.currentnav = this.initnav;
		this.currentsubnav = this.initsubnav;
		this.currentsubnavitem = this.initsubnavitem;
		this.showSubNav();
	},
	
	
	restore: function() {
		this.clearTimeOut(this.timeoutid);
		this.hideSubNav();
		this.activeInitCurrent();
	},
	
	hideSubNav: function() {
		if (this.currentsubnav){
			this.currentsubnav.style.display = "none";
		}
		if (this.currentnav){
			this.currentnav.className = this.currentnav.className.replace(/[ ]?hover/, '');
		}
		if (this.currentsubnavitem){
			this.currentsubnavitem.className = this.currentsubnavitem.className.replace(/[ ]?hover/, '');
		}
	},
	
	showSubNav: function() {
		if (this.currentsubnav){
			this.currentsubnav.style.display = "block";
		}
		if (this.currentnav){
			this.currentnav.className += ' hover';
		}
		if(this.currentsubnavitem){
			this.currentsubnavitem.className += ' hover';
		}
	},
	
	clearTimeOut: function(timeoutid){
		clearTimeout(timeoutid);
		this.timeoutid = 0;
	}

};

window.addEvent('domready', JbbMenu.init.bind(JbbMenu));
