/**
 * Generic menu functions
 */

function handleMenuMouseOver(menuItem) {
	parentElement = menuItem.parentNode;
	selectMenuItem(parentElement, menuItem);
}

function handleMenuMouseOut(menu) {
	selectMenuItem(menu, null);
}

function handleMenuMouseClick(menuItem) {
	/* Find the first A element inside this menu item and browse to that location */
	var elements = menuItem.getElementsByTagName('a');
	if (elements.length > 0) {
		window.location = elements[0].href;
	}
}

function getElementChildren(element) {
	var children = null;
	try {
		children = element.childElements();
	} catch (e) {
		children = element.children;
	}

	return children;
}

function getAllChildren(element) {
	var elements = [];

	for (var i = 0; i < getElementChildren(element).length; i++) {
		var child = getElementChildren(menu)[i];
		elements += getElementChildren(child);
	}
	return elements;
}

function selectMenuItem(menu, menuItem, className) {
	/* Add mouseover the menuItem elements class, remove it from all other elements within the menu */

	for (var i = 0; i < getElementChildren(menu).length; i++) {
		var element = getElementChildren(menu)[i];
		if (element == menuItem) {
			if (element.className.indexOf('mouseover') == -1) {
				element.className += ' mouseover';
			}
		} else {
			element.className = element.className.replace(/mouseover/ , '');
		}
	}
}
