var actuatorActive = false, currentSubMenu = null, currentMenuOption = null, currentMenu = null, menuActive = false, currentActuator = null, dd_z = 0

if (!document.getElementById)
    document.getElementById = function() { 
		return null 
	}

function initialiseMenu(menuId) {
    var menu = document.getElementById(menuId)
	if (menu == null) return
		menu.onmouseover = function() {
			menuActive = true
		}
		menu.onmouseout = function() {
			menuActive = false
		}
		menu.menuOptions = menu.childNodes
	var menuOption
		for (var i = 0; i < menu.menuOptions.length; i++) {
			if (menu.menuOptions[i].firstChild) {
				menuOption = menu.menuOptions[i].firstChild
				menuOption.i = i + 1
				menuOption.menu = menu
				menuOption.parent = 
					(menuOption.className.indexOf("parent") != -1) ? 1 : 0
				menuOption.dead = 
					(menuOption.className.indexOf("dead") != -1) ? 1 : 0
					menuOption.id = menuId + "Option" + i
				if (!menuOption.dead && !menuOption.parent) {
					menuOption.onclick = function(e) {
						if (bw.ie) event.cancelBubble = true
						else e.stopPropagation()
						menuActive = false
						currentMenu.style.visibility = "hidden"
						currentActuator.className = "actuator"
						currentMenu = null
						tidySubMenu(this)
					}
				}
				if (menuOption.parent && !menuOption.dead) {
				//	menuOption.ref = "ref" + menuOption.id + "Object"
				//	eval(menuOption.ref + "=menuOption")
					menuOption.subMenu = new initialiseSubMenu(menuOption)
					menu.menuOptions[i].onmouseover = function() {
						if (this.firstChild.active && !currentSubMenu) {
							currentMenuOption.showSubMenu()
							currentMenuOption.className = "parentOptionHover"
						}
					}
					menuOption.showSubMenu = function() {
						currentSubMenu = this.subMenu.menu
						this.subMenu.menu.style.top = this.subMenu.menu.menuOption.offsetTop - this.subMenu.menu.margin + "px"
						//set dead/live sub options
						for (var j = 0; j < currentSubMenu.menuOptions.length; j += 2) {
							if (eval("resData[" + currentActuator.resIndex + "].option" + Math.ceil(this.i/2) + "SubOption" + (Math.ceil(j/2) + 1))) {
								currentSubMenu.menuOptions[j].firstChild.style.display = "block"
								currentSubMenu.menuOptions[j+1].firstChild.style.display = "none"
							} else {
								currentSubMenu.menuOptions[j+1].firstChild.style.display = "block"
								currentSubMenu.menuOptions[j].firstChild.style.display = "none"
							}
						}
						if ((bw.ie6 || bw.ie55) && bw.win) {
							this.subMenu.menu.filters[0].apply()
							this.subMenu.menu.filters[0].play()
						}
						this.subMenu.menu.style.visibility = "visible"
					}
					hideSubMenu(menuOption)
				} else {
					menuOption.onmouseout = function() {
						if (!this.dead) this.className = "option"
					}
				}
				menuOption.onmouseover = function() {
					if (currentMenuOption && currentMenuOption.parent && (this.id != currentMenuOption.id)) {
						hideSubMenu(currentMenuOption)
						currentMenuOption.className = "parentOption"
						currentMenuOption.active = false
					}
					if (!this.parent && !this.dead) this.className = "optionHover"
					this.active = true
					currentMenuOption = this	
				}
			}
		}
	return menu
}
function tidySubMenu(menuOption) {
	if (currentSubMenu) {
		menuOption.className = "parentOption"
		menuOption.active = false
		hideSubMenu(menuOption)
		currentMenuOption = null
	}
}
function hideSubMenu(menuOption) {
	currentSubMenu = null
	menuOption.subMenu.menu.style.visibility = "hidden"
}
function initialiseMenuActuator(menu,actuatorId,i) {
    var actuator = document.getElementById(actuatorId)
		if (actuator == null) return
	var parentContainerStr = "actuator.parentNode.parentNode.parentNode.parentNode.parentNode"
	var offsetParentContainerStr = "actuator.offsetParent.offsetParent"
		for (var j = 0; j < resData[i].aggregationLevel; j++) {
			parentContainerStr += ".parentNode.parentNode.parentNode.parentNode"
			offsetParentContainerStr += ".offsetParent.offsetParent"
		}
		actuator.parentContainer = eval(parentContainerStr)
		actuator.offsetParentContainer = eval(offsetParentContainerStr)
		actuator.resIndex = i
		actuator.onmouseover = function() {
			resIndex = this.resIndex
			if (currentMenuOption) tidySubMenu(currentMenuOption)
			if (currentMenu && (this.id != currentActuator.id)) {
				currentMenu.style.visibility = "hidden"
				currentActuator.className = "actuator"
				actuator.className = "actuatorDown"
				this.showMenu()
			} else if (!currentMenu) actuator.className = "actuatorOver"
			actuatorActive = true
		}
		actuator.onmouseout = function() {
			if (!currentMenu) actuator.className = "actuator"
			actuatorActive = false
		}
		actuator.onblur = function() {
			if (currentMenu && !menuActive && !actuatorActive) {
				currentMenu.style.visibility = "hidden"
				currentActuator.className = "actuator"
				currentMenu = null
				currentActuator = null
			}
		}
		actuator.onclick = function(e) {
			if (currentMenuOption) tidySubMenu(currentMenuOption)
			if (currentMenu == null) {
				this.showMenu()
				this.className = "actuatorDown"
				if (bw.ie) this.focus()
			} else {
				menuActive = false
				currentMenu.style.visibility = "hidden"
				currentActuator.className = "actuator"
				currentMenu = null
				currentActuator = null
				this.className = "actuatorOver"
			}
			if (bw.ie) event.cancelBubble = true
			else e.stopPropagation()
			return false
		}
		actuator.showMenu = function() {
			for (var i = 0; i < menu.menuOptions.length; i += 2) {
				if (eval("resData[" + this.resIndex + "].option" + (Math.ceil(i/2) + 1))) {
					menu.menuOptions[i].firstChild.style.display = "block"
					menu.menuOptions[i + 1].firstChild.style.display = "none"
				} else {
					menu.menuOptions[i + 1].firstChild.style.display = "block"
					menu.menuOptions[i].firstChild.style.display = "none"
				}
			}
			// cope with nesting in results table
			var aggregateHeightAdjustorStr = "this"
			var aggregateHeightAdjustor = (bw.ie) ? 0 : -8
			for (var j = 0; j < resData[this.resIndex].aggregationLevel; j++) {
				aggregateHeightAdjustorStr += ".offsetParent.offsetParent"
				aggregateHeightAdjustor += eval(aggregateHeightAdjustorStr + ".offsetParent.offsetTop") + j
			}
			menu.style.left = this.parentContainer.offsetLeft + this.offsetWidth + this.offsetLeft + this.offsetParent.offsetLeft + (this.offsetParent.offsetLeft * resData[this.resIndex].aggregationLevel) + px
			menu.style.top = this.parentContainer.offsetTop + this.offsetParentContainer.offsetTop + this.offsetParent.offsetTop + this.offsetTop + aggregateHeightAdjustor + px
			dd_z++
			menu.style.zIndex = dd_z
			if ((bw.ie6 || bw.ie55) && bw.win) {
				menu.filters[0].apply()
				menu.filters[0].play()
			}
			menu.style.visibility = "visible"
			currentMenu = menu
			currentActuator = this
			currentMenu.onclick = function() {
				currentActuator.focus()
			}
		}
	return actuator
}
function initialiseSubMenu(menuOption) {
	this.menu = menuOption.nextSibling
	this.menu.menuOption = menuOption
		// ie not detecting first-child selector in style sheet
		if (bw.ie) this.menu.margin = parseInt(this.menu.menuOption.currentStyle.margin) 
		else this.menu.margin = 0
	this.menu.menuOptions = this.menu.childNodes
	var menuOption
		for (var i = 0; i < this.menu.menuOptions.length; i++) {
			if (this.menu.menuOptions[i].firstChild) {
				menuOption = this.menu.menuOptions[i].firstChild
				// menuOption.id = this.menu.menuOption.id + "subMenuOption" + i
				menuOption.dead = 
					(menuOption.className.indexOf("dead") != -1) ? 1 : 0
				if (!this.dead) { 
					menuOption.onmouseover = function(e) {
						this.className = "optionHover"
						if (bw.ie) event.cancelBubble = true
						else e.stopPropagation()
						menuActive = true
					}
					menuOption.onmouseout = function(e) {
						this.className = "option"
						if (bw.ie) event.cancelBubble = true
						else e.stopPropagation()
						menuActive = false
					}
					menuOption.onclick = function() {
						// collapse all menus
						if (currentMenuOption) tidySubMenu(currentMenuOption)
						menuActive = false
						currentMenu.style.visibility = "hidden"
					}
				}
			}
		}	
	return this
}
