function Menu(id, topNormalClass, topHoverClass, expZ, collZ, menuNormalClass, menuHoverClass, disabledClass){
//	this.childCounter = 0;
	this.id = id;
	this.children = new Array();
	this.childCounter = 0;
	this.menusAreExpanded = false;
	this.normalClass = topNormalClass;
	this.hoverClass = topHoverClass;
	this.childNormalClass = menuNormalClass;
	this.childHoverClass = menuHoverClass;
	this.disabledClass = disabledClass;
	this.expandedZ = expZ;
	this.collapsedZ = collZ;
	this.isTopLevel = true;
	this.XPos = getDOMX("this.id");
	this.YPos = getDOMY("this.id");
	this.icons = new Array();
	this.arrows = new Array();
	
	this.addChild = function(menuObj){
		deb.alert("top addChild (currentCounter = " + this.childCounter + ")");
		this.children = this.children.concat(menuObj);
//		this.children[this.childCounter] = menuObj;
//		this.childCounter++;
	}
	
	this.getArrow = function(status){
		return(this.arrows[status].src);
	}
	
	this.setArrows = function(normalUrl, hoverUrl){
		if((navigator.appName.indexOf("Internet Explorer") != -1) && (navigator.userAgent.indexOf("Opera") == -1) && (getAppVersion() < 7)){
			// Internet Exploder zuigt!!!
			normalUrl = normalUrl.replace(/png$/, "gif");
			hoverUrl = hoverUrl.replace(/png$/, "gif");
		}
		this.arrows["normal"] = new Image();
		this.arrows["normal"].src = normalUrl;
		this.arrows["hover"] = new Image();
		this.arrows["hover"].src = hoverUrl;
	}
	
	this.addIcon = function(name, normalURL, hoverURL){
		this.icons[name] = new menuIcon(name, normalURL, hoverURL);
	}
	
	this.getIconImage = function(name, status){
		if(this.icons[name]){
			return(this.icons[name].getImage(status));
		}
		return(null);
	}
	
	this.getTopClass = function(state){
		switch(state){
			case "hover" :
				return(this.hoverClass);
				break;
			default: 
				return(this.normalClass);
				break;
		}
	}

	this.getSubClass = function(state){
		switch(state){
			case "hover" :
				return(this.childHoverClass);
				break;
			case "disabled" :
				return(this.disabledClass);
				break;
			default: 
				return(this.childNormalClass);
				break;
		}
	}

	
	this.getZIndex = function(state){
		switch(state){
			case "expanded" : return(this.expandedZ);
			default : return(this.collapsedZ);
		}
	}
	
	this.collapseChildren = function(){
		deb.alert("top collapseChildren");
		for(var i in this.children){
			this.children[i].collapse();
		}
		this.menusAreExpanded = false;
	}
	
	this.collapseAll = function(){
		this.collapseChildren();
	}

	this.getMenusExpanded = function(){
		deb.alert("top getMenusExpanded");
		deb.alert("&nbsp;&nbsp;menusAreExpanded status = " + this.menusAreExpanded);
		return(this.menusAreExpanded);
	}
	
	this.setMenusExpanded = function(){
		deb.alert("top setMenusExpanded")
		this.menusAreExpanded = true;
		deb.alert("&nbsp;&nbsp;menusAreExpanded status = " + this.menusAreExpanded);
	}
	
	this.getItemById = function(id){
		var x;
		for(var i in this.children){
			x = this.children[i].getItemById(id);
			if(x != null){
				return(x);
			}
		}
		return(null);
	}
	
	this.getItemsByUrl = function(url){
		deb.alert("top getItemsByUrl: getting " + url);
		var items = new Array();
		for(var i in this.children){
			items = items.concat(this.children[i].getItemsByUrl(url));
		}
		return(items);
	}
	
	this.reposition = function(){
		deb.alert("top reposition");
		for(var i in this.children){
			this.children[i].reposition();
		}
	}
	
	this.resize = function(){
		if((getDOMX(this.id) != this.XPos) || (getDOMY(this.id) != this.YPos)){
			this.reposition();
		}
	}
	
	this.addToEventList = function(thisObject){
		var oldClickHandler = document.onclick;
		var oldBlurHandler = document.onblur;
		function newHandler(){
			thisObject.collapseAll();
		}
		if(oldClickHandler){
			document.onclick = function(){oldClickHandler(); newHandler();};
		}else{
			document.onclick = function(){newHandler();};
		}
		if(oldBlurHandler){
			document.onblur = function(){oldBlurHandler(); newHandler();};
		}else{
			document.onblur = function(){newHandler();};
		}
		var oldResizeHandler = window.onresize;
		function newResizeHandler(){
			thisObject.resize();
		}
		if(oldResizeHandler){
			window.onresize = function(){oldResizeHandler(); newResizeHandler();};
		}else{
			window.onresize = function(){newResizeHandler();};
		}
	}
	
	this.addToEventList(this);
}

function menuIcon(name, normalImage, hoverImage){
	this.name = name;
	this.images = new Array();
	
	this.getImage = function(status){
		if(this.images[status] != null){
			return(this.images[status].src);
		}
		return(null)
	}
	
	this.setImage = function(status, imageURL){
		if(this.images[status] == null){
			this.images[status] = new Image();
		}
		this.images[status].src = imageURL;
	}
	
	this.setImage("normal", normalImage);
	this.setImage("hover", hoverImage);
}

function SubMenu(parentMenu, baseId, disabled, id, icon){
	this.type = "subMenu";
	this.baseId = baseId;
	this.layerId = baseId + "Layer";
	this.buttonId = baseId + "Button";
	this.parentMenu = parentMenu;
	this.id = id;
	this.icon = icon;
	this.iconElement = null;
	this.disabled = (disabled == true);
	if(this.parentMenu.isTopLevel){
		this.positionerId = baseId + "Button";
	}else{
		this.positionerId = baseId + "Pos";
	}
	this.isExpanded = false;
	this.children = new Array();
	this.fadeId = 0;
	this.isTopLevel = false;
	this.parentMenu.addChild(this);
	getDOMObject(this.layerId).style.visibility = "hidden";
	this.additional = 0;
//	this.additional += ((navigator.appName.indexOf("Internet Explorer") != -1) && (navigator.userAgent.indexOf("Opera") == -1)) ? 2 : 0;
//	this.additional += ((navigator.appName.indexOf("Internet Explorer") != -1) && (navigator.userAgent.indexOf("Opera") != -1)) ? -1 : 0;
//	this.additional += ((!this.parentMenu.isTopLevel) && (navigator.appName.indexOf("Internet Explorer") != -1) && (navigator.userAgent.indexOf("Opera") == -1)) ? -3 : 0;
	this.addTop = this.additional;
	this.addLeft = this.additional;
//	this.addLeft += ((navigator.appName.indexOf("Internet Explorer") != -1) && (navigator.userAgent.indexOf("Opera") == -1)) ? -1 : 0;
	// move submenus left by 6px in Gecko browsers (Firefox)
	this.addLeft += ((!this.parentMenu.isTopLevel) && (navigator.userAgent.indexOf("Gecko") != -1) && (navigator.userAgent.indexOf("like Gecko") == -1)) ? -6 : 0;
//	addLeft += (navigator.userAgent.indexOf("Gecko") != -1 && this.parentMenu.isTopLevel) ? -1 : 0;
	// move submenus downwards by 2px in KHTML browsers(Safari)
	this.addTop += ((!this.parentMenu.isTopLevel) && (navigator.userAgent.indexOf("KHTML") != -1)) ? 2 : 0;
	// move submenus downwards by 8px in Gecko browsers (Firefox)
	this.addTop += ((!this.parentMenu.isTopLevel) && (navigator.userAgent.indexOf("Gecko") != -1) && (navigator.userAgent.indexOf("like Gecko") == -1)) ? 9 : 0;
//	alert(getStyle(this.layerId, "right"));
	this.baseTop = getDOMY(this.layerId) + this.addTop;
	this.baseLeft = getDOMX(this.layerId) + this.addLeft;

	this.position = function(){
		getDOMObject(this.layerId).style.left = (getDOMX(this.positionerId) + this.baseLeft) + "px";
		getDOMObject(this.layerId).style.top = (getDOMY(this.positionerId) + this.baseTop) + "px";
	}
	
	this.reposition = function(){
		deb.alert("sub " + this.id + " reposition");
		this.position();
		for(var i in this.children){
			this.children[i].reposition();
		}
	}
	
	this.click = function(e){
		if(!this.disabled){
			if(this.parentMenu.isTopLevel){
				if(this.isExpanded){
					this.parentMenu.collapseChildren();
				}else{
					this.expand();
				}
			}
		}
		if(!e){
			var e = window.event;
		}
		if(window.event){
			window.event.cancelBubble = true;
		}
		if(e.stopPropagation){
			e.stopPropagation();
		}
		return(false);
	}
	
	this.expand = function(){
		deb.alert("expand");
		cancelFade(this.fadeId);
		this.parentMenu.collapseChildren();
		getDOMObject(this.layerId).style.zIndex = this.getZIndex("expanded");
		setOpacity(this.layerId, 100);
		getDOMObject(this.layerId).style.visibility = "visible";
		this.isExpanded = true;
		this.setMenusExpanded();
	}
	
	this.collapseChildren = function(){
		for(var i in this.children){
			this.children[i].collapse();
		}
	}
	
	this.collapse = function(){
		deb.alert("sub collapse");
		this.collapseChildren();
		if(this.isExpanded){
			getDOMObject(this.layerId).style.zIndex = this.getZIndex("collapsed");
			this.cancelFade = false;
			this.isExpanded = false;
			getDOMObject(this.buttonId).className = this.getClass("normal");
			this.showIcon("normal");
			this.showArrow("normal");
			this.fadeId = getNewFadeId();
			fadeOut(this.layerId, 100, 10, this.fadeId);
		}
	}
	
	this.collapseAll = function(){
		this.parentMenu.collapseAll();
	}
	
	this.setMenusExpanded = function(){
		deb.alert("sub setMenusExpanded");
		this.parentMenu.setMenusExpanded();
	}
	
	this.getMenusExpanded = function(){
		return(this.parentMenu.getMenusExpanded());
	}
	
	this.addChild = function(menuObj){
		this.children = this.children.concat(menuObj);
	}
	
	this.getClass = function(state){
		if(this.parentMenu.isTopLevel){
			return(this.parentMenu.getTopClass(state));
		}
		return(this.parentMenu.getSubClass(state));
	}
	
	this.getSubClass = function(state){
		return(this.parentMenu.getSubClass(state));
	}
	
	this.getZIndex = function(state){
		return(this.parentMenu.getZIndex(state));
	}
	
	this.mouseOver = function(){
		if(!this.disabled && (this.parentMenu.isTopLevel || this.parentMenu.isExpanded)){
			try{
				getDOMObject(this.buttonId).className = this.getClass("hover");
				this.showIcon("hover");
				this.showArrow("hover");
				if(this.parentMenu.getMenusExpanded() && !this.isExpanded){
					this.expand();
				}
			}catch(e){
				deb.alert(e);
			}
		}
	}
	
	this.mouseOut = function(){
		if(!this.isExpanded && !this.disabled){
			getDOMObject(this.buttonId).className = this.getClass("normal");
			this.showIcon("normal");
			this.showArrow("normal");
		}
	}
	
	this.enable = function(){
		getDOMObject(this.buttonId).className = this.getClass("normal");
		this.disabled = false;
	}
	
	this.disable = function(){
		getDOMObject(this.buttonId).className = this.getClass("disabled");
		this.disabled = true;
	}
	
	this.getItemById = function(id){
		deb.alert("sub getItemById comparing: " + id + " to " + this.id);
		var x;
		if(id == this.id){
			return(this);
		}
		for(var i in this.children){
			x = this.children[i].getItemById(id);
			if(x != null){
				return(x);
			}
		}
		return(null);
	}
	
	this.getItemsByUrl = function(url){
		deb.alert("sub getItemsByUrl getting: " + url);
		var x;
		var items = new Array();
		for(var i in this.children){
			x = this.children[i].getItemsByUrl(url);
			if(x != null){
				items = items.concat(x);
			}
		
		}
		return(items);
	}

	this.getText = function(){
		return(getDOMObject(this.baseId + "Text").innerHTML);
	}
	
	this.setText = function(text){
		getDOMObject(this.baseId + "Text").innerHTML = text;
		this.parentMenu.reposition();
	}
	
	this.showIcon = function(status){
		if(this.icon != null){
			if(this.iconElement == null){
				this.iconElement = document.createElement("img");
				this.iconElement.setAttribute("class", "menuIcon");
				this.iconElement.setAttribute("id", this.baseId + "IconImage");
				getDOMObject(this.baseId + "Icon").appendChild(this.iconElement);
			}
			this.iconElement.src = this.getIconImage(this.icon, status);
			this.parentMenu.reposition();	// TODO kijken of dit wel efficient genoeg is of dat iconen gewoon altijd dezelfde grootte moeten hebben
		}
	}
	
	this.setIcon = function(name){
		this.icon = name;
		this.showIcon("normal");
	}
	
	this.removeIcon = function(){
		this.icon = null;
		if(this.iconElement != null){
			getDOMObject(this.baseId + "Icon").removeChild(this.iconElement);
		}
	}
	
	this.getIconImage = function(name, status){
		return(this.parentMenu.getIconImage(name, status));
	}
	
	this.showArrow = function(status){
		var arrow = this.getArrow(status);
		if((getDOMObject(this.positionerId).nodeName.toLowerCase() == "img") && (arrow != null)){
			getDOMObject(this.positionerId).src = arrow;
		}
	}
	
	this.getArrow = function(status){
		return(this.parentMenu.getArrow(status));
	}

	this.addEvents = function(x){
		eval("function " + this.buttonId + "OnHandler(){ x.mouseOver(); }");
		eval("getDOMObject(this.buttonId).onmouseover = function(){ " + this.buttonId + "OnHandler(); };");
		eval("function " + this.buttonId + "OffHandler(){ x.mouseOut(); }");
		eval("getDOMObject(this.buttonId).onmouseout = function(){ " + this.buttonId + "OffHandler(); };");
		eval("function " + this.buttonId + "ClickHandler(event){ x.click(event); }");
		eval("getDOMObject(this.buttonId).onclick = function(event){ " + this.buttonId + "ClickHandler(event); };");
	}

	this.position();
	if(this.disabled){
		getDOMObject(this.buttonId).className = this.getClass("disabled");
	}
	this.showIcon("normal");
	this.addEvents(this);
	this.showArrow("normal");
}

function MenuLink(parentMenu, baseId, URL, disabled, id, icon){
	this.type = "menuLink";
	this.baseId = baseId;
	this.buttonId = baseId + "Button";
	this.parentMenu = parentMenu;
	this.URL = URL;
	this.id = id;
	this.disabled = (disabled == true);
	this.parentMenu.addChild(this);
	this.icon = icon;
	this.iconElement = null;
	if(getDOMObject(this.baseId + "Href") && getDOMObject(this.baseId + "Href").hasAttribute && getDOMObject(this.baseId + "Href").hasAttribute("href")){
		getDOMObject(this.baseId + "Href").removeAttribute("href");
	}

	this.getClass = function(state){
		if(this.parentMenu.isTopLevel){
			return(this.parentMenu.getTopClass(state));
		}
		return(this.parentMenu.getSubClass(state));
	}
	
	this.getSubClass = function(state){
		return(this.parentMenu.getSubClass(state));
	}
	
	this.collapse = function(){
	}
	
	this.reposition = function(){
	}
	
	this.mouseOver = function(){
		var menusExpanded = this.parentMenu.getMenusExpanded();
		if(!this.disabled && (this.parentMenu.isTopLevel || this.parentMenu.isExpanded)){
			try{
				getDOMObject(this.buttonId).className = this.getClass("hover");
				this.showIcon("hover");
				window.status = this.URL;
				this.parentMenu.collapseChildren();
				if(this.parentMenu.isTopLevel && menusExpanded){
					this.parentMenu.setMenusExpanded();
				}
			}catch(e){}
		}
	}
	
	this.mouseOut = function(){
		if(!this.disabled){
			try{
				getDOMObject(this.buttonId).className = this.getClass("normal");
				this.showIcon("normal");
				window.status = "";
			}catch(e){}
		}
	}
	
	this.click = function(e){
		if(!this.disabled){
			try{
				if(this.URL.substr(0, 11) == "javascript:"){
					eval(this.URL.substr(11));
					getDOMObject(this.buttonId).className = this.getClass("normal");
					this.parentMenu.collapseAll();
				}else{
					document.location.href = this.URL;
				}
				this.showIcon("normal");
				window.status = "";
			}catch(e){}
		}
		if(!e){
			var e = window.event;
		}
		if(window.event){
			window.event.cancelBubble = true;
		}
		if(e.stopPropagation){
			e.stopPropagation();
		}
		return(false);
	}
	
	this.getItemById = function(id){
		deb.alert("link getItemById comparing: " + id + " to " + this.id);
		if(id == this.id){
			return(this);
		}else{
			return(null);
		}
	}
	
	this.getItemsByUrl = function(url){
		deb.alert("link getItemsByUrl comparing " + url + " to " + this.URL);
		if(url == this.URL){
			deb.alert("  returning object");
			return(this);
		}else{
			return(null);
		}
	}
	
	this.enable = function(){
		getDOMObject(this.buttonId).className = this.getClass("normal");
//		getDOMObject(this.baseId + "Href").setAttribute("href", this.URL);
		this.disabled = false;
	}
	
	this.disable = function(){
		getDOMObject(this.buttonId).className = this.getClass("disabled");
//		getDOMObject(this.baseId + "Href").removeAttribute("href");
		this.disabled = true;
	}
	
	this.getText = function(){
		return(getDOMObject(this.baseId + "Text").innerHTML);
	}
	
	this.setText = function(text){
		getDOMObject(this.baseId + "Text").innerHTML = text;
	}
	
	this.showIcon = function(status){
		if(this.icon != null){
			if(this.iconElement == null){
				this.iconElement = document.createElement("img");
				this.iconElement.setAttribute("class", "menuIcon");
				this.iconElement.setAttribute("id", this.baseId + "IconImage");
				getDOMObject(this.baseId + "Icon").appendChild(this.iconElement);
			}
			this.iconElement.src = this.getIconImage(this.icon, status);
		}
	}
	
		
	this.setIcon = function(name){
		this.icon = name;
		this.showIcon("normal");
	}
	
	this.setUrl = function(URL){
		this.URL = URL;
//		getDOMObject(this.baseId + "Href").setAttribute("href", URL);
	}
	
	this.removeIcon = function(){
		this.icon = null;
		if(this.iconElement != null){
			getDOMObject(this.baseId + "Icon").removeChild(this.iconElement);
		}
	}
	
	this.getIconImage = function(name, status){
		return(this.parentMenu.getIconImage(name, status));
	}
	
	this.addEvents = function(x){
		eval("function " + this.buttonId + "OnHandler(){ x.mouseOver(); }");
		eval("getDOMObject(this.buttonId).onmouseover = function(){ " + this.buttonId + "OnHandler(); };");
		eval("function " + this.buttonId + "OffHandler(){ x.mouseOut(); }");
		eval("getDOMObject(this.buttonId).onmouseout = function(){ " + this.buttonId + "OffHandler(); };");
		eval("function " + this.buttonId + "ClickHandler(event){ x.click(event); }");
		eval("getDOMObject(this.buttonId).onclick = function(event){ " + this.buttonId + "ClickHandler(event); };");
	}
	
	if(this.disabled){
		getDOMObject(this.buttonId).className = this.getClass("disabled");
		getDOMObject(this.baseId + "Href").removeAttribute("href");
	}
	this.showIcon("normal");
	this.addEvents(this);
}