// JavaScript Document

var offsetLeft = 1;				// discript the offset which is added to the calculate left position of a submenu
var offsetTop = 1;				// offset of the first level of sub menus
var offsetTopOfSubMenu = 05;		// offset of submenus of submenus
var menuOverlap = 0.05;

var hideDelay = 500;
var hidingTimerId = 0;
var hidingElementId = null;

var activeMenus = new Array();

// mouseIn()
function mouseOver(elementId, menuId, subMenuId) {
	var menu
	var subMenu;
	var element;
	element = findElementById(elementId);
	menu = findElementById(menuId);
	subMenu = findElementById(subMenuId);
	if (element) {
		if ((menu) || (subMenu))
			ShowMenu(menu, subMenu, element);
	}
}


// mouseOut()
function mouseOut(elementId) {
	var element;
	element = findElementById(elementId);
	if (element) {
		HideActiveMenus();
	}
}


// Sets the position of the SubMenu relative to the position of the ParentElement
// subMenu - the object of the SubMenu
// [optional] parentElement - the object of the ParentElement. If defined the subMenu is relativ positioned to the parent element

function ShowMenu(menu, subMenu, parentElement) {
	cancleTimer();
	var sub;
	// if menu is null all menues on stack will be hide.
	while ((activeMenus.length > 0) && (activeMenus[activeMenus.length-1] != menu)) {
		sub = activeMenus.pop();
		HideMenuNow(sub);
	}

	if (subMenu) {
		if (parentElement) {
			//offsetScroll = parent.frames[secondFrame].document.body.scrollTop;
			offsetScroll = document.body.scrollTop;
			if (activeMenus.length == 0) {
				subMenu.style.top = getPos(parentElement, "Top") + parentElement.offsetHeight + offsetTop;//	+ offsetScroll;
				subMenu.style.left = getPos(parentElement, "Left") + offsetLeft;
			} else {
				// only offsetTop for the first level of submenus
				subMenu.style.top = getPos(parentElement, "Top") + offsetTopOfSubMenu ;
				subMenu.style.left = getPos(parentElement, "Left") + offsetLeft + parentElement.offsetWidth * (1 - menuOverlap);
			}
		}
		subMenu.style.visibility = "visible";
		activeMenus.push(subMenu);
	}
}

// Sets the timer for hiding the submenu
function HideActiveMenus(subMenu) {
	cancleTimer();
	hidingTimerId = window.setTimeout("HideAllSubMenus();", hideDelay);
}

function HideAllSubMenus() {
	var menu;
	hidingTimerId = 0;
	while (activeMenus.length > 0) {
		menu = activeMenus.pop();
		HideMenuNow(menu);
	}
}

// hides the subMenu defined by the id. The function is called by the timer.
// subMenuId - the id string of the subMenu
function HideMenuNow(menu) {
	if (menu) {
		menu.style.visibility = "hidden";
	}
}

// if a timer is set, the function clears the timer an sets hidingTimerId = 0
function cancleTimer() {
	if (hidingTimerId != 0) {
		window.clearTimeout(hidingTimerId);
		hidingTimerId = 0;
	}
}


// search for the SubMenuID in the current frame and in the secondFrame
// subMenuID - the ID of the subMenu (String)
// return - the Object or Null
function findElementById (elementId) {
	var element = null;
	if ((elementId) && (elementId.length > 0)) {
		element = getElem("id", elementId, null);
//		if (!element)
//			alert("findElementById - element with ID "+elementId+" not found");
		return element;
	}
	return null;
}

// calculate the left or top position of an element
// element - the element from whom the position is looked up
// property - "left" or "top"
function getPos(element, property) {
	var iPos = 0;
	var last = 0;
	while (element != null) { // && element.id != "header") {
		iPos += element["offset" + property];
		element = element.offsetParent;
	}
	return iPos-last;
}

