﻿	function menu(name,id)
	{
		this.head = name;
		this.menuid = id;
		this.activateMenu = activateMenu;
		this.menus = [];
		this.hideDelay;
		this.menuCount;
	}
	
	function menuNode(obj)
	{
		this.show = show;
		this.hide = hide;
		this.index;
		this.obj = obj;
		this.menuTimer = null;
		this.parentIndex = null;
		this.menuLevel = 0;
		this.isRoot = 0;
	}
		
	
	var mainMenu = new menu('mainMenu','menuTop');
	
	mainMenu.hideDelay = 200;
	
	function activateMenu(menuObj){ with(this)
	{	
//		var menuTopObj = document.getElementById("menuTop");
		
		var menuList = document.getElementsByTagName('ul');
		menuCount = 0;
		var ulRoot = menuList[0], node;
		
		

		for(var i = 0; i < menuList.length; i++)
		{
			var li, ul;
			node = ul = menuList[i];
			// Checking to see if the current 'ul' is a submenu 
			if(ul.parentNode.nodeName.toLowerCase() == 'li')
			{
				li = ul.parentNode;
			}
			else
			{
				// Assigning the Top level menus properties
				menus[menuCount] = new menuNode(ul);
				menus[menuCount].parentIndex = null;
				menus[menuCount].isRoot = 1;
				menus[menuCount].index = menuCount++;
				continue;
			}
			
			var a = null;
			for(var j = 0; j < li.childNodes.length; j++)
			{
				if(li.childNodes[j].nodeName.toLowerCase() == 'a')
				{
					a = li.childNodes[j];
				}
			}
			menus[menuCount] = new menuNode(ul);
			menus[menuCount].index = menuCount;
			
			// Assigning menu levels to each node
			while(node.nodeName.toLowerCase() != 'body')
			{
				node = node.parentNode;
				if(node.nodeName.toLowerCase() == 'ul')
					menus[menuCount].menuLevel++;

			}	
			
			// Find the parent menu in the list of Menus using the above calculated menu Levels
			// This is based on the observation that the parent's menus level is 
			// just one number less than the current menu and is the first such menu when traversing 
			// the list of menus in the reverse direction.
			
			for(var k = menuCount; k > 0; k--)
			{
				menus[menuCount].parentIndex = k - 1;
				if(menus[menuCount].menuLevel > menus[k - 1].menuLevel)
				{
					break;
				}

			}
			
			//Create a function to do the following
			/*
				1. Show the current menu upon mouse move over
				2. Clear the time out of the parent menu when mouse is over the sub menu
				3. Clear the time out of the curent menu when mouse is over it
			*/
			
			var eshow = new Function('mainMenu.menus['+ menuCount+ '].show();'+
									'clearTimeout(mainMenu.menus[mainMenu.menus['+menuCount+
									'].parentIndex].menuTimer);'+
									'clearTimeout(mainMenu.menus['+menuCount+'].menuTimer);');

			//Create a function to do the following
			/*	
				1. Hide the current menu when mouse moves out
				2. Check to see if the parent of the current menu is root 
					a. If yes on mouse move out just hide the current menu
					b. If no hide the current menu as well as the parent menu 
			*/
			var ehide = new Function('mainMenu.menus['+ menuCount+ '].menuTimer = setTimeout' + 
									'("mainMenu.menus['+menuCount+'].hide()",mainMenu.hideDelay);'+
									'if(mainMenu.menus[mainMenu.menus['+menuCount+'].parentIndex].isRoot == 0)'+
									'mainMenu.menus[mainMenu.menus['+menuCount+'].parentIndex].menuTimer'+
									'= setTimeout("mainMenu.menus[mainMenu.menus['+menuCount+'].parentIndex].hide()"'+
									',mainMenu.hideDelay);');

			// Locate all the leaf nodes
			for(var j = 0; j < ul.childNodes.length; j++)
			{
				if( ul.childNodes[j].nodeName.toLowerCase() == 'li')
				{
					var hasSubMenu = 0;
					li = ul.childNodes[j];
					for(var k = 0; k < li.childNodes.length; k++)
					{
						if(li.childNodes[k].nodeName.toLowerCase() == 'ul')
						{
							hasSubMenu = 1;
							break;
						}
					}
					if(hasSubMenu == 1)
					{
						continue;
					}
					else
					{
						for(var l = 0; l < li.childNodes.length; l++)
						{
							if(li.childNodes[l].nodeName.toLowerCase() == 'a')
							{
								addEvent(li.childNodes[l],'mouseover',eshow);
								addEvent(li.childNodes[l],'mouseout',ehide);
//								alert(li.childNodes[l].id);
							}
							else
								continue;
						}
					}
				}
					
			}
			
			addEvent(a,'mouseover',eshow);
			addEvent(a,'mouseout', ehide);
			menuCount++;
		}
		
	}}
	
	function addEvent(obj,eventType,fn)
	{
		if (obj.addEventListener)
		{ 
			obj.addEventListener(eventType, fn, false); 
			return true; 
		}
		else if (obj.attachEvent)
		{ 
			var r = obj.attachEvent('on' + eventType, fn); 
			return r; 
		} 
		else
		{ 
			return false; 
		}
	}
	


	function show()
	{
//		clearTimeout(hideDelay);
//		menuSubObj = document.getElementById("menuSub");
//		alert("inside show!" + obj.id);
//		var menuObject = document.getElementById(this.obj.id);
		this.obj.style.visibility = 'visible';
		return;
	}
	
	function hide()
	{
//		var menuObject = document.getElementById(this.obj.id);
//		clearTimeout(hideDelay);
//		var hideDelay = this.setTimeout("this.obj.style.visibility = 'hidden'",100);
		this.obj.style.visibility = 'hidden';
		return;
	}		
	

