function getRef(id)
{
	return document.getElementById(id);
}


function getSty(id) {
  return (getRef(id).style);
}


function popOver(menuNum, itemNum)
{
	clearTimeout(popTimer);
	hideAllBut(menuNum);   
	litNow = getTree(menuNum, itemNum);
	changeCol(litNow, true);   //isOver = true
	targetNum = menu[menuNum][itemNum].target;

	if (targetNum > 0)  //wenn untermenu
	{

		thisX = parseInt(menu[menuNum][0].ref.marginLeft) + parseInt(menu[menuNum][itemNum].ref.marginLeft);
//thisX = 250;
		thisY = parseInt(menu[menuNum][0].ref.top) + parseInt(menu[menuNum][itemNum].ref.top);

//			alert(thisY);
		with (menu[targetNum][0].ref)
		{
	//		alert(targetNum);
		  marginLeft = parseInt(thisX + menu[targetNum][0].x)+'px';
		  top = parseInt(thisY + menu[targetNum][0].y)+'px';

		  visibility = 'visible';
		}
	}
}

function popOut(menuNum, itemNum) {
	if ((menuNum == 0) && !menu[menuNum][itemNum].target)
	{ hideAllBut(0); }   
	else
	{ popTimer = setTimeout('hideAllBut(0)', 500);}
}


function getTree(menuNum, itemNum)
{
	itemArray = new Array(menu.length);
	
	while(1) {
		itemArray[menuNum] = itemNum;
		// If we've reached the top of the hierarchy, return.
		if (menuNum == 0) { return itemArray; }
		itemNum = menu[menuNum][0].parentItem;
		menuNum = menu[menuNum][0].parentMenu;
	}

}


function changeCol(changeArray, isOver)
{

	for (menuCount = 0; menuCount < changeArray.length; menuCount++)
	{
		if (changeArray[menuCount])
		{
			newCol = isOver ? menu[menuCount][changeArray[menuCount]].marcoover : menu[menuCount][changeArray[menuCount]].marcoback;
			with (menu[menuCount][changeArray[menuCount]].ref)
			{
				backgroundColor = newCol; 
			}
		}
	}
}


function hideAllBut(menuNum)
{
	var keepMenus = getTree(menuNum, 1);
	
	for (count = 0; count < menu.length; count++)
	{
		if (!keepMenus[count])
		  menu[count][0].ref.visibility = 'hidden';
		  changeCol(litNow, false);
	}
}



function Menu(isVert, popInd, x, y, width, borderClass, textClass)
{
	this.isVert = isVert;
	this.popInd = popInd
	this.x = x;
	this.y = y;
	this.width = width;
	this.borderClass = borderClass;
	this.textClass = textClass;
	this.parentMenu = null;
	this.parentItem = null;
	this.ref = null;
}


function Item(text, href, frame, length, spacing, target, marcoover, marcoback)
{
	this.text = "&nbsp; "+text;
	this.href = href;
	this.frame = frame;
	this.length = length;
	this.spacing = spacing;
	this.target = target;
	this.marcoover = marcoover;
	this.marcoback = marcoback;
	this.ref = null;
}



function writeMenus()
{

	
	for (currMenu = 0; currMenu < menu.length; currMenu++) with (menu[currMenu][0])
	{
	
		var str = '', itemX = 0, itemY = 0;
	
		for (currItem = 1; currItem < menu[currMenu].length; currItem++) with (menu[currMenu][currItem])
		{
			var itemID = 'menu' + currMenu + 'item' + currItem;
	
			var w = (isVert ? width : length);
			var h = (isVert ? length : width);
	

				
			str += '<div id="' + itemID + '" style="position: absolute; margin-left: ' + itemX + 'px; top: ' + itemY + 'px; width: ' + w + 'px; height:'
	  + h + 'px; visibility: inherit; ';
	  //test: if (backCol) { str += 'background: ' + backCol + '; '; }
			if (marcoback) { str += 'background: ' + marcoback + '; '; }
			str += '" ';

	
		
			
			if (borderClass) { str += 'class="' + borderClass + '" '; }
			
			str += 'onMouseOver="popOver(' + currMenu + ',' + currItem + ')" onMouseOut="popOut(' + currMenu + ',' + currItem + ')">';
			
			str += '<table width="' + (w - 8) + '" border="0" cellspacing="0" cellpadding="0"><tr><td align="left" height="' + (h - 7) + '">' + '<a class="' + textClass + '" href="' + href + '"' + (frame ? ' target="' + frame + '">' : '>') + text + '</a></td>';
			
			if (target > 0)
			{
				menu[target][0].parentMenu = currMenu;
				menu[target][0].parentItem = currItem;
				
				if (popInd) 
				{
					str += '<td class="' + textClass + '" align="right">' + popInd + '</td>';
				}
			}
			str += '</tr></table></div>';

			if (isVert) { itemY += length + spacing; }
			else { itemX += length + spacing; }

		}

//alert(str);


		var newDiv = document.createElement('div');
		document.getElementsByTagName('body').item(0).appendChild(newDiv);
		newDiv.innerHTML = str;
		ref = newDiv.style;
		ref.position = 'absolute';
		ref.visibility = 'hidden';

		
		
		for (currItem = 1; currItem < menu[currMenu].length; currItem++)
		{
			itemName = 'menu' + currMenu + 'item' + currItem;
			menu[currMenu][currItem].ref = getSty(itemName);
			
		}
	}
	
	// place main menu
	with(menu[0][0])
	{
//		alert(x+'px');
//		alert(y);
		ref.marginLeft = x+'px';
		ref.top = y+'px';
		ref.visibility = 'visible';
	}


}


