//-----------------------------------------------------------------------------
// Global Javascript library
//-----------------------------------------------------------------------------

/**
 * Returns the DOM node for the specified ID or returns false.
 * @id ID of a DOM node to retrieve
 */

function byId (id) {
	
	return document.getElementById(id);

}

/**
 * Switches the specified elements to 'block' (or 'inline') if specified.
 * @id 				ID or element node to show
 * @displayMode		Optional. CSS display value, default is 'block'
 */
 
function showElement (id, displayMode) {
	
	if (!displayMode) displayMode = "block";
	
	var node;
	
	// If ID is a string, look up by ID
	if (typeof(id) == "string") {
		node = byId(id);
	}
	// Otherwise assume id is a full node object
	else {
		node = id;
	}
	
	if (node) {
		node.style.display = displayMode;
	}

}

/**
 * Toggles an element between show and hide.
 * @id	ID of element to toggle
 */

function toggleElement (id) {
	
	var node;
	
	// If ID is a string, look up by ID
	if (typeof(id) == "string") {
		node = byId(id);
	}
	// Otherwise assume id is a full node object
	else {
		node = id;
	}
	
	if (node) {
		if (node.style.display == "block" 
				|| node.style.display == "inline") {
			hideElement(id);
		}
		else {
			showElement(id);	
		}
	}
	
}

/**
 * Switches the specified elements CSS display mode to 'none'.
 * @id	ID or element node to hide
 */

function hideElement (id) {
	
	var node;
	
	// If ID is a string, look up by ID
	if (typeof(id) == "string") {
		node = byId(id);
	}
	// Otherwise assume id is a full node object
	else {
		node = id;
	}

	if (node) {
		node.style.display = "none";	
	}

}


/**
 * Binds a hover menu according to the specified parameters.
 * @sources		String array of source DOM node IDs
 * @targets		String array of target DOM node IDs
 */

function bindMenu (sources, targets) {
	
	// Bind each source node to specified target node
	var sourceNode;
	var targetNode;
	
	for (var i = 0; i < sources.length; i++) {
		
		sourceNode 	= byId(sources[i]);
		targetNode 	= byId(targets[i]);
		
		sourceNode.menuTarget = targetNode.id;
		
		bind(sourceNode.id, "onmouseover", function () {showMenu(this.menuTarget)});
		bind(sourceNode.id, "onmouseout", function () {hideMenu(this.menuTarget)});
		bind(targetNode.id, "onmouseover", function () {showMenu(this.id)});
		bind(targetNode.id, "onmouseout", function () {hideMenu(this.id)});
		
	}

}

/**
 * Starts the hide timeout for the specified menu.
 * @id	ID of menu to hide
 */

function hideMenu (id) {
	
	var menu = byId(id);
	menu.thread = setInterval("hideElement('" + id + "');", 500);
	
}


/**
 * Shows the specified menu.
 * @id	ID of menu to show
 */

function showMenu (id) {
	
	var menu = byId(id);
	
	clearInterval(menu.thread);
	
	showElement(id);
	
}

/**
 * Binds a DOM nodes event to a specified function.
 * @source	ID of event source node
 * @event	String name of event, e.g. 'onmouseover'
 * @action	Function pointer of action
 */

function bind (source, event, action) {
	
	var sourceNode;
	
	if (typeof(source) == "string") {
		sourceNode = byId(source);		
	}
	else {
		sourceNode = source;
	}

	if (sourceNode) {
		var code = "sourceNode." + event + " = " + action + ";";
		eval(code);
	}
	
}