// Cascading pull down menus
// Works with IE 4.0 + and Mozilla 1.2+

var menuStack = new Array();
var submenuStack = new Array();

var bMoved = false;
var origClassName = null;

var offsetX = 2;
var offsetY = 48;

function getObject(name)
{
	var obj = null;
	if (document.getElementById) {
	  	obj = document.getElementById(name);
	}
	else if (document.all) {
		obj = document.all[name];
	}
	else if (document.layers) {
		obj = getObjNN4(document,name);
	}
	return obj
}

function getObjNN4(obj,name)
{
	var x = obj.layers;
	var foundLayer;
	for (var i=0;i<x.length;i++)
	{
		if (x[i].id == name)
		 	foundLayer = x[i];
		else if (x[i].layers.length)
			var tmp = getObjNN4(x[i],name);
		if (tmp) foundLayer = tmp;
	}
	return foundLayer;
}


function getStyleObject(objectId) {
	var obj = getObject(objectId)
	if (!obj)
		return null
		
	if(document.getElementById || document.all) {
		return obj.style;
    } else if (document.layers) {
		 return obj;
    } else {
		return null;
    }
} 

function changeObjectVisibility(objectId, newVisibility) {
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
		styleObject.visibility = newVisibility;
		return true;
    } else {
		// we couldn't find the object, so we can't change its visibility
		return false;
    }
}

function moveObject(objectId, newXCoordinate, newYCoordinate) {
	var styleObject = getStyleObject(objectId);
    if(styleObject) {
		styleObject.left = newXCoordinate;
		styleObject.top = newYCoordinate;
		return true;
    } else {
		// we couldn't find the object, so we can't very well move it
		return false;
    }
} 

function fixChildrenWidth(objectId)
{
	if(!document.getElementById && !document.all)
		return 

	// a hack to ensure that the A HREF inside the DIV is set to the full width of the row
	var obj = getObject(objectId)
	if (!obj)
		return null

	var node = null;
	for (node = obj.firstChild; node != null; node = node.nextSibling){
		var aref = node.firstChild
		if (aref)
			if (aref.style.width < node.offsetWidth-8)
				aref.style.width = node.offsetWidth-8
	}
}

function getParentElement(child){
  var parent=null;
  
  if (child==window)
     parent=null;
  else if (child==document) 
     parent=window;
  else if (child.parentLayer){
  	if (child.parentLayer !=window)
		parent=child.parentLayer;
  }
  else{
    if (child.parentNode) 
		parent=child.parentNode;
    else if (child.offsetParent) 
		parent=child.offsetParent;
    else if (child.parentElement) 
		parent=child.parentElement;
 }
  return parent;
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}


function show_props(obj) 
{         
 	var result = ""          
 	for (var i in obj)
       result +=  i + " = " + obj[i] + ", " 
	return result
}
	
function show(name,lvl,obj)
{
	if (bMoved)
		clearTimeout(bMoved);
	
	if (menuStack[lvl] && menuStack[lvl] == name)
	    return; // this item is already open - just return
	
	if (menuStack[lvl]){
		closeAll(lvl);  // new menu item moved over - close the old one
	}
	
	// get the parent object, save it's class & set the hover class
	var parent = getParentElement(obj)
	if (parent && lvl==1){
		// special case code cause the top line are buttons.
		if (submenuStack[lvl] && origClassName)
			submenuStack[lvl].className = origClassName
			
		origClassName = parent.className
		if (origClassName.toLowerCase().indexOf("long")>=0)
			parent.className="topMenuBarItemALong"
		else
			parent.className="topMenuBarItemA"
		
		submenuStack[lvl] = parent
	}
	else if (lvl>1){				
		if (obj.className != "menuLinkA") {
			obj.className = "menuLinkA"	
								
			if (parent)
				parent.className = "menuLinkA"
			
			if (submenuStack[lvl]){
				submenuStack[lvl].className = "menuLink"
				
				var oldElemParent = getParentElement(submenuStack[lvl])
				if (oldElemParent)
					oldElemParent.className = "menuLink"
			}

			submenuStack[lvl] = obj
		}
	}
	
	// open the currently hovered over menu
	if (name){
		var posX = 0
		var posY = 0
		var widthX = 0
		var heightY = 0

		if (parent){
			posX = findPosX(parent)
			posY = findPosY(parent)
			widthX = parent.offsetWidth
			heightY = parent.offsetHeight
		}
		else {
			posX = findPosX(obj)-20
			posY = findPosY(obj)-5
			widthX = offsetX
			heightY = offsetY
		}
	
		if (lvl == 1){
			//posX += 1
			posY += heightY  // +1
		}
		else{
			posX += widthX + 5;
			
			var rightedge=document.body.clientWidth
			var bottomedge=document.body.clientHeight
			
			var offWidth = 0
			var offHeight = 0
			
			var obj = getObject(name)
			if (obj){
				offWidth = obj.offsetWidth
				offHeight = obj.offsetHeight
				rightedge = rightedge - offWidth
				bottomedge = bottomedge - offHeight
			}
	
			if (posX > rightedge){
				if (rightedge < posX-widthX+20)
					posX = posX - widthX - offWidth - 8
				else
					posX = rightedge
				
				if (posX < 0)
					posX = 0
			}
			
			if (posY > bottomedge){
				posY = bottomedge
				if (posY < 0)
					posY = 0
			}
		}
		
			
		fixChildrenWidth(name)
		moveObject(name, posX, posY) 
		changeObjectVisibility(name, 'visible')
		menuStack[lvl] = name;	
	}
}

function restore(obj)
{
	checkUserInput();
	// get the parent object & save it's class
	//var parent = getParentElement(obj)
	//if (parent && origClassName){
	//	parent.className = origClassName;
	//	origClassName = null;
	//}
}

function closeAll(lvl)
{
	for (i=menuStack.length - 1;i>=lvl;i--){
		if (menuStack[i])
			changeObjectVisibility(menuStack[i], 'hidden')
		menuStack[i] = null;
		
		if (submenuStack[i]){
			if (i==1 && origClassName){
				// special case code cause the top line are buttons
				submenuStack[i].className = origClassName;
			}
			else{
				submenuStack[i].className = 'menuLink';
				
				var oldElemParent = getParentElement(submenuStack[i])
				if (oldElemParent)
					oldElemParent.className = "menuLink"
			}
			submenuStack[i] = null;
		}
	}
}

function checkUserInput()
{
	if (bMoved) 
		clearTimeout(bMoved);
	bMoved = setTimeout('closeAll(1)',400);
}

