//Variables
var sCimpleMenuID = 'Menu';
var sArrowImg = '/gfx/icons/arrow.gif';
var iMainMenuSpace = 3;
var iHideDelay = 1200;
var iMenuWidth = 660;
var iMenuOffset = 12;
var iMenuLinkPadding = 16;

//Object references
var objCimpleMenu;
var bMenuActive = true;
var iHideTimer = 0;
var iSubIndex = 0;
var aVisibleSubs = new Array();

//Functions
function CimpleMenuShow() {
	var aExceptions = new Array();

	//Display sub
	objSub = document.getElementById('Sub' + this.id.replace(/[^0-9]/g,''));
	if (objSub) {
		objSub.style.display = 'block';
		aExceptions[objSub.id] = true;
		aVisibleSubs.push(objSub);
	}

	//Display parents to sub
	objParent = this.parentNode;
	while (objParent && objParent.id != sCimpleMenuID + 'Main') {
		objParent.style.display = 'block';
		aExceptions[objParent.id] = true;
		aVisibleSubs.push(objParent);

		objMenuItem = document.getElementById('Item' + objParent.id.replace(/[^0-9]/g,''));
		objMenuItem.className = 'Active';

		objParent = objMenuItem.parentNode;
	}

	CimpleMenuHide(aExceptions);
}

function CimpleMenuHide(aExceptions) {
	var aNewVisibleSubs = new Array();

	for (i = 0;i < aVisibleSubs.length;i++) {
		if (aVisibleSubs[i].id) {
			if (!aExceptions[aVisibleSubs[i].id]) {
				aVisibleSubs[i].style.display = 'none';
				document.getElementById('Item' + aVisibleSubs[i].id.replace(/[^0-9]/g,'')).className = '';
			} else {
				aNewVisibleSubs.push(aVisibleSubs[i]);
			}
		}
	}

	aVisibleSubs = aNewVisibleSubs;
}

function CimpleMenuMouseOver() {
	bMenuActive = true;
}

function CimpleMenuMouseOut() {
	bMenuActive = false;
	CimpleMenuAutoHide();
}

function CimpleMenuAutoHide() {
	if (bMenuActive) {
		iHideTimer = 0;
		return;
	}

	if (iHideTimer < iHideDelay) {
		iHideTimer += 100;
		setTimeout('CimpleMenuAutoHide()',99);
	} else if (!bMenuActive) {
		iHideTimer = 0;
		CimpleMenuHide(new Array());
	}
}

function CimpleMenuInitSub(objParentLI,iInitLeftOffset,iInitTopOffset) {
	var aSubs = objParentLI.getElementsByTagName('UL');
	if (aSubs.length > 0) {
		var objSub = aSubs[0];

		//Set styles
		objSub.id = 'Sub' + objParentLI.id.replace(/[^0-9]/g,'');
		objSub.style.display = 'block';

		//Move sub to top
		objSub = objCimpleMenu.appendChild(objSub);
		objSub.style.left = iInitLeftOffset + 'px';
		objSub.style.top = iInitTopOffset + 'px';
		objSub.style.zIndex = objParentLI.style.zIndex++;
		objSub.style.width = objParentLI.offsetWidth + 'px';

		//Set actions
		objSub.onmouseover = CimpleMenuMouseOver;
		objSub.onmouseout = CimpleMenuMouseOut;

		//Position menuitems
		var iSubWidth = 0;
		var iItemTop = 0;
		var objMenuItem = objSub.getElementsByTagName('LI')[0];
		while (objMenuItem) {
			if (objMenuItem.tagName == 'LI') {
				objMenuItem.onmouseover = CimpleMenuShow;
				objMenuItem.style.top = iItemTop + 'px';
				objMenuItem.style.zIndex = objSub.style.zIndex;
				objMenuItem.id = 'Item' + iSubIndex;
				
				iSubIndex++;

				if (objMenuItem.offsetWidth > iSubWidth)
					iSubWidth = objMenuItem.offsetWidth;

				iItemTop += objMenuItem.offsetHeight;
			}

			objMenuItem = objMenuItem.nextSibling;
		}

		//Fit submenu
		objSub.style.height = iItemTop + 'px';
		objSub.style.width = (iSubWidth + iMenuLinkPadding) + 'px';

		//Next level sub
		var iRightEdge = iInitLeftOffset + iSubWidth + iMenuLinkPadding;
		var iNextLeft = iInitLeftOffset;
		if (iRightEdge > iMenuWidth) {
			iNextLeft = iInitLeftOffset - iSubWidth - iMenuOffset;
		} else {
			iNextLeft = iInitLeftOffset + iSubWidth;
		}

		var objMenuItem = objSub.getElementsByTagName('LI')[0];
		while (objMenuItem) {
			if (objMenuItem.tagName == 'LI') {
				//Fit menuitems
				objMenuItem.style.width = (iSubWidth + iMenuLinkPadding) + 'px';

				//Fit links
				objLink = objMenuItem.getElementsByTagName('A')[0];
				if (objLink) {
					objLink.style.width = (iSubWidth - 4) + 'px';
				}

				//Call subs
				var aSubs = objMenuItem.getElementsByTagName('UL');
				if (aSubs.length > 0) {
					CimpleMenuInitSub(objMenuItem,iNextLeft,iInitTopOffset + objMenuItem.offsetTop);
				}
			}

			objMenuItem = objMenuItem.nextSibling;
		}

		//Hide
		objSub.style.display = 'none';
	}
}

function CimpleMenuInit() {
	objCimpleMenu = document.getElementById(sCimpleMenuID);
	objCimpleMenu.style.zIndex = 1000;

	//Properties for ground level
	objMainMenu = objCimpleMenu.getElementsByTagName('UL')[0];
	if (objMainMenu) {
		//Set actions
		objMainMenu.onmouseover = CimpleMenuMouseOver;
		objMainMenu.onmouseout = CimpleMenuMouseOut;

		//Set styles
		objMainMenu.style.border = 'none';
		objMainMenu.id = sCimpleMenuID + 'Main';

		//Determine if arrow is displayed
		var aLinks = objMainMenu.getElementsByTagName('A');
		for (i = 0;i < aLinks.length;i++) {
			aSubs = aLinks[i].parentNode.getElementsByTagName('UL');
			if (aSubs.length > 0) {
				aLinks[i].style.backgroundImage = 'url(' + sArrowImg + ')';
			}
		}

		//Draw init menu
		var iCurrentLeft = 0;
		var objMenuItem = objMainMenu.getElementsByTagName('LI')[0];
		while (objMenuItem) {
			if (objMenuItem.tagName == 'LI') {
				objMenuItem.onmouseover = CimpleMenuShow;
				objMenuItem.style.left = iCurrentLeft + 'px';
				objMenuItem.style.zIndex = objMenuItem.parentNode.style.zIndex;
				objMenuItem.style.width = objMenuItem.offsetWidth + 'px';
				objMenuItem.id = 'Item' + iSubIndex;

				iSubIndex++;

				var aSubs = objMenuItem.getElementsByTagName('UL');
				if (aSubs.length > 0) {
					CimpleMenuInitSub(objMenuItem,iCurrentLeft,objMenuItem.offsetHeight);
				}

				iCurrentLeft += objMenuItem.offsetWidth + iMainMenuSpace;
			}

			objMenuItem = objMenuItem.nextSibling;
		}
	}

	objCimpleMenu.style.visibility = 'visible';
}

womAdd('CimpleMenuInit()');
