var oOpenMenuTimeout;
var oCloseMenuTimeout;

function fnAddIcons(node) {
	var aMenus = getElementsByClass('layout_MenuItemHasChildren', node, 'span');
	var oImg = document.createElement('img');
	var oPlus, oMinus;
	var intCounter;
	oImg.className = "layout_MenuIcon";
	for (intCounter = 0; intCounter < aMenus.length; intCounter++) {
		if (aMenus[intCounter].className.search(/Expanded/i) != -1) {
			oMinus = oImg.cloneNode(true);
			oMinus.src = "/theme/images/minus_sign.gif";
			aMenus[intCounter].insertBefore(oMinus, aMenus[intCounter].firstChild);
		}
		if (aMenus[intCounter].className.search(/Collapsed/i) != -1) {
			oPlus = oImg.cloneNode(true);
			oPlus.src = "/theme/images/plus_sign.gif";
			YAHOO.util.Event.addListener(oPlus, 'click', function() { fnOpenNow(this.parentNode); } );
			aMenus[intCounter].insertBefore(oPlus, aMenus[intCounter].firstChild);
		}
	}
}

function fnAddExpansions(node) {
	var aMenus = getElementsByClass('layout_MenuItem', node, 'span');
	var intCounter;
	// Add mouseout to all equally.
	YAHOO.util.Event.addListener(aMenus, 'mouseout', fnDelayClose);
	for (intCounter = 0; intCounter < aMenus.length; intCounter++) {
		if ((aMenus[intCounter].className.search(/layout_MenuItemHasChildren/i) != -1) && (aMenus[intCounter].className.search(/Collapsed/i) != -1)) {
			// Add expanding mouseover and click to menus with children that are initially collapsed.
			YAHOO.util.Event.addListener(aMenus[intCounter], 'mouseover', function() { fnDelayOpen(this); });
		} else {
			// All other menus get a function to cancel the delay close timer.
			YAHOO.util.Event.addListener(aMenus[intCounter], 'mouseover', function() { clearTimeout(oCloseMenuTimeout); } );
		}
	}
}

function fnOpenMenu(strMenuID) {
	var intCounter;
	var oBox = $(strMenuID);
	if ((oBox.className) && (oBox.className.search(/Expanded/i) != -1)) { return; }
	var oIcon = getElementsByClass('layout_MenuIcon', oBox, 'img');
	if (oIcon.length == 1) { oIcon = oIcon[0]; } else { oIcon = null; }
	var oExpands = getElementsByClass('layout_ExpandingMenu', oBox, 'span');
	var oMenu;
	for (intCounter = 0; intCounter < oExpands.length; intCounter++) {
		oMenu = oExpands[intCounter];
		if (oMenu.className.search(/Persist/i) == -1) {
			if ((oMenu.filters) && (oMenu.filters[0])) { oMenu.filters[0].Apply(); }
			oMenu.className = oMenu.className.replace(/closed/i,'Open');
			if ((oMenu.filters) && (oMenu.filters[0])) { oMenu.filters[0].Play (); }
			if (oIcon) { oIcon.src = "/theme/images/minus_sign.gif"; }
			if (oBox.className) { oBox.className = oBox.className.replace(/Collapsed/i,'Expanded'); }
		}
	}
}

function fnCloseMenus() {
	var intCounter;
	var oBox;
	var oIcon;
	var oExpands = getElementsByClass('layout_ExpandingMenu', null, 'span');
	var oMenu;
	for (intCounter = 0; intCounter < oExpands.length; intCounter++) {
		oMenu = oExpands[intCounter];
		oBox = oMenu.parentNode;
		oIcon = getElementsByClass('layout_MenuIcon', oBox, 'img');
		if (oIcon.length == 1) { oIcon = oIcon[0]; } else { oIcon = null; }
		if (oMenu.className.search(/Persist/i) == -1) {
			if ((oMenu.filters) && (oMenu.filters[0])) { oMenu.filters[0].Apply(); }
			oMenu.className = oMenu.className.replace(/open/i, 'Closed');
			if ((oMenu.filters) && (oMenu.filters[0])) { oMenu.filters[0].Play (); }
			if (oIcon) { oIcon.src = "/theme/images/plus_sign.gif"; }
			if (oBox.className) { oBox.className = oBox.className.replace(/Expanded/i,'Collapsed'); }
		}
	}
}

function fnOpenNow(oMenu) {
	clearTimeout(oCloseMenuTimeout);
	clearTimeout(oOpenMenuTimeout);
	fnOpenMenu(fnUniqueID(oMenu));
}

function fnDelayOpen(oMenu) {
	clearTimeout(oCloseMenuTimeout);
	oOpenMenuTimeout = setTimeout("fnOpenMenu('" + fnUniqueID(oMenu) + "');", 500);
}

function fnDelayClose() {
	clearTimeout(oOpenMenuTimeout);
	oCloseMenuTimeout = setTimeout("fnCloseMenus();", 1500);
}
