/*
Modified by
Franco Ponticelli, 26 March 2005
ponticelli@litefarm.com
http://www.weblob.net/

Original by
http://www.kryogenix.org/code/browser/aqlists/
Stuart Langridge, November 2002
sil@kryogenix.org
Inspired by Aaron's labels.js (http://youngpup.net/demos/labels/) and Dave Lindquist's menuDropDown.js (http://www.gazingus.org/dhtml/?id=109)
*/

function processMenu(Menu, className){

    if(Menu.tagName != "UL") Menu = getFirstElementByTag(Menu, "UL");
    if(Menu == null) return;
	Menu.className = className;
	Menu.setOpen = function(){ this.opened = true; }
	Menu.setClose = function(){ this.opened = false; }
	Menu.isOpen = function(){ return this.opened; }
	processMenuUL(Menu, Menu, 0, className);
}

function processMenuUL(UL, MainUL, isSubMenu, className) {
    if (!UL.childNodes || UL.childNodes.length == 0) return;
    // Iterate through LIs, which are menu headers in the menu bar
	for(var i = 0; i < UL.childNodes.length; i++){
		var item = UL.childNodes[i];
        if (item.nodeName == "LI") {
            // Iterate things in this LI: should be an A and a UL
            var A = null;
            var SubUL = null;
            for (var j = 0; j < item.childNodes.length; j++) {
                var sitem = item.childNodes[j];
                switch (sitem.nodeName) {
                    case "A":
						A = sitem;
						break;
                    case "UL":
						SubUL = sitem;
						processMenuUL(SubUL, MainUL, 1);
						break;
                }
            }
			if(SubUL){
				bindActuatorList(A, MainUL, SubUL, className);
				if(!A.className || A.className == '') A.className = 'submenuheader';
                SubUL.style.display = "none";
			}
        }
    }
}

function bindActuatorList(A, MainUL, SubUL, className) {
	A.onclick = function (e) {
		if (!e) var e = window.event
		e.cancelBubble = true;
		if (e.stopPropagation) e.stopPropagation();
	
		var oldDocumentFunction = document.onclick;	
		// cerca tutti gli altri menù nel documento e nascondili
		document.onclick = function(){
			MainUL.setClose();
			hideChildren(MainUL, MainUL, className);
			document.onclick = oldDocumentFunction;
		}
		
		if (SubUL.style.display == "none") {
			this.displayParents();
			MainUL.setOpen();
		}
		return false;
	}

	A.onmouseover = function () {
        if(MainUL.isOpen() && SubUL.style.display == "none") {
			hideChildren(MainUL, MainUL, className);
			this.displayParents();
		}
    }
	
	A.getParents = function() {
			var parents = new Array();
			var parent = SubUL;
			do {
				if (parent.nodeName == "UL") {
					parents[parents.length] = parent;
				}
				parent = parent.parentNode;
			} while (parent.className != className);
			return parents;
	}
	
	A.displayParents = function(){
		var parents = this.getParents();
		for (var i = 0; i < parents.length; i++) {
			parents[i].style.display = "";
		}
	}	
}

function hideRecursive(menu, toplevel) {
	for (var i = 0; i < menu.childNodes.length; i++) {
		hideRecursive(menu.childNodes[i], toplevel);
	}
    if (menu.nodeName == "UL" && menu != toplevel){
        menu.style.display = "none";
	}
}

function hideChildren(menu, toplevel, className) {
    hideRecursive(menu, toplevel);
	var uls = document.getElementsByTagName('UL');
	var j=0;
	for(var i = 0; i < uls.length; i++){
		if(uls[i].className == className && uls[i] != toplevel){
			hideRecursive(uls[i], uls[i]);
		}
	}
}

function getElementLeft(eElement){
    var nLeftPos = eElement.offsetLeft;
    var eParElement = eElement.offsetParent;  
    while (eParElement != null){ 
        nLeftPos += eParElement.offsetLeft;     
        eParElement = eParElement.offsetParent; 
    }
    return nLeftPos;                         
}

function getElementTop(eElement){
    var nTopPos = eElement.offsetTop;    
    var eParElement = eElement.offsetParent;
    while (eParElement != null){                                    
        nTopPos += eParElement.offsetTop;      
        eParElement = eParElement.offsetParent; 
    }
    return nTopPos;                        
}

function getChildWidth(el){
	var width = el.offsetWidth;
	for (var i = 0; i < el.childNodes.length; i++) {
		if(el.childNodes[i].offsetWidth > width){
			width = el.childNodes[i].offsetWidth;
		}
	}
	return width;
}
