document.observe("dom:loaded", function() {

	var currentPage = document.body;
	
	//GLOBAL VARS
	var timer;
	
	//EVENT LISTENERS
	if( $(currentPage).hasClassName('parents') || $(currentPage).hasClassName('donors')) {
		//do nothing
	} else {
		
		$('nav-sub-donors').setStyle({ display: 'none' });
		$('nav-sub-parents').setStyle({ display: 'none' });

		$('btn-parents').observe('mouseover', subnavShow);
		$('btn-parents').observe('mouseout', subnavTimout);
		
		$('btn-donors').observe('mouseover', subnavShow);
		$('btn-donors').observe('mouseout', subnavTimout);
		
		$('nav-sub').observe('mouseover', subnavOut);
		$('nav-sub').observe('mouseout', subnavOver);
		
	}
		
	//EVENT HANDLERS
	//show parents or donors subnav
	function subnavShow(event) {
		
		var el = Event.element(event).id;
		_clearTimer();
		
		if(el == 'btn-parents') {
			$('nav-sub-donors').hide();
			$('nav-sub-parents').appear({ duration: .5 });
		}
		
		if(el == 'btn-donors') {
			$('nav-sub-parents').hide();
			$('nav-sub-donors').appear({ duration: .5 });
		}
		
	}
	//set timer to hide subnav
	function subnavTimout(event) {
		
		var el = Event.element(event).id;
		timer = setTimeout(function () { subnavHide(el) } ,200);
		
	}
	//hide parents or donors subnav
	function subnavHide(el) {
		
		$('nav-sub-parents').hide();
		$('nav-sub-donors').hide();
		
	}
	//over subnav
	function subnavOver(event) {
		
		var el = Event.element(event).id;
		_startTimer(el);
		
	}
	function subnavOut(event) {
		
		_clearTimer();
		
	}
	
	//PRIVATE FUNCTIONS
	//timer utility
	function _clearTimer() {
		
		clearTimeout(timer);
		
	}
	function _startTimer(el) {
		
		timer = setTimeout(function () { subnavHide(el) },200);
		
	}

});
