var menuGecko=(navigator.userAgent.indexOf ("Gecko")!=-1);
var menuOpera=(navigator.userAgent.indexOf ("Opera")!=-1);
var menuIE=(navigator.userAgent.indexOf ("MSIE")!=-1 && !menuGecko && !menuOpera);

var menuMaxDepth=2;

var menuOverFlag=false;
var menuToggledDivs=new Array (menuMaxDepth);

function menuMouseOver (id, depth)
{
	menuOverFlag=true;
	if (!id)
		return;
	for (var i=depth;i<menuToggledDivs.length;i++)
		if (menuToggledDivs[i])
		{
			menuShowElements (i);
			menuToggledDivs[i].style.display="none";
			menuToggledDivs[i]=null;
		}
	menuToggledDivs[depth]=document.getElementById ("sub"+id);
	if (menuToggledDivs[depth])
	{
		menuToggledDivs[depth].style.display="block";
		var divToCorrect=menuToggledDivs[depth].parentNode;
		if (divToCorrect && divToCorrect.tagName=="DIV")
		{
			divToCorrect.style.top=null;
			var maxtop=document.body.scrollTop+document.body.clientHeight-menuToggledDivs[depth].offsetHeight;
			if (menuGetOffsetTop (divToCorrect)>maxtop)
			{
				maxtop-=(menuGetOffsetTop (divToCorrect)-divToCorrect.offsetTop);
				if (maxtop<0)
					maxtop=0;
				divToCorrect.style.top=maxtop;
			}
		}
		menuHideElements (depth, menuToggledDivs[depth]);
		menuToggledDivs[depth].style.display="block";
	}
}

var menuTimerID=-1;
function menuMouseOut ()
{
	menuOverFlag=false;
	if (menuTimerID!=-1)
		clearTimeout (menuTimerID);
	menuTimerID=setTimeout (menuCheckOverFlag, 2000);
}

function menuCheckOverFlag ()
{
	if (!menuOverFlag)
		menuHideAll ();
}

function menuHideAll ()
{
	var divs;
	divs=document.getElementsByTagName ("DIV");
	for (i=0;i<divs.length;i++)
		if (divs[i].id.slice (0, 3)=="sub")
			divs[i].style.display="none";
	for (var i=0;i<menuIFrames.length;i++)
		menuShowElements (i);
}
document.onclick=menuHideAll;

var menuIFrames=new Array (menuMaxDepth);

function menuHideElements (index, div)
{
	if (!menuIE) return;
	var menuIFrame=menuIFrames[index];
	if (!menuIFrame)
	{
		menuIFrame=document.createElement ("iframe");
		menuIFrame.style.position="absolute";
		menuIFrame.style.display="none";
		menuIFrame.style.zIndex="32766";
		menuIFrame.scrolling="no";
		menuIFrame.frameBorder="0";
		document.body.appendChild (menuIFrame);
		menuIFrames[index]=menuIFrame;
	}
	menuIFrame.style.width=div.offsetWidth;
	menuIFrame.style.height=div.offsetHeight;
	menuIFrame.style.left=menuGetOffsetLeft (div);
	menuIFrame.style.top=menuGetOffsetTop (div);
	menuIFrame.style.display="block";
}

function menuShowElements (index)
{
	if (!menuIE) return;
	if (menuIFrames[index])
		menuIFrames[index].style.display="none";
}

function menuGetOffsetLeft (tag)
{
	var tagLeft=tag.offsetLeft;
	var tagParent=tag.offsetParent;
	while (tagParent.tagName.toUpperCase ()!="BODY")
	{
		tagLeft+=tagParent.offsetLeft;
		if (tagParent.scrollLeft)
			tagLeft-=tagParent.scrollLeft;
		tagParent=tagParent.offsetParent;
	}
	return tagLeft;
}

function menuGetOffsetTop (tag)
{
	var tagTop=tag.offsetTop;
	var tagParent=tag.offsetParent;
	while (tagParent.tagName.toUpperCase ()!="BODY")
	{
		tagTop+=tagParent.offsetTop;
		if (tagParent.scrollTop)
			tagTop-=tagParent.scrollTop;
		tagParent=tagParent.offsetParent;
	}
	return tagTop;
}
