/*
	menu.js
	
	©2007 Sound Evolution Pty Ltd
	
	
*/


var SoundEvolutionMenus = new Object();
document.SoundEvolutionMenus = SoundEvolutionMenus;

function debugLog( x ) {
	var d = document.getElementById('debug');
	if (d) d.innerHTML += x + "     \n";
}

SoundEvolutionMenus.getParentOfType = function (object, type) {
	
	while( object ) {
		if (object.nodeName == type ) return object;
		object = object.parentNode;
	}
	return object;
}


SoundEvolutionMenus.showSubMenu = function ( event, id ) {
	// show the menu <ul> with id == id
	
	var target = event.target || event.srcElement;
	
	target = this.getParentOfType( target, 'LI' );
	if (!target) return false;
	
	// debugLog( 'MouseOver = ' + target + '      ' );
	
	// if there is a current menu, and we're moving into another one,
	// hide it immediately.
	if (this._currentMenu && this._currentMenu != target) {
		this.hideMenu();
	}
	
	// cancel any pending timeout function to hide a menu
	this.cancelDelayHide();
	
	// get the current menu's submenu tag
	var menu = document.getElementById( id );
	if (menu) {
		
		// show the menu, and remember it
		menu.style.display='block';
		this._currentMenu = menu;
		
		// assign a mouseout function to the displayed menu
		menu.onmouseout = function(event) { SoundEvolutionMenus.delayHideMenu(event); }
	}
	
	// assign a mouse out function to the menu
	target.onmouseout = function(event) { SoundEvolutionMenus.delayHideMenu(event); }
}


SoundEvolutionMenus.hideMenu = function() {
	
	// debugLog( 'hideMenu' );
	
	if (this._currentMenu) {
		// debugLog( 'hidden!' );
		this._currentMenu.style.display = 'none';
	}
	this._currentMenu = null;
}


SoundEvolutionMenus.delayHideMenu = function() {
	// if there is a current submenu being displayed, start a timer to hide it
	if (this._currentMenu) {
		this._delayHideMenu = this._currentMenu;
		this._delayHideTimeout = window.setTimeout( 'SoundEvolutionMenus.doDelayHide()', this._delayTime );
	}
}


SoundEvolutionMenus.doDelayHide = function() {
	// debugLog( 'doDelayHide' );
	if (this._delayHideMenu && this._currentMenu && this._currentMenu == this._delayHideMenu) {
		this.hideMenu();
	}
	this.cancelDelayHide();
}


SoundEvolutionMenus.cancelDelayHide = function() {
	// debugLog( 'cancelDelayHide' );
	if (this._delayHideMenu) this._delayHideMenu.onmouseout = null;
	this._delayHideMenu = null;
	window.clearTimeout( this._delayHideTimeout );
}


SoundEvolutionMenus._currentMenu = null;
SoundEvolutionMenus._delayTime = 600;	// milliseconds
