// Simple navigation menu. Snippets of which were borrowed from http://www.dynamicdrive.com/dynamicindex1/dropmenuindex.htm
// I got the general idea from their code, and modified quite a bit, but I'll leave their license in here just in case.
// ** Drop down menu link- © Dynamic Drive (www.dynamicdrive.com)
// ** For full source code and 100's more DHTML scripts, visit http://www.dynamicdrive.com
// ** Credit MUST stay intact for use

var zIndex = 100;
var isNav4 = document.layers;
var isNav6 = (document.getElementById && !document.all);
var isIe4  = document.all;
var OFF = (isNav4 ? "hide" : "hidden");
var ON = (isNav4 ? "show" : "visible"); 
var WAIT = null;

// helper for getting Object in dom space
function getObject(id) {
	var obj = isNav6 ? document.getElementById(id) : eval(id);
	return ((isNav6 || isIe4) ? obj.style : obj);
}

var menuObj = null;

function _hide() {
	this.objRef.visibility = this.status = OFF;
	if(isNav4) this.event.cancelBubble = true;
	return false;
}

function _show() {
	this.objRef.visibility = this.status = ON;
	
	if(isNav4) this.event.cancelBubble = true;
	this.timeout = false;
	return false;
}

// create menu object event and id
function MenuObject(e, id) {
	this.id = id;
	this.objRef = getObject(id);
	this.status = OFF;	//not being used, but just in case
	this.event = e;
	this.hide = _hide;
	this.show = _show;
	this.offsetX = (isIe4==-1)? document.body.scrollLeft : 0;
	this.offsetY = (isIe4==-1)? document.body.scrollTop : 0;
	this.objRef.left= (isNav6||isNav4) ? e.pageX-e.layerX-20 : this.offsetX+event.clientX-event.offsetX-20
	this.objRef.top = (isNav6||isNav4) ? e.pageY-e.layerY+30 : (this.offsetY + event.clientY-event.offsetY+30) + getScrolled();
	return this;
}

function getScrolled() {
	var scrolledY;
	if (document.documentElement && document.documentElement.scrollTop)
		scrolledY = document.documentElement.scrollTop;
	else if (document.body)
		scrolledY = document.body.scrollTop
	return scrolledY;
}


function hideMenu(event, menuId) {
	if(navMenu.menus[menuId] && !navMenu.menus[menuId].menuItems) {
		return;
	}
	if(menuObj == null) {
		navMenu.menuObjs[menuId] = new MenuObject(event, menuId);
		menuObj = navmenu.menuObjs[menuId];
	}
	WAIT = window.setTimeout("menuObj.hide()", 800);
}

function showMenu(event, menuId) {
	if(navMenu.menus[menuId] && !navMenu.menus[menuId].menuItems) {
		if(menuObj && navMenu.menus[menuObj.id] && navMenu.menus[menuObj.id].menuItems) {
			menuObj.hide();
		}	
		return;
	}
	if(menuObj == null) {
		if(!navMenu.menuObjs[menuId]) {
			navMenu.menuObjs[menuId] = new MenuObject(event, menuId);
		}
	}
	if(menuObj && menuObj.id != menuId) {
		menuObj.hide();
		if(!navMenu.menuObjs[menuId]) {
			navMenu.menuObjs[menuId] = new MenuObject(event, menuId);
		}		
	}
	menuObj = navMenu.menuObjs[menuId]
	window.clearTimeout(WAIT);
	menuObj.show();	
}



function MenuItem(name, link) {
	this.name = name;
	this.link = link;
}

function _addMenuItems() {
	if(this.menuItems == null) {
		this.menuItems = new Array();
	}
	for(var i=0; i < arguments.length; i++) {
		this.menuItems[this.menuItems.length] = new 
			MenuItem(arguments[i++], arguments[i]);
	}
}

function _getMenuItems() {
	return this.menuItems;
}

function Menu(name, link, features) {
	this.name = name;
	this.link = link;
	this.addMenuItems = _addMenuItems;
	this.getMenuItems = _getMenuItems;
	if(features) {
		this.paths = features.split(",");
	} else {
		this.paths = new Array();
	}	
}
	
function _getMenu(id) {
	if(this.menus[id] != null) {
		return this.menus[id];
	}
}

function _addMenu(id, menuObj) {
	this.menus[id] = menuObj;
}

function _displayMenu(id) {
	var m = this.menus[id];
	
	var selected = false;
	if(m) {
		leftImg = "<img src='./images/null.gif' width=4 height=24 border=0>";
		rightImg = leftImg;
		style = "mainNav";
		for(i=0; i < m.paths.length; i++) {
			if(window.location.pathname.indexOf(m.paths[i]) == 0) {
				selected = true;
			}
		}
		if(selected) {
			leftImg = "<img src='./images/tab_left.gif' width=4 height=24 border=0>";
			rightImg = "<img src='./images/tab_right.gif' width=7 height=24 border=0>";
			style = "selectNav";
		}
		document.writeln("<td>" + leftImg + "</td>");
		document.writeln("<td  class="+ style +" nowrap>");
		document.writeln("<ilayer><layer visibility=hide><div class=wrap1>");
		document.writeln("<span class=wrap2 onmouseout=\"hideMenu(event, '"+id+"')\" onmouseover=\"showMenu(event, '"+id+"');\">");
		document.writeln("<a href='"+m.link+"'>"+m.name+"</a></span>&nbsp;&nbsp;</div></layer></ilayer></td>");
		document.writeln("<td>" + rightImg + "</td>");

		mItems = m.getMenuItems();
		if(mItems) {
		    if (id == "ContactLensTechLabel" || id == "IOLTechLabel" ){
			 document.writeln("<div id='"+ id + "' onmouseover=\"showMenu(event, '" + id + "')\" onmouseout=\"hideMenu(event, '" + id + "')\" style=\"position:absolute;left:0;top:0;layer-background-color:white;background-color:white;width:170;visibility:hidden;border:1px solid black;padding:3px\">");
			 }
			else if ( id == "IOLLabel" || id =="TechDocumentLabel" ){
			 document.writeln("<div id='"+ id + "' onmouseover=\"showMenu(event, '" + id + "')\" onmouseout=\"hideMenu(event, '" + id + "')\" style=\"position:absolute;left:0;top:0;layer-background-color:white;background-color:white;width:250;visibility:hidden;border:1px solid black;padding:3px\">");
			 }
			 else if ( id == "contactLensLabel" ){
			 document.writeln("<div id='"+ id + "' onmouseover=\"showMenu(event, '" + id + "')\" onmouseout=\"hideMenu(event, '" + id + "')\" style=\"position:absolute;left:0;top:0;layer-background-color:white;background-color:white;width:130;visibility:hidden;border:1px solid black;padding:3px\">");
			 }
			else{
			  document.writeln("<div id='"+ id + "' onmouseover=\"showMenu(event, '" + id + "')\" onmouseout=\"hideMenu(event, '" + id + "')\" style=\"position:absolute;left:0;top:0;layer-background-color:white;background-color:white;width:100;visibility:hidden;border:1px solid black;padding:3px\">");
			  }
			for(i=0; i < mItems.length; i++) {
				document.writeln("<div class='subMenu'><a href='" + mItems[i].link + "'>" + mItems[i].name + "</a></div>" );			
			}
			document.writeln("</div>");
		}		
	}
}


function NavMenu() {
	this.menus = new Object();
	this.menuObjs = new Object(); //hashmap
	this.addMenu = _addMenu;
	this.getMenu = _getMenu;
	this.displayMenu = _displayMenu;
}


