// JavaScript Document

var popupId;

function getParent(el, pTagName) {
	if (el == null) {
		return null;	//passed the root node
		//alert("passed the root node");
	}
	else if (el.nodeType == 1 && el.tagName.toLowerCase() == pTagName.toLowerCase()) {
		// The nodeType == 1 is a test if the node is an actual element
		// Gecko has a bug that gives HTML tagNames in lowercase
		//alert("found " + pTagName);
		return el;
	}
	else {
		//alert("checking " + el.tagName);
		return getParent(el.parentNode, pTagName);	// traverse up
	}	
}

function isClassOf(node, cClassName) {
	return (node.className==cClassName || 
			node.className.indexOf(cClassName + " ")==0 || 
			((node.className.indexOf(" " + cClassName)>=0) &&
				  (node.className.indexOf(" " + cClassName)==(node.className.length - cClassName.length - 1))) ||
				((node.className.indexOf(" " + cClassName + " ")>=0)));
}

function getChildByClass(el, cClassName) {
  if (el!=null) {
	  for (var i=0; i<el.childNodes.length; i++) {
		node = el.childNodes[i];
		if (node.className!=null && isClassOf(node,cClassName))
			return node;
	  }
  }
  return null;
}

function getDeepChildByClass(el, className, level) {
  if (el!=null) {
	  for (var i=0; i<el.childNodes.length; i++) {
		node = el.childNodes[i];
		if ((node.className!=null) && isClassOf(node,className)) {
			return node;
		} else {
			var result = getDeepChildByClass(node, className, level + 1 );
			if (result!=null) {
				return result;
			}
		}
	  }
  } 
  return null;
}

startList = function() {
	detect = navigator.userAgent.toLowerCase();
	msie=detect.indexOf("msie");
	opera=detect.indexOf("opera");

	navRoot = document;
	//doList(navRoot);
}

function doList(element) {
var navRoot=element;
var i;

for (var i=0; i<navRoot.childNodes.length; i++) {
node = navRoot.childNodes[i];

if (node.nodeName=="LI") {
	if (app.isIE4 || app.isIE5) {
	
		node.onmouseover=function() {
			this.className+=" over";
		        if (document.all)
		        {
		        	subMenu=getChildByClass(this,"menu");
					if (subMenu!=null) {
		                subMenu.overlapList = new Array ();
		                hideOverlaped('SELECT', subMenu);
		            }
		        }
		}
		
		node.onmouseout=function() {
			this.className=this.className.replace(" over", "");
		        if (document.all)        // it is IE
		        {
		        	subMenu=getChildByClass(this,"menu");
					if (subMenu!=null) {
		                showOverlaped(subMenu);
		            }
		        }
		}
	} else {
		node.onmouseover=function() {
			//alert("On mouse over");
		}
	}
}
doList(node);
}

}

function menuItemOver(item) {
	if (app.isIE4 || app.isIE5) {
		item.className+=" over";
		if (document.all) {
	       	subMenu=getChildByClass(item,"menu");
			if (subMenu!=null) {
		    	subMenu.overlapList = new Array ();
			    hideOverlaped('SELECT', subMenu);
			}
		}
	}
	var submenu = getDeepChildByClass(item, "menu");
	if (submenu != null) {
		var itemCoords = getCoords(item);
		var yPos = yMousePos;
		var dY = 0;
		if ((itemCoords.top - document.documentElement.scrollTop + submenu.clientHeight) > document.documentElement.clientHeight) {
			if (submenu.clientHeight + 10 <= document.documentElement.clientHeight) {
				dY = document.documentElement.clientHeight + (scroll.initHeight-document.documentElement.clientHeight) - (itemCoords.top + submenu.clientHeight) - 25;
			} else {
				overSize = submenu.clientHeight - document.documentElement.clientHeight;
				mousePercent = (yPos-20.0)/(document.documentElement.clientHeight-40);
				dY = -itemCoords.top - Math.round(mousePercent*overSize);
			}
			submenu.style.top = (document.documentElement.scrollTop + dY) + 'px';
		}
	}
}

function menuItemOut(item) {
	if (app.isIE4 || app.isIE5) {
		item.className=item.className.replace(" over", "");
		if (document.all)        // it is IE
		{
			subMenu=getChildByClass(item,"menu");
			if (subMenu!=null) {
				showOverlaped(subMenu);
			}
		}
	}
}
popup_over=function(image) {
	li=getParent(image,"LI");
		
	
	if ((menu=getChildByClass(li,"menu"))!=null) {
		popupId = li;
		menu.className="menu1";
	} else if ((menu=getChildByClass(li, "menu1"))!=null) {
		popupId = li;
		menu.className="menu";
	}	
	if ((popup=getChildByClass(li,"popup"))!=null) {
		popup.className="popup1";
		//image.src="../images/info_sign_pushed.gif";
		image.src="/site/images/info.gif";
	}	else if ((popup=getChildByClass(li,"popup1"))!=null) {
		popup.className="popup";
		//image.src="../images/info_sign_pushed.gif";
		image.src="/site/images/info.gif";
	}
}

//window.onload=startList;

// helper functions
function getX (obj)
{
	var x = 0;

	do
	{
		x += obj.offsetLeft;
		obj = obj.offsetParent;
	}
	while (obj);
	return x;
}

function getY (obj)
{
	var y = 0;
	do
	{
		y += obj.offsetTop;
		obj = obj.offsetParent;
	}
	while (obj);
	return y;
}


function hideOverlaped (tagName, subMenu)
{
	var x = getX(subMenu);
	var y = getY(subMenu);
	var w = subMenu.offsetWidth;
	var h = subMenu.offsetHeight;

	var i;
	for (i = 0; i < document.all.tags(tagName).length; ++i)
	{
		var obj = document.all.tags(tagName)[i];
		if (!obj || !obj.offsetParent)
			continue;

		// check if the object and the subMenu overlap
		var ox = getX(obj);
		var oy = getY(obj);
		var ow = obj.offsetWidth;
		var oh = obj.offsetHeight;

		if (ox > (x + w) || (ox + ow) < x)
			continue;
		if (oy > (y + h) || (oy + oh) < y)
			continue;

		// if object is already made hidden by a different
		// submenu then we dont want to put it on overlap list of
		// of a submenu a second time.
		if(obj.style.visibility != "hidden") {
			subMenu.overlapList[subMenu.overlapList.length] = obj;
			obj.style.visibility = "hidden";
		}
	}
}

//
// show the control hidden by the subMenu
//
function showOverlaped (subMenu)
{
	if (subMenu.overlapList)
	{
		var i;
		for (i = 0; i < subMenu.overlapList.length; ++i)
			subMenu.overlapList[i].style.visibility = "";
	}
	subMenu.overlapList = null;
}

var menuItems = new Array();

// hide menu onClick
function hideOnClick(menu)
{
	grandparent=menu;
	greatgrandparent=null;
	i=0;
	menuItems = new Array();
	while (grandparent!=null) {
		//alert("getting parent");
		// for IE - clear all over's
		parentLI = getParent(menu,"LI");
		parentLI.className = parentLI.className.replace(" over","");
		
		par = getParent(menu, "UL");
		//alert("getting grandparent");
		grandparent = getParent(par.parentNode,"UL");
		if (grandparent != null) {
			greatgrandparent = getParent(grandparent.parentNode,"UL");
		}
		//alert(grandparent);
		if (greatgrandparent == null && grandparent!=null) {
			par.className = par.className.replace("menu","menu1");
			//alert("2");
			//document.forms["datasheets"].elements["categoryName"];
			//alert("focused");
			//alert("Adding menu item");
			//menuItems[i]=par;
			i++;
			par1=par;
			setTimeout("showMenu(par1)",1000);
//			alert("Tiemeout set");
		} 	
			menu=par.parentNode;
	}
}

function showMenu(menu) {
//alert("3");
	//for (var i=0; i<menuItems.length; i++) {
		//alert("old class: " + menu.className);
		menu.className=menu.className.replace("menu1", "menu");
		//alert("visibility restored: " + menu.className);
	//}
}

function moveMenu(item) {
	alert("Moving menu");
}

commands[commandIter++] = "startList()";


