function makeMenu(e) {
	var children = e.getElements('li');
	
	for (var i = 0; i < children.length; i++) {
		var child = children[i];

		child.lighter = child.effects({duration: 250, transition:Fx.Transitions.Quint.easeInOut});
		child.idleBgColor = child.getStyle('background-color');
		child.addEvent('mouseenter', highlightMenuItem.bindWithEvent(child));
		child.addEvent('mouseleave', downlightMenuItem.bindWithEvent(child));

		var sub = child.getElement('ul');
		if (sub) {
			child.subMenu = sub;
//			child.addClass('subMenuIndicator');
			child.setStyle('background-image', 'url("_img/subMenuIndicator.gif")');
			child.setStyle('background-repeat', 'no-repeat');
			child.setStyle('background-position', 'center right');
			
			sub.slider = new Fx.Styles(sub, {duration: 250, transition: Fx.Transitions.Quint.easeInOut});
			sub.setOpacity(0);
			sub.style.display = 'none';
			
			child.addEvent('mouseenter', openSubMenu.bindWithEvent(sub));
			child.addEvent('mouseleave', closeSubMenu.bindWithEvent(sub));
		};
		
		var link = child.getFirst();
		if (link.getTag() == 'span') {
			link = link.getFirst();
		}
		
		if (link != null || link != undefined) {
			child.href = link;
			child.addEvent('click', openLinkFromEvent.bindWithEvent(child));
		}
	};
}

function openLinkFromEvent(event) {
	if (event.target.href) {
		document.location = event.target.href;
	};
	event.stopPropagation();
}

function openSubMenu() {
	this.style.display = 'block';
	this.slider.stop();
	this.slider.start({'opacity': [0, 1]});
}

function closeSubMenu() {
	this.slider.stop();
	this.slider.start({'opacity': [1, 0]});
}

function highlightMenuItem() {
	this.lighter.stop();
	this.lighter.start({'background-color': '#f1f3f1'});
}

function downlightMenuItem() {
	this.lighter.stop();
	this.lighter.start({'background-color': this.idleBgColor});
}

var mainMenu = makeMenu($('mainMenu'));
