/* JavaScript Menus
 * CSS based, multi-level
 * ligthweight
 *
 * Author: Pedro Vale de Gato
 */

var selectedItem = null;
var selectedMenu = null;
var oldy = null;
var timeoutID = null;
var originalY = 0;
var menuList = new Array();
//the current menu level, used to close same depth menus when opening a sub-menu
var indent = 0;

function removeClassName(el, name) {

  var i, curList, newList;

  if (el.className == null)
    return;

  // Remove the given class name from the element's className property.

  newList = new Array();
  curList = el.className.split(" ");
  for (i = 0; i < curList.length; i++) {
    if (curList[i] != name && curList[i] != '') {
      newList[newList.length+1] = curList[i]; //FI : IE 5.0 doesn't support push !?
    }
  }
  el.className = newList.join(" ");
}


function itemMouseover(button, submenu) {
	x = button.offsetParent.offsetLeft + button.offsetWidth;
	y = button.offsetParent.offsetTop + button.offsetTop;
	
	if (button.offsetParent != menuList[menuList.length-1]) hideLastOpenedMenu();
	
	if (submenu != undefined) {
		if (x + 150 > screen.width) x = button.offsetParent.offsetLeft - getMenuWidth(submenu) - 3;
		showMenu(submenu, x, y);
	}

	//deselect all other menus and select this one only
	kids = button.parentNode.childNodes;
 
	for (var i = 0; i < kids.length; i++) {
		removeClassName(kids[i], "MenusItemHiligth");
	}
	button.className += " MenusItemHiligth";
	selectedItem = button;
}

function menuMousemove(event) {
	var y, evtY;
	
	window.clearTimeout(timeoutID);
//uncomment this code to get scrolling vertical behavior on mouse
//move
/*	event = event || window.Event || window.event;

	evtY = event.pageY || event.clientY;
	if (!isNaN(evtY)) {
		//if there has been vertical mouse movement
		//adjust the menu position
		if (oldy > evtY) {
			y = parseInt(selectedMenu.style.top.replace("px", "")) + 1;
			if (y > originalY) y=originalY;
			y += "px";
		} else if (oldy < evtY) {
			y = parseInt(selectedMenu.style.top.replace("px", "")) - 1;
			if (y > originalY) y=originalY;
			y += "px";
		} else {
			y = selectedMenu.style.top;
		}

		selectedMenu.style.top = y;
	
		oldy = evtY;
	}*/
	timeoutID = window.setTimeout('hideSelectedMenu()', 5000);
}

function hideSelectedMenu() {
	while (elem = menuList.pop()) {
		elem.style.display = "none";
		selectedMenu = null;
	}
	window.clearTimeout(timeoutID);
	indent = 0;
}

function windowMouseClick(event) {
	hideSelectedMenu();
}

function hideLastOpenedMenu() {
	elem = menuList.pop();
	if (elem) {
		elem.style.display = "none";
		indent--;
	}
}

function showTopLevelMenu(id, x, y) {
	hideSelectedMenu();
	indent=0;
	showMenu(id, x, y);
}

function showMenu(id, x, y) {
	var maxWidth = 0;

	originalY = y;
	menu = document.getElementById(id);
	//adds this menu the the list of opened menus
	menuList.push(menu);
	
	if (window.captureEvents) {
		window.captureEvents(Event.MOUSEMOVE | Event.MOUSECLICK);
		menu.onmousemove = menuMousemove;
		document.onclick = windowMouseClick;
	} else {
		menu.onmousemove = menuMousemove;
		document.onclick = windowMouseClick;
	}
	//show the menu
	menu.style.display = '';

	//get the width of the largest menuitem
	menuItems = menu.getElementsByTagName("SPAN");
	
	for (i = 0; i < menuItems.length; i++) {
		menuItems[i].style.cursor = 'pointer';
		removeClassName(menuItems[i].parentNode, "MenusItemHiligth");
		if (menuItems[i].offsetWidth > maxWidth)
			maxWidth = menuItems[i].offsetWidth;
	}

	//sanity check
//	if (maxWidth < 100) maxWidth = 150;

	//and apply-it to the menu
	menu.style.width = maxWidth + 5;

	menu.style.position = "absolute";
	menu.style.top = y + "px";
	menu.style.left = x + "px";
	if (selectedMenu == null) {
		timeoutID = window.setTimeout('hideSelectedMenu()', 3000);
	}
	selectedMenu = menu;
	//number of menu level's opened
	//is set back to zero when all menus are hidden
	indent++;
}

function getMenuWidth(id) {
	var maxWidth = 0;
	
	menu = document.getElementById(id);
	menu.style.display = '';
	
	//get the width of the largest menuitem
	menuItems = menu.getElementsByTagName("SPAN");

	
	for (i = 0; i < menuItems.length; i++) {
		if (menuItems[i].offsetWidth > maxWidth)
			maxWidth = menuItems[i].offsetWidth;
	}

	//sanity check
//	if (maxWidth < 100) maxWidth = 150;
	
	menu.style.display = 'none';
	
	return maxWidth;
}

function moveMenu(id, x, y) {
	menu = document.getElementById(id);

	menu.style.position = "absolute";
	menu.style.top = y + "px";
	menu.style.left = x + "px";
}
