/*
 * menu class
 */


// global variables
var _CMenu_bgcolor = "#ffffff";
var _CMenu_hlcolor = "#ccccff";
var _CMenu_txtcolor = "#005070";
var _CMenu_hltxtcolor = "#003399";
var _CMenu_itembordercolor = "#003399";
var _CMenu_menubordercolor = "#005070";
var _CMenu_itemborder = "1";
var _CMenu_menuborder = "1";
var _CMenu_timerID = null;
var _CMenu_currentmenu = null;

// ブラウザチェック
var isIe = navigator.appName.charAt(0) == "M";
var isMz = !isIe && document.getElementById;

function _CMenu_addItem(caption, action)
{
	this.captions[this.captions.length] = caption;
	this.actions[this.actions.length] = action;
}

function _CMenu_build(caption)
{
	// メニュー内容の作成
	var menustyle = "width: 100%; font-size: 90%; font-family: Tahoma, MS UI Gothic; background-color: "+this.bgcolor+"; border: "+this.itemborder+"px solid "+this.bgcolor+"; color: "+this.txtcolor+";  cursor: hand; cursor: pointer; padding: 4px;";
	var menuHtml = "<table cellpadding='1' cellspacing='2' id='"+this.id+"'\n";
	menuHtml += "style='width:"+this.width+"; visibility: hidden; position: absolute; z-index: 10;";
	menuHtml += "border: "+this.menuborder+"px solid "+this.menubordercolor+"; background-color: "+this.bgcolor+";'>\n";
	for (var i=0; i<this.captions.length; i++)
	{
		menuHtml += "<tr><td nowrap style='"+menustyle+"' onClick='"+this.instancename+".closeMenu(); "+this.actions[i]+";' id='"+this.id+"m"+i+"'>"+this.captions[i]+"</td></tr>\n";
	}

	menuHtml += "</table>\n\n";
	menuHtml += "<div id='"+this.id+"shadow' style='visibility: hidden; position: absolute; z-index: 9; -moz-opacity: 0.2; filter: Alpha(opacity=20); opacity: 0.2; background-color: #000000;'></div>\n";
	document.writeln(menuHtml);
	if (isMz)
	{
		document.getElementById(this.id).onmouseover = this.onMenuEnter;
		document.getElementById(this.id).onmouseout = this.onMenuLeave;
	}
	else
	{
		document.all(this.id).onmouseover = this.onMenuEnter;
		document.all(this.id).onmouseout = this.onMenuLeave;
	}
	for (var i=0; i<this.captions.length; i++)
	{
		if (isMz)
		{
			document.getElementById(this.id+"m"+i).onmouseover = this.highlight;
			document.getElementById(this.id+"m"+i).onmouseout = this.unhighlight;
		}
		else
		{
			document.all(this.id+"m"+i).onmouseover = this.highlight;
			document.all(this.id+"m"+i).onmouseout = this.unhighlight;
		}
	}
}


function _CMenu_showMenu(e)
{
	if (_CMenu_timerID != null)
	{
		clearTimeout(_CMenu_timerID);
		_CMenu_timerID = null;
	}
	// 現在表示中のメニューの場合はメニューを閉じる
	if (_CMenu_currentmenu == this)
	{
		this.closeMenu();
	}
	else if (_CMenu_currentmenu != null)
	{
		_CMenu_currentmenu.closeMenu();
	}
	var elem;
	var menuelem;
	var shadowelem;
	if (isMz)
	{
		elem = e.target;
		menuelem = document.getElementById(this.id);
		shadowelem = document.getElementById(this.id+"shadow");
	}
	else if (isIe)
	{
		elem = window.event.srcElement;
		menuelem = document.all(this.id);
		shadowelem = document.all(this.id+"shadow");
	}

        var ol = this.getAncestorOffsetLeft(elem);
        var ew = menuelem.offsetWidth;
	var x=0, y=0;
	// 下表示
	if (this.position == 0)
	{
	        switch (this.align) {
        	case 0: // 左揃え
			x = ol;
          	  break;
        	case 1: // 中央揃え
			x = ol - (ew - elem.offsetWidth) / 2;
	          break;
	        case 2: // 右揃え
			x = ol - ew + elem.offsetWidth;
	          break;
	        }
		y = this.getAncestorOffsetTop(elem) + elem.offsetHeight;
	}
	// 横表示
	else
	{
	        switch (this.align) {
        	case 0: // 左横
			x = ol - elem.offsetWidth;
          	  break;
        	case 1: // 中央横
			x = ol - (ew - elem.offsetWidth) / 2;
	          break;
	        case 2: // 右横
			x = ol + elem.offsetWidth;
	          break;
	        }
		y = this.getAncestorOffsetTop(elem);
	}
	menuelem.style.top = y;
	menuelem.style.left = x;
	menuelem.style.visibility = "visible";
	shadowelem.style.top = y + 8;
	shadowelem.style.left = x + 8;
	shadowelem.style.width = menuelem.offsetWidth;
	shadowelem.style.height = menuelem.offsetHeight;
	shadowelem.style.visibility = "visible";
	_CMenu_currentmenu = this;
	this.onMenuEnter();
}

function _CMenu_onMenuEnter()
{
	if (_CMenu_timerID != null)
	{
		clearTimeout(_CMenu_timerID);
		_CMenu_timerID = null;
	}
}

function _CMenu_onMenuLeave()
{
	if (_CMenu_currentmenu == null) return;
	_CMenu_timerID = setTimeout(_CMenu_currentmenu.instancename+".closeMenu()", 600);
}

function _CMenu_closeMenu()
{
	if (_CMenu_currentmenu != null)
	{
		var menuelem;
		var shadowelem;
		if (isMz)
		{
			menuelem = document.getElementById(_CMenu_currentmenu.id);
			shadowelem = document.getElementById(_CMenu_currentmenu.id+"shadow");
		}
		else if (isIe)
		{
			menuelem = document.all(_CMenu_currentmenu.id);
			shadowelem = document.all(_CMenu_currentmenu.id+"shadow");
		}
		menuelem.style.visibility = "hidden";
		shadowelem.style.visibility = "hidden";
		_CMenu_currentmenu = null;
		_CMenu_timerID = null;
	}
}

function _CMenu_highlight(e)
{
	if (isMz)
		var elem = e.target;
	else if (isIe)
		var elem = window.event.srcElement;
	elem.style.width = "100%";
	elem.style.cursor = "hand";
//	elem.style.backgroundColor = this.hlcolor;
	elem.style.backgroundColor = _CMenu_hlcolor;
//	elem.style.borderColor = this.itembordercolor;
	elem.style.borderColor = _CMenu_itembordercolor;
	elem.style.color = this.hltxtcolor;
}

function _CMenu_unhighlight(e)
{
	if (isMz)
		var elem = e.target;
	else if (isIe)
		var elem = window.event.srcElement;
	else
		var elem = e;
	elem.style.width = "100%";
	elem.style.cursor = "hand";
//	elem.style.backgroundColor = this.bgcolor;
	elem.style.backgroundColor = _CMenu_bgcolor;
//	elem.style.borderColor = this.bgcolor;
	elem.style.borderColor = _CMenu_bgcolor;
	elem.style.color = this.txtcolor;
}

function _CMenu_getAncestorOffsetTop(elem)
{
	var parent = elem.offsetParent;
	if (parent.tagName == "BODY")
		return elem.offsetTop;
	else
		return _CMenu_getAncestorOffsetTop(parent) + elem.offsetTop;
}

function _CMenu_getAncestorOffsetLeft(elem)
{
	var parent = elem.offsetParent;
	if (parent.tagName == "BODY")
		return elem.offsetLeft;
	else
		return _CMenu_getAncestorOffsetLeft(parent) + elem.offsetLeft;
}

function _CMenu_clearTimeout()
{
	if (_CMenu_timerID != null)
	{
		clearTimeout(_CMenu_timerID);
		_CMenu_timerID = null;
	}
}

// constructor
function CMenu(id, align, position, width, bgcolor, hlcolor, itembordercolor, itemborder, menubordercolor, menuborder)
{
	// variables
	this.instancename = id;
	this.id = id+"id";
	this.captions = new Array();
	this.actions = new Array();
	this.width = width || "150";
        this.align = align || 0;
	this.position = position || 0;

	this.bgcolor = bgcolor || _CMenu_bgcolor;
	this.hlcolor = hlcolor || _CMenu_hlcolor;
	this.itembordercolor = itembordercolor || _CMenu_itembordercolor;
	this.itemborder = itemborder || _CMenu_itemborder;
	this.menubordercolor = menubordercolor || _CMenu_menubordercolor;
	this.menuborder = menuborder || _CMenu_menuborder;
	this.hltxtcolor = _CMenu_hltxtcolor;
}
// methods
CMenu.prototype.addItem = _CMenu_addItem;
CMenu.prototype.build = _CMenu_build;
CMenu.prototype.showMenu = _CMenu_showMenu;
CMenu.prototype.onMenuEnter = _CMenu_onMenuEnter;
CMenu.prototype.onMenuLeave = _CMenu_onMenuLeave;
CMenu.prototype.closeMenu = _CMenu_closeMenu;
CMenu.prototype.highlight = _CMenu_highlight;
CMenu.prototype.unhighlight = _CMenu_unhighlight;
CMenu.prototype.getAncestorOffsetTop = _CMenu_getAncestorOffsetTop;
CMenu.prototype.getAncestorOffsetLeft = _CMenu_getAncestorOffsetLeft;
CMenu.prototype.clearTimeout = _CMenu_clearTimeout;
