/*
	The drop out menu script for Websense.com.
	
	This script will utilize any of the links with ids in the 
	parentDivId and check if there are subNav items 
	based on that same id name with the subNavExt 
	prepended to them. From this it will add the correct 
	mouse over and out functionality to both the parent 
	and child elements as well as add the menuOverOnExt 
	to the parent ids class to keep the parent highlighted.
	
	To use the menu, make the appropriate changes to 
	the initilization variables, add the ids to the links within 
	the parentDivId layer, add subnavs with the 
	correct divs.

*/
onload = init;

/*
	get the browser type
*/
ns4 = (document.layers)? true : false;
ie4 = (document.all)? true : false;
dom = (document.getElementById)? true : false;

/*
	initilization variables
*/
var parentDivId = "navItems";
var subNavExt = "sub_";
var menuOverOnExt = "Over";
var hideDelay = 200;
var menuTimer = 0;
var trackCurrentMenu = null;
var trackOnMenu = null;

function init() {
	/*
		initilization function for the menu - called onload
	*/
	if (dom || ie4) {
		menu = new addMenuItems(parentDivId);
		trackOnMenu = getOnMenu();
		startMenuOn();
	}
}

function addMenuItems(divId) {
	/*
		adds the functionality to the menu items that have 
		secondary items utilitizing the ids on each of the 
		menu links and prepending the subNavExt to them
	*/
	obj = getElement(divId);
		
	for (i=0; i<obj.childNodes.length; i++) {
		node = obj.childNodes[i];
		if (node.attributes) {
						
			node.onmouseout= function() {
				/*
					add the functionality for the mouse out and 
					hiding the appropriate menu
				*/
				
				subNode = getSubNode(this);
							
				if (subNode) {
					
					subNode.onmouseout = function() {
						/*
							add the functionality for the sub menu 
							items for the mouse out
						*/
						
						startTimer(this)
					}
					
					subNode.onmouseover = function() {
						/*
							add the functionality for the sub menu 
							items for the mouse over
						*/
						
						clearTimeout(menuTimer);
					}
					startTimer(subNode)
					
					
				} else {
					/*
						for menus that do not have any sublevels 
						we must return to the On Menu
					*/
					clearTimeout(menuTimer);
					showCurrentMenu();
					changeStyleOff();
				}
			}
			
			node.onmouseover= function() {
				/*
					add the functionality for the mouse over and 
					showing the appropriate menu
				*/
				
				
				changeStyleOff();
				/*if (this.attributes.id.value != trackOnMenu) {
					if (this.className.indexOf(menuOverOnExt) == -1)
						this.className+=menuOverOnExt;
				}*/

				if (this.id != trackOnMenu) {
					if (this.className.indexOf(menuOverOnExt) == -1)
						this.className+=menuOverOnExt;
				}
				
				clearMenus();

				//trackCurrentMenu = this.attributes.id.value;
				trackCurrentMenu = this.id;
				
				subNode = getSubNode(this);

				clearTimeout(menuTimer);

				if (subNode) {
					displayBlock(subNode);
					hideDropDowns(subNode)
				}
			}
		}
	}	
}

function hideDropDowns(node){
	var dropDowns = document.getElementsByTagName('select');

	var nodeLeft = findPosX(node);
	var nodeTop = findPosY(node);
	var nodeWidth = node.clientWidth;
	var nodeHeight = node.clientHeight;
	var nodeRight = nodeLeft + nodeWidth;
	var nodeBottom = nodeTop + nodeHeight;


	for(var i = 0; i < dropDowns.length; i++){
		if(dropDowns[i].id != 'GlobalLanguageSelect' && dropDowns[i].id != 'ctl00_Header1_ddLanguage') {
			
			var ddLeft = findPosX(dropDowns[i]);
			var ddTop = findPosY(dropDowns[i]);
			var ddWidth = dropDowns[i].clientWidth;
			var ddHeight = dropDowns[i].clientHeight;
			var ddRight = ddLeft + ddWidth;
			var ddBottom = ddTop + ddHeight;

			if( nodeBottom > ddTop && ( (nodeRight > ddLeft && nodeRight < ddRight) || (nodeLeft > ddLeft && nodeLeft < ddRight) || (ddLeft > nodeLeft && ddRight < nodeRight) ) ) {
				dropDowns[i].style.visibility = 'hidden';
			} else {
				dropDowns[i].style.visibility = 'visible';
			}

		}
	}
}

function showDropDowns(){
	var dropDowns = document.getElementsByTagName('select');
	for(var i = 0; i < dropDowns.length; i++){
		dropDowns[i].style.visibility = 'visible';
	}
}

function startTimer(node) {
	/*
		timeout for the menu system - controlled in millseconds 
		using hideDelay
	*/
	//ID = node.attributes.id.value;
	ID = node.id;
	//if (trackCurrentMenu != trackOnMenu)
		
		menuTimer = setTimeout("displayNoneById('" + ID + "')", hideDelay);
}

function showCurrentMenu() {
	/*
		shows the current menu after a user has rolled off others 
		if there is a OnMenu
	*/
	//alert(trackOnMenu)

	/*
	if (trackOnMenu != null) {
		obj = getElement(trackOnMenu);
		subNode = getSubNode(obj);
		if (subNode != null) {
			displayBlock(subNode);
		}
	}
	*/
}

function changeStyleOff() {
	/*
		turns the style of the main nav back to its original class 
		by removing the 
	*/
	/*
	if (trackCurrentMenu != null) {
		obj = getElement(trackCurrentMenu);
		if (obj.attributes.id.value != trackOnMenu)
			obj.className=obj.className.replace(menuOverOnExt, "")
	}
	*/

	if (trackCurrentMenu != null) {
		obj = getElement(trackCurrentMenu);
		if (obj.id != trackOnMenu)
			obj.className=obj.className.replace(menuOverOnExt, "")
	}
}

function displayBlock(node) {
	/*
		changes display to block depending on the node - which 
		is typically a sub nav item
	*/
	//hideDropDowns(node);
	node.style.display = "block";
}
function displayNone(node) {
	/*
		changes display to none depending on the node - which 
		is typically a sub nav item
	*/
	node.style.display = "none";
}
function displayNoneById(ID) {
	/*
		changes display to none based on the ID of an element 
		that you pass it
	*/
	obj = getElement(ID);
	obj.style.display = "none";
	showDropDowns();
	showCurrentMenu();
	changeStyleOff();
}

function clearMenus() {
	/*
		turn off all sub menus if there are sub menus to be 
		turned off
	*/
	obj = getElement(parentDivId);
	
	for (i=0; i<obj.childNodes.length; i++) {
		node = obj.childNodes[i];
		if (node.attributes) {
			subNode = getSubNode(node);
			
			if (subNode != null)
				displayNone(subNode)
		}
	}
}

function findPosX(obj)  {
	/*
		find the x position of the passed node
	*/
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

function findPosY(obj)	{
	/*
		find the x position of the passed node
	*/
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }



function getSubNode(node) {
	/*
		find the sub nodes based on the extension and the 
		node that it is passed
	*/
	
	//nodeIdName = node.attributes.id.value
	nodeIdName = node.id;
	nodeIdName = subNavExt + nodeIdName;
	
	subNode = getElement(nodeIdName);

	
	var leftPos = findPosX(node);
	
	//Set the left position to the same as the parent node
	subNode.style.left = leftPos + 'px';
	
	return subNode;
}

function getOnMenu() {
	/*
		find the current on utilizing the On within the class of 
		the node and then use this for tracking purposes
	*/
	obj = getElement(parentDivId);
	
	/*
	for (i=0; i<obj.childNodes.length; i++) {
		node = obj.childNodes[i];
		if (node.attributes) {
			if (node.className.indexOf("On") > 0)
				return node.attributes.id.value
		}
	}
	*/

	for (i=0; i<obj.childNodes.length; i++) {
		node = obj.childNodes[i];
		if (node.attributes) {
			if (node.className.indexOf("On") > 0)
				return node.id
		}
	}
}

function startMenuOn() {
	/*
		turn the subnav on if there is a sub node for it on the 
		load of the page
	
	obj = getElement(parentDivId);
	
	for (i=0; i<obj.childNodes.length; i++) {
		node = obj.childNodes[i];
		if (node.attributes) {
		
			if (node.attributes.id.value == trackOnMenu) {
				subNode = getSubNode(node);
				if (subNode != null)
					displayBlock(subNode)
			}
		}
	}
	*/	
}

function getElement(ID) {
	if (ns4) {
		return null;
	} else if (dom) {
		return (document.getElementById(ID))? document.getElementById(ID) : null;
	} else if (ie4) {
		return (document.all[ID])? document.all[ID] : null;
	}
}


