var buttonPOSArray = new Array();
var buttonPOS;
var hideTimeOut;
var currentMenu;
var level;

var hideMenusArray = new Array();
var menusArray = new Array();

function countInstances(string, word) {
	var substrings = string.split(word);
	return substrings.length - 1;
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}


function showMenu(button,state,menu,lvl,prefix){
	if(button.tagName == "DIV"){
		button.style.background = "url('images/background_acct_item_menu_selected.gif') left no-repeat";
	}
	if(state == "show"){
		level = lvl;
		currentMenu = menu;
		itemPrefix = prefix;
		hideAll(prefix);
		clearTimeout(hideTimeOut);
		
		//button.style.background='url(../images/background_acct_item_menu_selected.gif) left no-repeat';
		if(menu != ""){
			itemsMenu = document.getElementById(menu);
			if(button.tagName == "DIV"){
				buttonPOS = findPos(button.childNodes[0]).toString();
				buttonPOSArray = buttonPOS.split(",");
				xPos = parseInt(buttonPOSArray[0]) + parseInt(button.parentNode.clientWidth);
				xPos = xPos.toString() + "px";
				yPos = parseInt(buttonPOSArray[1]);
				yPos = yPos.toString() + "px";
			}else{
				buttonPOS = findPos(button.childNodes[0]).toString();
				buttonPOSArray = buttonPOS.split(",");
				xPos = parseInt(buttonPOSArray[0])+ parseInt(button.childNodes[0].width);
				xPos = xPos.toString() + "px";
				yPos = parseInt(buttonPOSArray[1]);
				yPos = yPos.toString() + "px";
			}	
			itemsMenu.style.top = yPos;
			itemsMenu.style.left = xPos;
			itemsMenu.style.display = "block";
			
		}
	}
	if(state == "hold"){
		clearTimeout(hideTimeOut);
		level = lvl;
		hideAll(prefix);
	}
	if(state == "hide"){
		hideTimeOut = setTimeout("hideAll()",350);
		if(button.tagName == "DIV"){
			button.style.background = "url('images/background_acct_item_menu.gif') left no-repeat";
		}
	}
}

function hideAll(prefix){
	for(i=0;i<menusArray.length;i++){
		hideMenusArray = menusArray[i].split('_');
		if(countInstances(hideMenusArray[0],prefix)){
			if(parseInt(hideMenusArray[1]) >= level){
				document.getElementById(hideMenusArray[0]).style.display = "none";
			}
		}else{
			document.getElementById(hideMenusArray[0]).style.display = "none";
		}
	}
}
