var useMenuButton = false;
var showMenuButtonOnExec = false;
var contextHtml = "";
var contextDiv;
var vpDim;
var vpOff;

function initContextMenu(menuHtml){

	vpDim = document.viewport.getDimensions();
	if(Prototype.Browser.IE){
		/* IEでは上記処理が落ちるため、以下の関数による再取得を実施 */
		vpDim.width = getViewWidth();
		vpDim.height = getViewHeight();
	}
	vpOff = document.viewport.getScrollOffsets();

	contextDiv = new Element('div', {className: 'context_menu', id: 'context_menu', style: 'display: none'});
	$(document.body).insert(contextDiv);

	contextHtml = menuHtml;

	// コンテキストメニューをクリックされた場合に、	// 閉じる。	Event.observe(document, 'click', function(e){
		if(useMenuButton == false){
			if (contextDiv.style.display == "block") {
				contextDiv.style.display = "none";
			}
		}
		else{
			if(showMenuButtonOnExec == true){
				if (contextDiv.style.display == "block") {
					contextDiv.style.display = "none";
				}
				useMenuButton = false;
				showMenuButtonOnExec = false;
			}else{
				showMenuButtonOnExec = true;
			}
		}
	}.bind(this));
}


/**
 * マウスの座標を取得する。 */
var Xpos;
var Ypos;
document.observe('dom:loaded', function (e){
	function mousePosition()
	{
		var myEvent = window.event;
		if(myEvent == undefined){
			myEvent = arguments[0];
		}
		Xpos = Event.pointer(myEvent).x;
		Ypos = Event.pointer(myEvent).y;
	}
	document.onmousemove = mousePosition;
});

/**
 * 右クリックメニューを表示する。
 */
function showContextMenu(useButton){
	useMenuButton = useButton;
	var x = Xpos;
	var y = Ypos;

	if(contextHtml == ""){
		setTimeout( 'showContextMenu('+ useButton +')' , 500 );
		return;
	}

	var outHtml = "";
	outHtml += "<table class=\"context_menu\" width=\"190\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\r\n";
	outHtml += "<tr>\r\n";
	outHtml += "<td style=\"height:10px; background-image:url(./images/menu_list_head.gif);\"></td>";
	outHtml += "</tr>\r\n";

	outHtml += contextHtml;

	// フッタHTMLを生成	outHtml += "<tr>\r\n";
	outHtml += "<td style=\"height:10px; background-image:url(./images/menu_list_footer.gif);\"></td>";
	outHtml += "</tr>\r\n";
	outHtml += "</table>\r\n";

	contextDiv.innerHTML = outHtml;

	var elDim = Element.getDimensions(contextDiv);
	var left = ((x + elDim.width + 25) > vpDim.width
			? (vpDim.width - elDim.width - 25) : x) + 'px';
	var top = ((y - vpOff.top + elDim.height) > vpDim.height && (y - vpOff.top) > elDim.height
			? (y - elDim.height) : y) + 'px';
	contextDiv.style.position = "absolute";

	contextDiv.style.top = top;
	contextDiv.style.left = left;
	contextDiv.style.display = "block";
	contextDiv.style.display = "block";
	contextDiv.style.display = "block";
	contextDiv.style.display = "block";
	contextDiv.style.display = "block";

	return;
}

/**
 * メニューHTMLを作成する。
 */
function createContextMenuHtml(label, callbackFunc, tab){
	//var html = "<a href='#' style='cursor:pointer;' name='contextmenu' title='"+ label +"' onClick='"+ callbackFunc +";'>　"+ label +"</a>";

	var leftPadding = 1;
	if(tab != undefined){
		leftPadding = tab;
	}

	var html = "";
	html = "<tr style=\"cursor:pointer;\">\r\n";
	if(callbackFunc != ""){
		html += "<td style=\"padding:4px 1px 4px "+ leftPadding +"px; background-image:url(./images/menu_list_body.gif); background-repeat:repeat-y; color:#666666;\" onmouseover=\"setColor(this);\" onmouseout=\"resetColor(this);\" onclick=\""+ callbackFunc +"\">　"+ label +"</td>";
	}else{
		html += "<td style=\"padding:4px 1px 4px "+ leftPadding +"px; background-image:url(./images/menu_list_body.gif); background-repeat:repeat-y; color:#666666;\">　"+ label +"</td>";
	}
	html += "</tr>\r\n";

	return html;
}

/*
 * スペーサーHTMLを作成する。
 */
function createContextSeparaterHtml(){
	//var html = "<div class='separator'></div>";
	var html = "<tr style=\"cursor:pointer;\">\r\n";
	html += "<td style=\"padding:1px 0px 1px 0px; background-image:url(./images/menu_list_body.gif); background-repeat:repeat-y;\"><hr style=\"border:1px solid #999;\"></td>";
	html += "</tr>\r\n";
	return html;
}
/*
 * http://d.hatena.ne.jp/yamachan360/20081105
 */
function getViewWidth() {
  return document.documentElement.clientWidth && document.compatMode && document.compatMode=="CSS1Compat" ? document.documentElement.clientWidth : document.body.clientWidth;
}
function getViewHeight() {
  return document.documentElement.clientHeight && document.compatMode && document.compatMode=="CSS1Compat" ?  (navigator.userAgent.indexOf('Opera') > -1 && parseFloat(window.opera.version()) < 9.5 ? document.body.clientHeight : document.documentElement.clientHeight) : document.body.clientHeight;
}

function setColor(obj){
	obj.style.cursor = "pointer";
	obj.style.background = "#0A246A";
	obj.style.color = "#ffffff";
}

function resetColor(obj){
	obj.style.background = "";
	obj.style.backgroundImage = 'url(./images/menu_list_body.gif)';
	obj.style.backgroundRepeat= 'repeat-y';
	obj.style.color = "#666666";
}
