// ------ Setup -----
// --- frames
/* 
NOTE: 
The crossframe functionality is not complete in this version and will not work
*/
//Menu is used in a frameset [true/false]
var crossFrame = false;
//The name of the frame where the menu triggers					
var triggerFrame = 'topframe';
//The name of the frame where the menu opens					
var dropFrame = 'contentframe';
// --- /frames
//Drop menu offset left (IE)
if (document.all) {
	var dropMenuOffsetLeft = 10;
	/* 
	NOTE:
	For Internet Explorer frames drop menu containers may need to be positioned to prevent a 
	positioning bug for page images when menu triggers are clicked.
	Set div runtime style onload like:
	document.getElementById('dropMenuContainers').runtimeStyle.cssText = "position: absolute; top: 0; left: 0; z-index: 3;"
	*/
}
//Drop menu offset left (not IE)
if (!document.all) {
	var dropMenuOffsetLeft = -1;
}
// ------ /Setup -----
// ------ Variables -----
var dropId = null;
var openId = null;
/*
NOTE: 
For frames openId must also be declared in each page 
except in the menu trigger page. Declare like:
var openId = null;
*/			
// ------ /Variables -----
// ------ Close any open menu on document keyup (ESC) -----
//NN, MOZ; handle document onkeydown
if (!document.all) {
	document.onkeydown = function (evt) {
		fDocEscClose(evt)
	}
}
//IE; handle document onkeydown
else {
	document.onkeydown = fDocEscClose;
}
//Close menu on ESC key up
function fDocEscClose(e) {
	//for IE (e.keyCode or window.event.keyCode can be used)
	if (document.all) {
		var key = window.event.keyCode; 
	}
	//netscape, mozilla
	if (!document.all) {
		var key = e.which; 
	}
	//if ESC key (27) has been pressed close any open menu and focus the trigger for that menu
	if (key == 27) {
		if (openId != null) {
			var sTrigId = 'dT-' + openId.split('-')[1];
			if (crossFrame) {
				parent.frames[triggerFrame].document.getElementById(sTrigId).focus();
			}
			else {
				document.getElementById(sTrigId).focus();
			}
			fCloseMenu(openId);
			return false;
		}
	}
}
// ------ Close any open menu on document keydown (ESC) -----
// ------ Close any open menu on document click -----
//NN, MOZ; handle document onclick 
if (!document.all) {
	document.onclick = function (evt) {
		fDocClickClose(evt)
	}
}
//IE; handle document onclick
else {
	document.onclick = fDocClickClose;
}
//Close menu onclick
function fDocClickClose(e) {
	//----- NN,MOZ
	if (!document.all) {
		if (e.target.className.indexOf('dropTrigger') != '-1' || e.target.className.indexOf('dropItem') != '-1') {
			//click drop menu item link when clicking menu item div
			/*if (e.target.className == 'dropItem') {
				e.target.childNodes[0].focus();
			}*/
			return true;
		}
		else if (e.target.parentNode.className.indexOf('dropTrigger') != '-1' || e.target.parentNode.className.indexOf('dropItem') != '-1') {
			return true;
		}
		else {
			if (openId != null) {
				//close all sub menus
				fCloseSubMenusAll();
				//close menu
				fCloseMenu(openId);
			}
		}
	}
	//----- /NN,MOZ
	//----- IE
	else {
		if (event.srcElement.className.indexOf('dropTrigger') != '-1' || event.srcElement.className.indexOf('dropItem') != '-1') {
			//click drop menu item link when clicking menu item div
			/*if (event.srcElement.className == 'dropItem') {
				event.srcElement.childNodes[0].click();
			}*/
			return true;
		}
		else if (event.srcElement.parentElement && event.srcElement.parentElement.className.indexOf('dropTrigger') != '-1' || event.srcElement.parentElement && event.srcElement.parentElement.className.indexOf('dropItem') != '-1') {
			return true;
		}
		else {
			if (openId != null) {
				//close all sub menus
				fCloseSubMenusAll();
				//close menu
				fCloseMenu(openId);
			}
		}
	}
	//----- /IE
}
// ------ /Close any open menu on document click -----
// ------ Close menu -----
function fCloseMenu(sOpenId) {
	if (crossFrame) {
		parent.frames[dropFrame].document.getElementById(sOpenId).style.visibility = 'hidden';
		parent.frames[dropFrame].openId = null;
		parent.frames[triggerFrame].openId = null;
	}
	else {
		//close all sub menus
		fCloseSubMenusAll();
		//close first level
		document.getElementById(sOpenId).style.display = 'none';
	}
	openId = null;
}
// ------ /Close menu -----
// ------ Handle dropmenu events -----
//variables
var arrOpenSubItems = new Array();
var iLevel = 0;
var oElm;
var oElmParent;
var oElmGrandParent;
var oActiveElm = null;
//drop menu functions
function fDropMenu(e,sId) {
	//set event source object
	//----- NN,MOZ
	if (!document.all) {
		oElm = e.target;
	}
	//----- /NN,MOZ
	//----- IE
	else {
		oElm = event.srcElement;
	}
	//----- /IE
	oElmParent = oElm.parentNode;
	oElmGrandParent = oElm.parentNode.parentNode;
	//set drop id
	dropId = 'dC-' + sId;
	//create focus highlights for NN; MOZ
	//----- NN,MOZ
	if (!document.all) {
		//clear old active element
		if (oActiveElm != null) {
			oActiveElm.style.backgroundColor = '';
			oActiveElm.style.color = '';
		}
		//set new active element
		if (oElmParent.className.indexOf('dropTrigger') == '-1') {
			oActiveElm = oElmParent;
			oActiveElm.style.backgroundColor = '#454F7B;';
			oActiveElm.style.color = '#CAD6E8';
		}
	}
	//----- /NN,MOZ
	//----- FOCUS
	if (e.type == 'focus') {
		//for trigger level
		if (oElmParent.className.indexOf('dropTrigger') != '-1') {
			if (openId != null && dropId != openId) {
				fCloseMenu(openId);
			}
		}
		//for menu levels
		//if (oElmParent.className.indexOf('subChildOn') != '-1' || oElmParent.className.indexOf('subChildOff') != '-1') {
		else {
			//get menu item level
			fGetMenuItemLevel();
			//first level
			if (oElmGrandParent.className == 'dropContainer') {
				//items with children
				if (oElmParent.className.indexOf('subChildOn') != '-1') {
					//close any open menu and open this menu
					if (arrOpenSubItems.length > 0) {
						if (oElmParent.childNodes[2] != arrOpenSubItems[0]) {
							if (iLevel == 0) {
								fCloseSubMenusAll();
								fOpenMenuSubLevels();
							}
						}
					}
					//open menu
					else {
						fOpenMenuSubLevels();
					}
				}
				//items without children
				if (oElmParent.className.indexOf('subChildOff') != '-1') {
					if (arrOpenSubItems.length > 0) {
						if (iLevel == 0) {
							fCloseSubMenusAll();
						}
					}
				}	
			}
			//other levels
			else {
				if (arrOpenSubItems.length >= iLevel) {
					//items with children
					if (oElmParent.className.indexOf('subChildOn') != '-1') {
						if (oElmParent.childNodes[2] != arrOpenSubItems[iLevel+1]) {
							fCloseSubMenusBelow();
							fOpenMenuSubLevels();
						}
						else {
							fOpenMenuSubLevels();
						}
					}
					//items without children
					if (oElmParent.className.indexOf('subChildOff') != '-1') {
						fCloseSubMenusBelow();
					}
				}
			}
		}
	}
	//----- /FOCUS
	//----- KEYDOWN
	if (e.type == 'keydown') {
		//----- NN,MOZ
		if (!document.all) {
			var key = e.which;
		}
		//----- /NN,MOZ
		//----- IE
		//for IE (e.keyCode or window.event.keyCode can be used)
		else {
			var key = e.keyCode; 
			 
		}
		//----- /IE
		//TABS for frames
		//--- frames
		if (crossFrame) {
			//TAB + SHIFT
			if (e.shiftKey) {
				if (key == 9) {
					//close drop menu on TAB back on first item
					if (!document.all) {
						if (e.target.className == 'firstItem') {
							fCloseMenu(openId);
							//ERROR; no focus on frame
							parent.frames[triggerFrame].document.getElementById('dT-' + sId).focus();
							return false;
						}
					}
					else {
						if (event.srcElement.className == 'firstItem') {
							parent.frames[triggerFrame].document.getElementById('dT-' + sId).focus();
							fCloseMenu(openId);
						}
					}
				}
			}
			//TAB
			else {
				if (key == 9) {
					//move to first link in open drop menu when pressing TAB forward while trigger has focus
					if (!document.all) {
						if (openId && e.target.parentNode.className.indexOf('dropTrigger') != '-1') {
							//ERROR; no focus on frame
							parent.frames[dropFrame].document.getElementById(dropId).getElementsByTagName('A')[0].focus();
						}
					}
					else {
						if (openId && event.srcElement.parentElement.className.indexOf('dropTrigger') != '-1') {
							parent.frames[dropFrame].document.getElementById(dropId).focus();
						}
					}
					//close drop menu on TAB forward on last item
					if (!document.all) {
						if (e.target.className == 'lastItem') {
							fCloseMenu(openId);
							//ERROR; no focus on frame
							parent.frames[triggerFrame].document.getElementById('dT-' + sId).focus();
							return false;
						}
					}
					else {
						if (event.srcElement.className == 'lastItem') {
							parent.frames[triggerFrame].document.getElementById('dT-' + sId).focus();
							fCloseMenu(openId);
						}
					}
				}
			}
		}
		//--- /frames
		//--- noframes
		//TABS for single pages
		else {
			if (key == 9) {
				//tab from menu trigger closes menu
				if (oElmParent.className.indexOf('dropTrigger') != '-1') {
					if (openId != null) {
						fCloseMenu(openId);
					}
				}
				//block tab from inside a drop menu, move focus to menu trigger
				if (oElmParent.className.indexOf('subChildOn') != '-1' || oElmParent.className.indexOf('subChildOff') != '-1') {
					fCloseMenu(openId);
					//use a timeout when moving focus to the drop trigger 
					setTimeout("fFocusMenuTrigger('" + 'dT-' + sId + "')",0);
				}
			}
		}
		//--- /noframes
		//if ENTER key (13) has been pressed open menu and menu levels
		if (key == 13) {
			//--- frames
			if (crossFrame) {
				//position
				if (!document.all) {
					iPosLeft = e.target.parentNode.parentNode.offsetLeft;
				}
				else {
					iPosLeft = event.srcElement.parentElement.parentElement.offsetLeft;
				}
				parent.frames[dropFrame].document.getElementById(dropId).style.left = iPosLeft + dropMenuOffsetLeft + 'px';
				//show
				parent.frames[dropFrame].document.getElementById(dropId).style.visibility = 'visible';
				parent.frames[dropFrame].openId = dropId;
			}
			//--- /frames
			//--- noframes
			else {
				//first level; open top level
				if (oElmParent.className.indexOf('dropTrigger') != '-1') {
					//close any open menu
					if (openId != null && dropId != openId) {
						fCloseMenu(openId);
					}
					if (document.getElementById(dropId)) {
						fOpenFirstMenuLevel(0);
						return false;
					}
					else {
						return false;
					}
				}
				//other levels; block enter for sub level trigger
				if (oElmParent.className.indexOf('subChildOn') != '-1') {
					return false;
				}
			}
			//--- /noframes
		}
		//if ARROW LEFT (37) has been pressed
		if (key == 37) {
			//move within triggers
			if (oElmParent.className.indexOf('dropTrigger') != '-1') {
				var goTrigger = 'dT-' + (parseInt(sId) - 1);
				if (document.getElementById(goTrigger)) {
					document.getElementById(goTrigger).focus();
				}
				else {
					//----- NN, MOZ
					if (!document.all) {
						//alert(oElmGrandParent.parentNode.childNodes[oElmGrandParent.parentNode.childNodes.length-4].childNodes[1].innerHTML);
						oElmGrandParent.parentNode.childNodes[oElmGrandParent.parentNode.childNodes.length-4].childNodes[1].childNodes[0].focus();
					}
					//----- /NN, MOZ
					//----- IE
					else {
						oElmGrandParent.parentNode.childNodes[oElmGrandParent.parentNode.childNodes.length-2].childNodes[0].childNodes[0].focus();
					}
					//----- /IE
				}
			}
			//close first level, focus drop trigger
			if (oElmGrandParent.className == 'dropContainer') {
				fCloseMenu(openId);
				document.getElementById('dT-' + sId).focus();
			}
			//move back one level
			if (oElmParent.className.indexOf('subChildOn') != '-1' || oElmParent.className.indexOf('subChildOff') != '-1') {
				//focus parent
				//----- NN, MOZ
				if (!document.all) {
					if (oElm == oElmGrandParent.childNodes[1].childNodes[0]) {
						oElmGrandParent.parentNode.childNodes[0].focus();
					}
				}
				//----- /NN, MOZ
				//----- IE
				else {
					if (oElm == oElmGrandParent.childNodes[0].childNodes[0]) {
						oElmGrandParent.parentNode.childNodes[0].focus();
					}
				}
				//----- /IE
			}
		}
		//if ARROW UP (38) has been pressed
		if (key == 38) {
			//move upwards inside menus
			if (oElmGrandParent.className == 'dropContainer' || oElmParent.className.indexOf('subChildOn') != '-1' || oElmParent.className.indexOf('subChildOff') != '-1') {
				//----- NN, MOZ
				if (!document.all) {
					if (oElmParent == oElmGrandParent.firstChild.nextSibling) {
						//rotate, skip to first position
						//oElmGrandParent.lastChild.previousSibling.childNodes[0].focus();
						//first level; close menu and move focus to menu trigger
						//sub level; move focus to sub menu opener
						if (oElmGrandParent.className == 'dropContainer') {
							fCloseMenu(openId);
							//use a timeout when moving focus to the drop trigger 
							setTimeout("fFocusMenuTrigger('" + 'dT-' + sId + "')",0);
						}
						else {
							if (oElm == oElmGrandParent.childNodes[1].childNodes[0]) {
								oElmGrandParent.parentNode.childNodes[0].focus();
							}
						}
					}
					else {
						if (oElmParent.previousSibling.previousSibling && oElmParent.previousSibling.previousSibling.childNodes[0].tagName == 'A') {
							oElmParent.previousSibling.previousSibling.childNodes[0].focus();
						}
					}
				}
				//----- /NN, MOZ
				//----- IE
				else {
					if (oElmParent.previousSibling && oElmParent.previousSibling.childNodes[0].tagName == 'A') {
						oElmParent.previousSibling.childNodes[0].focus();
					}
					else {
						//rotate, skip to first position
						//oElmGrandParent.childNodes[oElmGrandParent.childNodes.length - 1].childNodes[0].focus();
						//first level; close menu and move focus to menu trigger
						//sub level; move focus to sub menu opener
						if (oElmGrandParent.className == 'dropContainer') {
							fCloseMenu(openId);
							//use a timeout when moving focus to the drop trigger 
							setTimeout("fFocusMenuTrigger('" + 'dT-' + sId + "')",0);
						}
						else {
							if (oElm == oElmGrandParent.childNodes[0].childNodes[0]) {
								oElmGrandParent.parentNode.childNodes[0].focus();
							}
						}
					}
				}
				//----- /IE
			}
		}
		//if ARROW RIGHT (39) has been pressed
		if (key == 39) {
			//move within triggers
			if (oElmParent.className.indexOf('dropTrigger') != '-1') {
				var goTrigger = 'dT-' + (parseInt(sId) + 1);
				if (document.getElementById(goTrigger)) {
					document.getElementById(goTrigger).focus();
				}
				else {
					document.getElementById('dT-0').focus();
				}
			}
			//open next level
			if (oElmGrandParent.className == 'dropContainer') {
				if (oElmParent.className.indexOf('subChildOn') != '-1') {
					//focus first link in submenu
					//----- NN, MOZ
					if (!document.all) {
						oElmParent.childNodes[2].childNodes[1].childNodes[0].focus();
						return false;
					}
					//----- /NN, MOZ
					//----- IE
					else {
						oElmParent.childNodes[2].childNodes[0].childNodes[0].focus();
						return false;
					}
					//----- /IE
				}
			}
			//go to sub level
			if (oElmParent.className.indexOf('subChildOn') != '-1' || oElmParent.className.indexOf('subChildOff') != '-1') {
				//focus first link in next submenu
				//----- NN, MOZ
				if (!document.all) {
					if (oElmParent.childNodes[2]) {
						oElmParent.childNodes[2].childNodes[1].childNodes[0].focus();	
					}
				}
				//----- /NN, MOZ
				//----- IE
				else {
					if (oElmParent.childNodes[2]) {
						oElmParent.childNodes[2].childNodes[0].childNodes[0].focus();	
					}
				}
			}
		}
		//if ARROW DOWN (40) has been pressed
		if (key == 40) {
			//if top level; open menu and focus first menu element
			if (oElmParent.className.indexOf('dropTrigger') != '-1') {
				if (document.getElementById(dropId)) {
					fOpenFirstMenuLevel();
				}
				else {
					return false;
				}
			}
			//move downwards inside menus, leaving last item skips to first
			else {
				if (oElmGrandParent.className == 'dropContainer' || oElmParent.className.indexOf('subChildOn') != '-1' || oElmParent.className.indexOf('subChildOff') != '-1') {
					//----- NN, MOZ
					if (!document.all) {
						if (oElmParent == oElmGrandParent.lastChild.previousSibling) {
							oElmGrandParent.firstChild.nextSibling.childNodes[0].focus();
						}
						else {
							if (oElmParent.nextSibling.nextSibling && oElmParent.nextSibling.nextSibling.childNodes[0].tagName == 'A') {
								oElmParent.nextSibling.nextSibling.childNodes[0].focus();
							}
						}
					}
					//----- /NN, MOZ
					//----- IE
					else {
						if (oElmParent.nextSibling && oElmParent.nextSibling.childNodes[0].tagName == 'A') {
							oElmParent.nextSibling.childNodes[0].focus();
						}
						else {
							oElmGrandParent.childNodes[0].childNodes[0].focus();
						}
					}
					//----- /IE
				}
			}
		}
	}
	//----- /KEYDOWN
	//----- MOUSEDOWN
	if (e.type == 'mousedown') {
		//if trigger matches open menu, close menu
		if (openId != null && dropId == openId) {
			fCloseMenu(openId);
			return false;
		}
		//if trigger does not match open menu close any open menu before opening next menu
		if (openId != null && dropId != openId) {
			fCloseMenu(openId);
		}
		//open menu, set id for open menu
		//--- frames
		if (crossFrame) {
			//position
			if (!document.all) {
				iPosLeft = e.target.parentNode.parentNode.offsetLeft;
			}
			else {
				iPosLeft = event.srcElement.parentElement.parentElement.offsetLeft;
			}
			parent.frames[dropFrame].document.getElementById(dropId).style.left = iPosLeft + dropMenuOffsetLeft + 'px';
			//show
			parent.frames[dropFrame].document.getElementById(dropId).style.visibility = 'visible';
			parent.frames[dropFrame].openId = dropId;
		}
		//--- /frames
		//--- noframes
		else {
			document.getElementById(dropId).style.display = 'block';
		}
		//--- /noframes
		//set drop id as open id
		openId = dropId;
	}
	//----- /MOUSEDOWN
	//----- MOUSEOVER
	if (e.type == 'mouseover') {
		//get menu item level
		fGetMenuItemLevel();
		//handle sub menus
		fHandleSubMenus();
	}
	//----- /MOUSEOVER
	//----- MOUSEOUT
	if (e.type == 'mouseover') {
		//get menu item level
		fGetMenuItemLevel();
		//handle sub menus
		fHandleSubMenus();
	}
	//----- /MOUSEOUT
}
// ------ /Handle dropmenu events -----

// ------ Get menu item level -----
function fGetMenuItemLevel() {
	var oLevelElm = oElm;
	iLevel = 0;
	while (oLevelElm.className != 'dropContainer') {
		if (oLevelElm.className.indexOf('dropContainerSubLevel') != '-1') {
			iLevel++;
		}
		//alert(oLevelElm.className + ', ' + oLevelElm.name + ', ' + oLevelElm.className.indexOf('dropContainerSubLevel'));
		oLevelElm = oLevelElm.parentNode;
		alert(oLevelElm);
	}
}
// ------ /Get menu item level -----

// ------ Open first level -----
function fOpenFirstMenuLevel() {
	//open first level
		document.getElementById(dropId).style.display = 'block';
		openId = dropId;
	//focus first element if no argument (ARROW DOWN) is submitted
	if (arguments.length == 0) {
		if (!document.all) {
			document.getElementById(dropId).childNodes[1].childNodes[0].focus();
		}
		else {
			document.getElementById(dropId).childNodes[0].childNodes[0].focus();
		}
	}
	return false;
}
// ------ /Open first level -----

// ------ Open sub level -----
function fOpenMenuSubLevels() {
	if (oElmParent.childNodes[2]) {
		//show sub menu
		oElmParent.childNodes[2].style.top = parseInt(oElmParent.offsetTop) + 'px';
		oElmParent.childNodes[2].style.left = oElmParent.offsetWidth + 'px';
		oElmParent.childNodes[2].style.display = 'block';
		//add open object to sub menu array
		//for IE 5.x
		if (is_ie5) {
			var arrCurrentItems = arrOpenSubItems;
			var arrAddNewItem = new Array(oElmParent.childNodes[2]);
			arrOpenSubItems.length = arrCurrentItems.length;
			arrOpenSubItems = arrCurrentItems.concat(arrAddNewItem);
		}
		//for other browsers
		else {
			arrOpenSubItems.push(oElmParent.childNodes[2]);
		}
	}
}
// ------ /Open sub level -----

// ------ Handle submenus -----
function fHandleSubMenus() {
	//items with children
	if (oElmParent.className.indexOf('subChildOn') != '-1') {
		//close submenu if already open, else open it
		if (oElmParent.childNodes[2].style.display == 'none') {
			//close any open submenu
			if (arrOpenSubItems.length > 0) {
				if (iLevel == 0) {
					//close all sub menus
					fCloseSubMenusAll();
				}
				else {
					//close all submenus below this position
					fCloseSubMenusBelow();
				}
			}
			//show sub menu
			fOpenMenuSubLevels();
		}
	}
	//items without children
	if (oElmParent.className.indexOf('subChildOff') != '-1') {
		//check if a level below this level is open, then close it 
		if (arrOpenSubItems.length > iLevel) {
			if (iLevel == 0) {
				//close all sub menus
				fCloseSubMenusAll();
			}
			else {
				//close all submenus below this position
				fCloseSubMenusBelow();
			}
		}
	}
}
// ------ /Handle submenus -----

// ------ Close all sub menus below current position -----
function fCloseSubMenusBelow() {
	//close all sub menus below this level
	var newArrayLength = arrOpenSubItems.length;
	for (var j=0;j<arrOpenSubItems.length;j++) {
		if ((j+1) > iLevel) {
			arrOpenSubItems[j].style.display = 'none';
			newArrayLength--;
		}
	}
	//adjust sub menu array length
	arrOpenSubItems.length = newArrayLength;
}
// ------ /Close sub menus below current position -----

// ------ Close all sub menus -----
function fCloseSubMenusAll() {
	//close all sub menus
	arrOpenSubItems.reverse();
	for (var i=0;i<arrOpenSubItems.length;i++) {
		arrOpenSubItems[i].style.top = 0;
		arrOpenSubItems[i].style.left = 0;
		arrOpenSubItems[i].style.display = 'none';
	}
	//reset sub menu array length
	arrOpenSubItems.length = 0;
}
// ------ /Close all sub menus -----

// ------ Focus menu trigger -----
function fFocusMenuTrigger(sFocusId) {
	// Set the focus
	document.getElementById(sFocusId).focus();
}
// ------ /Focus menu trigger -----




