// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function hideMenu () {
	if (this.menuElement() == null) return;
	
	if (this.timeout != null) {
		Element.hide (this.menuElement());
		this.timeout = null;
	}
}

Behaviour.register ({
	'.initial': function(el) {
		Element.hide(el);
		Element.removeClassName(el, 'initial');
	},
	
	'.link_level0': function(el) {
		el.menuElement = function() {
			if (!this._menuElement) 
				this._menuElement = $A(this.childNodes).detect (
					function (n) {
						return n.className && Element.hasClassName(n, 'children');
					});
			
			return this._menuElement;
		}
		
		el.onmouseover = function() {
			//this.style.backgroundColor = "#336699";
			
			/* More than one element means there is a tag - less is only whitespace */
			if (this.menuElement() == null || this.menuElement().childNodes.length <= 1) return;
			
			if (this.timeout) {
				clearTimeout(this.timeout);
				this.timeout = null;
			}
			
			Element.show(this.menuElement());
		}
		
		el.onmouseout = function() {
			//this.style.backgroundColor = "white";

			if (this.timeout) {
				clearTimeout(this.timeout);
				this.timeout = null;
			}
			
			this.timeout = setTimeout (hideMenu.bind(this), 100); 
		}
	}
});