﻿var menuVisible;
var menuOwner;
document.write("<div id='js_menudiv' style='position:absolute; z-index:1000; visibility:hidden'></div>");

function attachMenu(menu, ownerId) {
	var owner = $(ownerId);
	if (owner.tagName != 'IMG') {
		if (theme != '') {
			owner.innerHTML += "<img src='" + base_path + "/themes/" + theme + "/icon/dot_arrowdown.gif' border='0' alt='' />";
		} else {
			if (is_ie) {
				owner.innerHTML += "<font face='webdings' style='font-size:12px'>6</font>";
			}
		}
	}
	addEvent(owner, 'mouseover', function() { showMenu(menu, owner) } );
}

function showMenu(menu, owner) {
	var point = getOffset(owner);
	if (menuOwner == owner && menuVisible === true) {
		return;
	}
	menuOwner = owner;
	if (typeof menu != "string")  menu = createMenu(menu);
	var container = $('js_menudiv');
	container.style.visibility = 'hidden';
	container.innerHTML = menu;
	addEvent($('js_menutable'), 'mouseout', function(e) { hideMenu(is_ie ? event : e) });
	if (point.x + container.offsetWidth < document.body.clientWidth) {
		container.style.left = parseInt(point.x) + 'px';
	} else {
		container.style.left = (point.x + owner.offsetWidth + (is_ie ? 5 : 3) - container.offsetWidth) + 'px';
	}
	container.style.top = (point.y + owner.offsetHeight + 4) + 'px';
	//if (container.filters && container.filters.length > 3) {
	//	container.filters[2].apply();
	//	container.filters[3].apply();
	//	container.filters[2].play();
	//	container.filters[3].play();
	//}
	container.style.visibility = 'visible';
	container.style.overflow = 'hidden';
	menuVisible = true;
	//openShim(container);
}

function hideMenu(e) {
	if (menuVisible) {
		var container = $('js_menudiv');
		if (typeof e == 'undefined') {
			container.style.visibility = 'hidden';
			menuVisible = false;
			menuOwner = null;
			//closeShim(container);
		} else {
			var scrollOffset = getScrollOffset();
			var mouse = { x : e.clientX + scrollOffset.x, y : e.clientY +scrollOffset.y };
			if ( mouse.x < parseInt(container.style.left) + (is_ie ? 2 : 0) ||
				mouse.x > parseInt(container.style.left) + container.offsetWidth - 2 ||
				mouse.y < parseInt(container.style.top) + (is_ie ? 1 : 0) ||
				mouse.y > parseInt(container.style.top) + container.offsetHeight - 2)
			{
				container.style.visibility = 'hidden';
				menuVisible = false;
				menuOwner = null;
				//closeShim(container);
			}
		}
	}
}

function createMenu(menu) {
	var content = "<table id='js_menutable' cellpadding='0' cellspacing='0' border='0'>";
	for (var i = 0; i < menu.length; i++) {
		if (menu[i][0] == '_line') {
			content += "<tr><td><div class='js_menuline'></div></td></tr>";
		} else {
			if (menu[i].length > 2 && menu[i][2]) {
				content += "<tr><td nowrap='nowrap'><a href='javascript:void(0);' disabled='disabled' style='color:gray'>" + menu[i][0] + "</a></td></tr>";
			} else {
				content += ("<tr><td nowrap='nowrap'><a href='" + menu[i][1] + "'");
				if (menu[i][1].substr(0, 11).toLowerCase() == 'javascript:') {
					content += " onclick='javascript:hideMenu();'";
				}
				content += (">" + menu[i][0] + "</a></td></tr>");
			}
		}
	}
	content += "</table>";
	return content;
}

function openShim(menu) {
	if (menu == null) {
		return;
	}
	var shim = getShim(menu);
	if (shim == null) {
		shim = createMenuShim(menu,getShimId(menu));
	}
	menu.style.zIndex = 100;
	shim.style.width = (menu.offsetWidth == 0 ? menu.renderedWidth : menu.offsetWidth) - 3;
	shim.style.height = (menu.offsetHeight == 0 ? menu.renderedHeight : menu.offsetHeight) - 4;
	shim.style.top = menu.style.top;
	shim.style.left = menu.style.left;
	shim.style.zIndex = menu.style.zIndex - 1;
	shim.style.position = "absolute";
	shim.style.display = "block";
	shim.style.filter = "alpha(opacity=0)";
	shim.style.opacity = "0";
}

function closeShim(menu) {
	if (menu == null) return;
	var shim = getShim(menu);
	if (shim != null) shim.style.display = "none";
}

function createMenuShim(menu)
{
	if (menu == null) return null;
	var shim = document.createElement("<iframe scrolling='no' frameborder='0' style='position:absolute; top:0px; left:0px; display:none'></iframe>");
	shim.name = getShimId(menu);
	shim.id = getShimId(menu);
	if (menu.offsetParent == null || menu.offsetParent.id == "") {
		window.document.body.appendChild(shim);
	} else {
		menu.offsetParent.appendChild(shim);
	}
	return shim;
}
function getShimId(menu)
{
	if (menu.id == null) return "__shim";
	return "__shim" + menu.id;
}

function getShim(menu) {
	return $(getShimId(menu));
}

//--------------

function attachPanelMenu(ownerId) {
	var menu = [
			[ '我的个人信息', 'user_profile.aspx' ],
			[ '我最近参与的主题', 'my_topics.aspx' ],
			[ '我最近发表的帖子', 'my_posts.aspx' ],
			[ '我最近上传的文件', 'my_attachments.aspx' ],
			[ '_line' ],
			[ '发送新信件', 'message_send.aspx' ],
			[ '我收到的信件', 'message_box.aspx?type=inbox' ],
			[ '我发出的信件', 'message_box.aspx?type=outbox' ],
			[ '我的信件拒收设置', 'message_rejectsetting.aspx' ],
			[ '_line' ],
			[ '修改注册信息', 'my_registration.aspx' ],
			[ '修改个性化设置', 'my_personality.aspx' ],
			[ '设置形象图片', 'my_avatar.aspx' ],
			[ '编辑扩展信息', 'my_extension.aspx' ],
			[ '设置密码保护信息', 'my_pwdprotect.aspx' ]
		];
	attachMenu(menu, ownerId);
}

function attachThemeMenu(ownerId, themes) {
	var menu = [];
	var arr = themes.split('|');
	for (var i = 0; i < arr.length; i++) {
		var subArr = arr[i].split(':');
		if (subArr[1].toLowerCase() == theme.toLowerCase() && theme != '') {
			menu[i] = [ subArr[0] + " ←", 'changeview.aspx?theme=' + subArr[1], true ];
		} else {
			menu[i] = [ subArr[0], 'changeview.aspx?theme=' + subArr[1] ];
		}
	}
	attachMenu(menu, ownerId);
}

function attachFastReeditMenu(ownerId, replyId, action, usingTag, enableEmote, advanced, allowWysiwyg) {
	var menu = [
			[ '高级模式', 'deliver.aspx?rid=' + replyId + '&action=' + action ],
			[ '快速编辑内容', 'javascript:fastEdit(' + replyId + ',' + usingTag + ',' + enableEmote + ',' + advanced + ',' + allowWysiwyg + ');' ]
		];
	attachMenu(menu, ownerId);
}