var popup_menu = new Array();

function MenuDoPopup(elem, menu_name){
	if(popup_menu[menu_name]){ return; }
	var menu = document.getElementById(menu_name);
	if(menu){
		if(elem){
			var pos = GetPosition(elem);
			var x = pos[0];
			var y = pos[1] + elem.offsetHeight + 2;
			pos = AdjustPosition(x, y, menu)
			with(menu.style){
				zIndex  = 102;
				left    = pos[0] + 'px';
				top     = pos[1] + 'px';
				display = '';
			}
			if(menu.length > 20){
				menu.setAttribute('size', 20);
			} else if(menu.length < 2){
				menu.setAttribute('size', 2);
			} else {
				menu.setAttribute('size', menu.length);
			}
			menu.selectedIndex = -1;
		} else {
		}
		popup_menu[menu_name] = true;
	}
}

function MenuClosePopup(menu_name){
	popup_menu[menu_name] = false;
	setTimeout("ClosePopupMenu('" + menu_name + "')", 100);
}

function ClosePopupMenu(menu_name){
	if(!popup_menu[menu_name]){
		var menu = document.getElementById(menu_name);
		if(menu){
			menu.selectedIndex = -1;
			menu.style.display = 'none';
		}
	}
}

function GetPosition(elem){
	var p,x,y;
	if(elem){
		x = elem.offsetLeft;
		y = elem.offsetTop;
		p = elem.offsetParent;
		while(p != null) {
			x += p.offsetLeft;
			y += p.offsetTop;		
			p  = p.offsetParent;
		}
		// Mac IE Adjustment
		if (document.all && navigator.appVersion.indexOf("Mac") >= 0){
			y += parseInt(document.body.topMargin);
			x += parseInt(document.body.leftMargin);
		}
	} else {
		x = 0;
		y = 0;
	}
	var pos = new Array(x, y);
	return pos;
}

function AdjustPosition(posX, posY, layer){
	layer.style.display = 'block'; // 事前に表示しないとサイズが計算されない
	var info = mv_GetPageSize();
	var pageW   = info[0]; var pageH   = info[1]; // Page Size
	var windowW = info[2]; var windowH = info[3]; // Window Size
	var scrollW = info[4]; var scrollH = info[5]; // Scroll Size
	var offsetW = layer.offsetWidth;
	var offsetH = layer.offsetHeight; // Layer Size
	var minX = scrollW + windowW - offsetW;
	if(!document.all){ if(pageH > windowH){ minX -=20; } }
	if(minX < scrollW){ minX = scrollW};
	if(posX > minX){ posX = minX; }
	var minY = scrollH + windowH - offsetH;
	if(!document.all){ if(pageW > windowW){ minY -=20; } }
	if(minY < scrollH){ minY = scrollH};
	if(posY > minY){ posY = minY; }
	layer.style.display = 'none';
	var pos = new Array(posX, posY);
	return pos;
}

function mv_GetPageSize(){
    var pageWidth, pageHeight;
    if (window.innerHeight && window.scrollMaxY) {  
        pageWidth = window.innerWidth + window.scrollMaxX;
        pageHeight = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
        pageWidth = document.body.scrollWidth;
        pageHeight = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        pageWidth = document.body.offsetWidth;
        pageHeight = document.body.offsetHeight;
    }
    var windowWidth, windowHeight;
    if (self.innerHeight) { // all except Explorer
        windowWidth = self.innerWidth;
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth  = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowWidth  = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }   
    var scrollX, scrollY;
    scrollX = document.documentElement.scrollLeft || document.body.scrollLeft;
    scrollY = document.documentElement.scrollTop || document.body.scrollTop;
    if(pageWidth  < windowWidth) { pageWidth  = windowWidth;  }
    if(pageHeight < windowHeight){ pageHeight = windowHeight; }
    arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight, scrollX, scrollY) 
    return arrayPageSize;
}
