function findPosLeft(obj) {
	var curleft = 0;
	if(document.getElementById(obj).offsetParent) {
		curleft = document.getElementById(obj).offsetLeft
		while (obj = document.getElementById(obj).offsetParent.value) {
			curleft += document.getElementById(obj).offsetLeft
		}
	}
	//alert(curleft);
	return curleft;
}
function findPosTop(obj) {
	var curtop = 0;
	if(document.getElementById(obj).offsetParent) {
		curtop = document.getElementById(obj).offsetTop
		while (obj = document.getElementById(obj).offsetParent.value) {
			curtop += document.getElementById(obj).offsetTop
		}
	}
	//alert(curtop);
	return curtop;
}
function findElementWidth(obj) {
	var curwidth = 0;
	curwidth = document.getElementById(obj).offsetWidth;
	//alert(curwidth);
	return curwidth;
}
function findElementHeight(obj) {
	var curheight = 0;
	curheight = document.getElementById(obj).offsetHeight;
	//alert(curheight);
	return curheight;
}

var hldepth = 1;		//Fixed to compare with the first level menu
var hlimageId = "";		//Image that will not be affected by by mouseover or mouseout
var hlTopImageSrc = "";		//Image source of the Item at the top of the hierarchy of the Non mouseover item
var topImageSrc = "";		//Image source of the Item at the top of the hierarchy of the mouseover item
var topParentId = "";		//Item at the top of the hierarchy of the mouseover item

var inmenu = false;
var olmenu = "";
var lastmenu = "";

function Erase()
{
	if (!inmenu)
		clearDivs();
}

function Timeout()
{
	inmenu = false;
	setTimeout('Erase()',500);
}

function resetImage(topParent, mev, depth, imageId, replacementImage, moImage)
{
	var id = imageId.substring(3);
	if (mev=="mout")
	{
		topParentId = "img" + topParent;
		topImageSrc = moImage;
		hlTopImageSrc = replacementImage;
	}
	if (imageId != hlimageId)
		if (document.getElementById(imageId))
			document.getElementById(imageId).setAttribute("src",replacementImage);
	Timeout();
}

function changeImage(imageId, replacementImage, currImage)
{
	if (topParentId != imageId && topParentId != hlimageId)
		if (document.getElementById(topParentId))
		document.getElementById(topParentId).setAttribute("src",hlTopImageSrc);

	if (imageId != hlimageId)
		if (document.getElementById(imageId))
			document.getElementById(imageId).setAttribute("src",replacementImage);
	inmenu = true;
}

var prevDiv = "div2";
var prevDepth = 0;
var stack = [];
var depths = [];
function showNextDiv(curElem,nextDiv,depth)
{
	if(document.getElementById(topParentId))
		if (hldepth < depth)
			document.getElementById(topParentId).setAttribute("src",topImageSrc);
	inmenu = true;

	var left = findPosLeft(curElem);
	var top = findPosTop(curElem);
	var elementWidth = findElementWidth(curElem);
	var elementHeight = findElementHeight(curElem);
	
	//When we move up or down between divs depth does not change
	while(prevDepth > depth)
	{
		//Move is inward
		x = stack.pop();
		d = depths.pop();
		if(document.getElementById(x))
		{
			var so = document.getElementById(x).style;
			so.display = "none";
		}
		prevDepth = prevDepth - 1;
	}
			
	if(document.getElementById(nextDiv))
	{
		var styleObj = document.getElementById(nextDiv).style;
		styleObj.left = left + elementWidth;
		styleObj.top = top;
		styleObj.display="block";
		
		stack.push(nextDiv);
		depths.push(depth + 1);
		
		prevDepth = depth + 1;
	}
}

function clearDivs()
{
	if (topParentId != hlimageId)
	{
		if (document.getElementById(topParentId))
			document.getElementById(topParentId).setAttribute("src",hlTopImageSrc);
	}

	var ctr = stack.length;
	while(ctr >= 1)
	{
		x = stack.pop();
		var so = document.getElementById(x).style;
		so.display = "none";
		ctr = ctr - 1;
	}
	
	//Stack values cleared, now reset prevDepth value
	prevDepth = 1;
}

if(!document.all)
{
	document.captureEvents(Event.CLICK);
	document.onclick = clearDivs;
}
else
{
	document.onclick = clearDivs;
}