var menu = {
	goback: function(el) {
		el.addEvent('mouseleave', function() {
			if (el.active) return false;
			this.neato.start.delay(500, this.neato, {
				'padding-top': 0,
				'padding-bottom': 20,
				'color': '#aaa'
			});
		});
	},
	gobacknow: function() {
		$$('ul.nav_top>li>a').each(function(el){
			if (el.active) return false;
			el.neato.start({
				'padding-top': 0,
				'padding-bottom': 20,
				'color': '#aaa'
			});
		});
	},
	gohalfway: function(el) {
		if (!el) return false;
		el.addEvent('mouseenter', function() {
			if (el.active) return false;
			this.neato.stop();
			this.neato.start({
				'padding-top': 20,
				'padding-bottom': 0,
				'color': '#666'
			});
		});
	},
	goalltheway: function(el) {
		if (!el) return false;
		el.addEvent('click', function(e) {
			el.blur();
			if (el.active) return false;
			this.neato.stop();
			this.neatoFast.stop();
			this.neatoFast.start({
				'padding-top': 20,
				'padding-bottom': 25,
				'color': '#900'
			}).chain(function(){
				menu.subShow(el);
			});
			// Reset active to current
			menu.stripActive();
			el.active = true;
			// Send others back
			menu.gobacknow();
			// Hide open subnavs, show new subnav
			menu.subHide();
			//menu.subShow(el);
		});
	},
	subShow: function(el) {
		//alert(el.getNext().getTag());
		var next = el.getNext();
		if (next.getTag() == 'ul') {
			next.setStyles({'display':'block', 'opacity':0});
			var subNeato = new Fx.Styles( next, {duration: 100,transition: Fx.Transitions.Cubic.easeOut });
			subNeato.start({
				'opacity': 1
			});
		};
	},
	subHide: function() {
		$$('ul.nav_top>li>ul').each(function(el){
			var subNeato = new Fx.Styles( el, {duration: 100,transition: Fx.Transitions.Cubic.easeOut });
			subNeato.start({
				'opacity': 0
			});
		});
	},
	stripActive: function() {
		$$('ul.nav_top>li>a').each(function(el){
			el.active = false;
		});
	},
	setEvents: function() {
		// var elements = $$('ul.nav_top li a');
		$$('ul.nav_top>li>a').each(function(el){
			// Strip href links from top level nav items if they have children
			var parent = el.getParent();
			var children = parent.getChildren().filterByTag('ul');
			if (children.length>0) {
				el.setAttribute('href','javascript:;');
			};
			if (parent.hasClass('here')) {
				//el.addClass('active');
				el.active = true;
			};
			
			// Make one Fx instance, not 3 separate based on event. This way, conflicting events won't cause jitters
			el.neato = new Fx.Styles( el, { duration: 250,transition: Fx.Transitions.Cubic.easeOut });
			el.neatoFast = new Fx.Styles( el, { duration: 100,transition: Fx.Transitions.Cubic.easeOut });
			// Add the events
			menu.goback(el);
			menu.gohalfway(el);
			menu.goalltheway(el);
		});
	},
	init: function() {
		menu.setEvents();
	}
};

var forms = {
	setDefaultText: function() {
		$$('form .required').each(function(el){
			if (el.value != '') {
				var old = el.value;
				el.addEvents({
					'focus': function() {
						if (el.value == old) {
							el.value = '';
						}
					},
					'blur': function() {
						if (el.value == '') {
							el.value = old;
						}
					}
				});
			}
		});
	}
};

// Best way (in MOO) to perform onload tasks, only this is onDomReady
// If domready is premature, change to 'load'
window.addEvent('domready',function(){
	menu.init();
	forms.setDefaultText();	
});