/*******************************************************************************
	mt_win_menu.js
	
	windows style menus

	v	:	1.6 LAS net
	d	:	02/03
	b	:	win IE 5+
	
*******************************************************************************
	notes:
		- 	modified for LAS net
		-	requires mt_win_menu.css
		-	popup menu with optional Alt+key keyboard combinations for accessibility
********************************************************************************/

var winMenu = {
	activeButton	:	null,
	nLastKeyCode	:	null,
	nColourIndex	:	1
}

function windowsMenu(menuID, parentID, parentIndex){
	var oMenu = document.createElement("div")
	oMenu.setAttribute("className", "menu")
	oMenu.setAttribute("class", "menu")
	oMenu.id = menuID
	oMenu.onmouseover = menuMouseover
	document.body.appendChild(oMenu)
	this.obj = document.getElementById(menuID)
	if(parentID){
		document.getElementById(parentID).childNodes[parentIndex-1].onmouseover = 
		document.getElementById(parentID).childNodes[parentIndex-1].onfocus = function(e){
			menuItemMouseover(e, menuID)
		}
		document.getElementById(parentID).childNodes[parentIndex-1].onclick = function(){
			return false
		}
		var oArrow = document.createElement("span")
		oArrow.setAttribute("className", "menuItemArrow")
		oArrow.setAttribute("class", "menuItemArrow")
		oArrow.appendChild(document.createTextNode("&#9654;")) 
		document.getElementById(parentID).childNodes[parentIndex-1].appendChild(oArrow)
		oArrow = null
	}
	oMenu = null
}

windowsMenu.prototype.addItem = function(sTxt, sKey, sLink){
	var oLink = document.createElement("a")
	if(!sLink)sLink = "#"
	oLink.setAttribute("className", "menuItem")// + winMenu.nColourIndex)			
	oLink.setAttribute("class", "menuItem")// + winMenu.nColourIndex)
	oLink.Text = sTxt
	sTxt = underlineLink(sTxt, sKey.toLowerCase())
	oLink.setAttribute("href", sLink)
	oLink.setAttribute("tabindex", "-1")
	oLink.setAttribute("accessKey", sKey)
	oSpan = document.createElement("span")
	oSpan.setAttribute("className", "menuItemText")
	oSpan.innerHTML += sTxt
	oLink.appendChild(oSpan)
	oSpan = null
	this.obj.appendChild(oLink)
	oLink.onfocus = function(){
		if(winMenu.nLastKeyCode != 9)
			self.location = this.href
	}
	oLink = null
}

windowsMenu.prototype.addSeparator = function(){
	var oSep = document.createElement("div")
	oSep.setAttribute("className", "menuItemSep")
	oSep.setAttribute("class", "menuItemSep")
	this.obj.appendChild(oSep)
	oSep = null
}

function windowsMenuBar(barID, sWidth, nItems){
	this.obj = document.getElementById(barID)
}

windowsMenuBar.prototype.addButton = function(sTxt, sMenuID, sKey, sTooltip, sHref){
	var oLink = document.createElement("a")
	oLink.setAttribute("className", "menuButton")
	oLink.setAttribute("class", "menuButton")
	if(sHref)
		oLink.setAttribute("href", sHref)
	else
		oLink.setAttribute("href", "#")
	oLink.setAttribute("id", "menuLink" + sTxt)
	oLink.setAttribute("accessKey", sKey)
	oLink.setAttribute("tabindex", "-1")
	if(sTooltip)
		oLink.setAttribute("title", sTooltip)
		
	oLink.onmouseover = function(e){
		buttonMouseover(e, sMenuID)
	}
	oLink.onfocus = function(e){
		
			
		return buttonClick(e,  sMenuID, this)
	}
	oLink.oncontextmenu = function(){
		return false
	}
	var oImage = document.createElement("IMG")
	var oTxt = document.createElement("DIV")
	sTxt = underlineLink(sTxt, sKey.toLowerCase())
	oTxt.setAttribute("style", "position:relative;float:left;display:inline;border:1px solid black")
	oTxt.innerHTML = sTxt
	oTxt.style.align = "left"
	oLink.innerHTML = sTxt
	this.obj.appendChild(oLink)
	oLink = null
	oTxt = null
	oImage = null
	winMenu.nColourIndex ++
}

function underlineLink(sTxt, sKey){
	for(var i=0; i<sTxt.length; i++){
		if(sTxt.toLowerCase().charAt(i) == sKey){
			return sTxt.substring(0, i) + "<u>" + sTxt.charAt(i) + "</u>" + sTxt.substring(i + 1)
		}
	}
	return sTxt
}

if(document.all){
	document.attachEvent("onmousedown", pageMousedown)
	document.attachEvent("onkeydown", pageKeyDown)
}else{
	document.addEventListener("mousedown", pageMousedown, 0)
	document.addEventListener("keydown", pageKeyDown, 0)
}

function pageMousedown(e){
	var el
	if(winMenu.activeButton == null)
		return
	el = (window.event) ? window.event.srcElement : e.target
	if(el == winMenu.activeButton)
		return
	if(getContainerWith(el, "DIV", "menu") == null){
		resetButton(winMenu.activeButton)
		winMenu.activeButton = null
		selects("visible")
	}
}


function pageKeyDown(e){
	var oSrc = (window.event) ? event.srcElement : e.target
	if(oSrc.tagName == "INPUT" || oSrc.tagName == "TEXTAREA")
		return true
	var nCode = (window.event) ? event.keyCode : e.keyCode
	winMenu.nLastKeyCode = nCode
	if(nCode == 27 && winMenu.activeButton){
		return killMenus()
	}
}

function killMenus(){
	resetButton(winMenu.activeButton)
	selects("visible")
	return winMenu.activeButton = null	
}

function buttonClick(e, menuId, obj){
	
	selects("hidden")
	var button
	button = (obj) ? obj : (window.event) ? window.event.srcElement : e.target
	if(button.tagName == "U")
		button = button.parentElement
//	button.blur()

	if(button.menu == null){
		button.menu = document.getElementById(menuId)
	if(button.menu.initialized == null)
		menuInit(button.menu)
	}
	if(winMenu.activeButton != null){
			resetButton(winMenu.activeButton)
	}
	if(button != winMenu.activeButton){
		depressButton(button)
		winMenu.activeButton = button
	}
	else
		winMenu.activeButton = null
	if(winMenu.activeButton == null)
			selects("visible")

			
	return false;
}

function buttonMouseover(e, menuId){
	var button
	button = (window.event) ? window.event.srcElement : e.target
	while(button.tagName != "A")
		button = button.parentNode
	if(winMenu.activeButton != null && winMenu.activeButton != button)
		buttonClick(e, menuId)
}

function depressButton(button){
	var x, y
	button.className = button.className.replace("Button", "ButtonActive")
	x = getPageOffsetLeft(button)// + button.offsetParent.clientLeft
	y = getPageOffsetTop(button) + button.offsetHeight// + button.offsetParent.clientTop
	if(x + button.menu.offsetWidth > document.body.clientWidth)
		x = document.body.clientWidth - button.menu.offsetWidth
	button.menu.style.left = (x) + "px"
	button.menu.style.top  = (y) + "px"
	button.menu.style.visibility = "visible"
}

function resetButton(button){
	button.className = button.className.replace("ButtonActive", "Button")
	if(button.menu != null){
		closeSubMenu(button.menu)
		button.menu.style.visibility = "hidden"
	}
}

function menuMouseover(e){
	var menu
	menu = getContainerWith((window.event) ? window.event.srcElement : e.target, "DIV", "menu")
		closeSubMenu(menu)
}

function menuItemMouseover(e, menuId)
	{
	var item, menu, x, y
	item = getContainerWith((window.event) ? window.event.srcElement : e.target, "A", "menuItem")
	menu = getContainerWith(item, "DIV", "menu")
	if (menu.activeItem != null)
		{closeSubMenu(menu);}
	menu.activeItem = item
	item.className = (item.className.indexOf("Image") != -1) ? "menuItemImageHighLight" : "menuItemHighLight"
	if (item.subMenu == null)
		{item.subMenu = document.getElementById(menuId);}
	if (item.subMenu.initialized == null)
		{menuInit(item.subMenu);}
	x = getPageOffsetLeft(item) + item.offsetWidth
	y = getPageOffsetTop(item)

	var maxX, maxY
	maxX = document.body.scrollLeft + document.body.clientWidth
	maxY = document.body.scrollTop  + document.body.clientHeight
	maxX -= item.subMenu.offsetWidth
	maxY -= item.subMenu.offsetHeight
	if (x > maxX)
		{x = Math.max(0, x - item.offsetWidth - item.subMenu.offsetWidth + (menu.offsetWidth - item.offsetWidth));}
	y = Math.max(0, Math.min(y, maxY));
	if (isNaN(y))
		{
		item.subMenu = getObject(item.subMenu);
		y = getPageOffsetTop((window.event) ? window.event.srcElement : e.target);
		}
	item.subMenu.style.left = (x-8) + "px"	
	item.subMenu.style.top  = (y+8) + "px"
	item.subMenu.style.visibility = "visible"
	if (window.event)
		{window.event.cancelBubble = true;}
	else
		{e.stopPropagation();}
	}

function closeSubMenu(menu){
	if(menu == null || menu.activeItem == null)
	return
	if(menu.activeItem.subMenu != null){
		closeSubMenu(menu.activeItem.subMenu);
		menu.activeItem.subMenu.style.visibility = "hidden"
		menu.activeItem.subMenu = null
	}
	menu.activeItem.className = menu.activeItem.className.replace("ItemHighLight", "Item")
	menu.activeItem = null
}

function menuInit(menu){
	var itemList, spanList
	var textEl, arrowEl;
	var itemWidth;
	var w, dw;
	var i, j;
	menu.style.lineHeight = "2.5ex"
	spanList = menu.getElementsByTagName("SPAN")
	for(i = 0; i < spanList.length; i++)
		if(hasClassName(spanList[i], "menuItemArrow")){
		  spanList[i].style.fontFamily = "Webdings"
		  spanList[i].firstChild.nodeValue = "4"
		}
	itemList = menu.getElementsByTagName("A")
	if(itemList.length > 0)
		itemWidth = itemList[0].offsetWidth
	else
		return
	//alert(itemList[0].innerHTML)
	for(i = 0; i < itemList.length; i++){
		spanList = itemList[i].getElementsByTagName("SPAN")
		textEl  = null
		arrowEl = null;
		for(j = 0; j < spanList.length; j++){
			if(spanList[j].className.indexOf("menuItemArrow") != -1)
			  arrowEl = spanList[j]
			else if(spanList[j].className.indexOf("menuItem") != -1)
			  textEl = spanList[j]
		}
		if(textEl != null && arrowEl != null)
			textEl.style.paddingRight = (itemWidth - (textEl.offsetWidth + arrowEl.offsetWidth)) + "px"
	}
	w = itemList[0].offsetWidth
	itemList[0].style.width = w + "px"
	dw = itemList[0].offsetWidth - w
	w -= dw
	itemList[0].style.width = w + "px"
	menu.initialized = true
}

function getContainerWith(node, tagName, className){
	while (node != null){
		if(node.tagName != null && node.tagName == tagName && node.className.indexOf(className) != -1){
			return node
		}
		node = node.parentNode
	}
	return node
}

function hasClassName(el, name){
	var i, list
	list = el.className.split(" ")
	for(i = 0; i < list.length; i++)
		if(list[i] == name)
			return true
	return false
}

function removeClassName(el, name){
	var i, curList, newList
	if(el.className == null)
		return
	newList = new Array()
	curList = el.className.split(" ")
	for(i = 0; i < curList.length; i++)
		if(curList[i] != name)
			newList.push(curList[i])
	el.className = newList.join(" ")
}
var ieBox = document.all && (document.compatMode == null || document.compatMode != "CSS1Compat");

function getInnerLeft(el) {
	if (el == null) return 0;
	if (ieBox && el == document.body || !ieBox && el == document.documentElement) return 0;
	return getPageOffsetLeft(el) + getBorderLeft(el);
}

function getPageOffsetLeft(el) {
	if (el == null) return 0;
	return el.offsetLeft + getInnerLeft(el.offsetParent);
}

function getInnerTop(el) {
	if (el == null) return 0;
	if (ieBox && el == document.body || !ieBox && el == document.documentElement) return 0;
	return getPageOffsetTop(el) + getBorderTop(el);
}

function getPageOffsetTop(el) {
	if (el == null) return 0;
	return el.offsetTop + getInnerTop(el.offsetParent);
}

function getBorderLeft(el) {
	return brow.ie ?
		el.clientLeft :
		parseInt(window.getComputedStyle(el, null).getPropertyValue("border-left-width"));
}

function getBorderTop(el) {
	return brow.ie ?
		el.clientTop :
		parseInt(window.getComputedStyle(el, null).getPropertyValue("border-top-width"));
}
function selects(vis){
	var aS = document.getElementsByTagName("SELECT")
	for(var i=0; i< aS.length; i++)
		if(aS[i].id != "subwebs")
			aS[i].style.visibility = vis
}