/*
 * menuDropdown.js - implements an dropdown menu based on a HTML list
 * Author: Dave Lindquist (http://www.gazingus.org)
 */

var currentMenu = null;
var currentSubMenu = null;

if (!document.getElementById)
    document.getElementById = function() { return null; }

function initializeMenu(menuId, actuatorId) {
    var menu = document.getElementById(menuId);
    var actuator = document.getElementById(actuatorId);

    if (menu == null || actuator == null) return;

    //if (window.opera) return; // I'm too tired

    if (actuator.className == 'sub-actuator') {
	    actuator.onmouseover = function() {
	        if (currentSubMenu == null) {
	            this.showMenu();
	        }
	        else {
	            currentSubMenu.style.visibility = "hidden";
	            if (currentSubMenu != menu) {
	            	this.showMenu();
	            }
				else {
	            	currentSubMenu = null;
				}
	        }
	        return false;
	    }
	    //Controllo se fra gli item ce ne se sono alcuni che non hanno dei sottomenu
	    var menuItems = actuator.parentNode.parentNode.childNodes;
	    for (var i = 0 ; i < menuItems.length ; i++) {
	    	if (menuItems[i].nodeName != 'LI') {
	    		continue;
	    	}
	    	var item = menuItems[i].getElementsByTagName('a')[0];
	    	if (item && item.className != 'sub-actuator') {
			    item.onmouseover = function() {
			        if (currentSubMenu != null) {
			            currentSubMenu.style.visibility = "hidden";
			            currentSubMenu = null;
			        }
			        return false;
			    }
	    	}
	    }

	    actuator.showMenu = function() {
	        menu.style.left = this.offsetLeft + this.offsetWidth + "px";
	        menu.style.top = this.offsetTop + "px";
	        menu.style.visibility = "visible";
	        currentSubMenu = menu;
	    }
    }
    else {

	    actuator.onmouseover = function() {
	        if (currentMenu) {
	            currentMenu.style.visibility = "hidden";
	            this.showMenu();
	        }
	        if (currentSubMenu) {
	            currentSubMenu.style.visibility = "hidden";
	            currentSubMenu = null;
	        }
	    }

	    actuator.onclick = function() {
	        if (currentMenu == null) {
	            this.showMenu();
	        }
	        else {
	            currentMenu.style.visibility = "hidden";
	            currentMenu = null;
	        }

	        return false;
	    }

	    actuator.showMenu = function() {
	        menu.style.left = this.offsetLeft + "px";
	        menu.style.top = this.offsetTop + this.offsetHeight + "px";
	        menu.style.visibility = "visible";
	        currentMenu = menu;
	    }
    }
}

