function UMenuSetTextColor(targObj, color) {
	var tableNode = targObj.firstChild;
	if ( typeof(tableNode.tagName) == "string" && tableNode.tagName == "TABLE" ) {
		var check = ( color == "" );
		targObj = getChNode(tableNode,0,0);
		setDivShown(getChNode(tableNode,0,1).childNodes(0), check);
		setDivShown(getChNode(tableNode,0,1).childNodes(1), !check);
	}
	targObj.style.color = color;
}
function isAlNum(strA) {
	return /^[0-9a-z]*$/gi.test(strA);
}
function makeIrcUserID(strId) {
	return makeUserId(strId);
}
function strIrcIdEq(strIdA, strIdB) {
	return ( stricmp(makeIrcUserID(strIdA), makeIrcUserID(strIdB)) == 0 )
}
function UmenuItemMOver() {
	this.style.background='#31458C';
	UMenuSetTextColor(this, '#F1F1F1');
}
function UmenuItemMOut() {
	this.style.background='';
	UMenuSetTextColor(this, '');
}

function AMenuLvChange() {
	if ( typeof(userContextMenuObj.userContextMenu) != "object" )
		return;
	if ( !userContextMenuObj.aRLvContextMenu )
		userContextMenuObj.aRLvContextMenu = initARoomLvMenu();
	if ( !bShowaRLvContextMenu() )
		hideSubMenu();
	var top = userContextMenuObj.userContextMenu.style.pixelTop+this.offsetTop+1;
	var left = userContextMenuObj.userContextMenu.style.pixelLeft+userContextMenuObj.userContextMenu.clientWidth-15;
	moveContextMenu(userContextMenuObj.aRLvContextMenu, left, top);
}
function AMenuItemMoveZAvata(bUp) {
	ciItemMoveBeforeAvata(bUp);
}
function AMenuItemMoveZ(bUp, bAll) {
	ciItemMoveZ(bUp, bAll);
}
function makeTagNode(tagName, className) {
	var retTagNode = document.createElement(tagName)
	if ( className ) 
		retTagNode.className = className;
	return retTagNode;
}
function makeImgNode(src, width, height, hspace, vspace) {
	var node = makeTagNode("IMG"), arrImgAttr = new Array("src","width","height","hspace", "vSpace"), i;
	for(i = 0; i < arguments.length ; i++ ) 
		node[arrImgAttr[i]] = arguments[i];
	return node;
}
function makeTableNode() {
	var tBodyNode = makeTagNode("TBODY"), trNode, i, j;
	for ( i = 0 ; i < arguments.length ; i++ ) 
		for(trNode = tBodyNode.appendChild(makeTagNode("TR")), j = 0 ; j < arguments[i]; j++ )
			trNode.appendChild(makeTagNode("TD"));
	trNode = makeTagNode("TABLE");
	trNode.cellSpacing = 0;
	trNode.cellPadding = 0;
	trNode.border = 0;
	trNode.appendChild(tBodyNode);
	return trNode;
}
function getChNode(tbnode, rows, cols) {
	var node; 
	if  ( typeof( node = tbnode.firstChild) == "object" 
				&& !isNaN(rows)
				&& typeof(node = node.childNodes[rows]) == "object"
				&& !isNaN(cols)
				&& typeof(node = node.childNodes[cols]) == "object" );
	return node;
}
function UMenuSetText(targObj, strText, bSpecial) {
	targObj.innerText = strText;
	targObj.className = "uMenu"+( bSpecial ? "Special" : "Normal" );
}
function makeSubMenuBtn(strText) {
	var ret = makeTableNode(2), aNode;
	ret.width = "100%";
	ret.style.tableLayout = "fixed"
	aNode= getChNode(ret,0,0);
	UMenuSetText(aNode, strText, false);
	aNode.className = "uMenuNormal";
	aNode= getChNode(ret,0,1);
	aNode.width = 20;
	aNode.appendChild(makeImgNode(UMenuBaseUrl+"bul.gif",4,7));
	setDivShown(aNode.appendChild(makeImgNode(UMenuBaseUrl+"bul2.gif",4,7)),false);
	return ret;
}
function ciDisableEvent() {
	if ( ISObject(event) && event ) {
		event.cancelBubble = true;
		event.returnValue = 0;
	}
	return false;
}
function addMenuItem(tableObj, menuFlag, name, bOnlySet, bSpecial) {
	var tdObj = makeTagNode("TD"), trObj;
	tdObj.style.paddingTop = "3px";
	tdObj.onmouseover = UmenuItemMOver;
	tdObj.onmouseout = UmenuItemMOut;
	trObj = tableObj.firstChild.appendChild(makeTagNode("TR"));
	for ( i = 5 ; i < arguments.length ;) 
		trObj[arguments[i++]] = arguments[i++];
	if ( !trObj.onmousedown )
		tdObj.onmousedown = ciDisableEvent;
	tdObj.style.cursor = ( trObj.onmousedown ? "hand" : "default");
	trObj.appendChild(tdObj);
	trObj.menuFlag = menuFlag;
	trObj.bOnlySet = bOnlySet;
	if ( menuFlag ) {
		setDivShown(trObj, false);
		tdObj.style.paddingLeft = "10px"
		if ( !trObj.onmouseover ) {
			UMenuSetText(tdObj, name, bSpecial);
			trObj.onmouseover = hideSubMenu;
		}
		else 
			tdObj.appendChild(makeSubMenuBtn(name));
	}
	else {
		//tdObj.align = "center";
		tdObj.style.paddingLeft = "5px";
		UMenuSetText(tdObj, name, bSpecial);
	}
		
}
function makeContextMenuTable(bType, width) {
	var ret = makeTableNode(3,3,3), aNode, rowIdx = 0;
	document.styleSheets[0].addRule(".uMenuNormal", "color:#1E2E69");
	document.styleSheets[0].addRule(".uMenuSpecial", "color:#0033CC");
	setDivShown(ret, false);
	ret.style.position = "absolute";
	ret.style.tableLayout="fixed";
	//ret.style.filter="blendTrans(duration=0.3)";
	ret.width = ret.style.pixelWidth = width;
	getChNode(ret,rowIdx).height = 3
	aNode = getChNode(ret,rowIdx,0)
	aNode.width = 3;
	aNode.appendChild(makeImgNode(UMenuBaseUrl+"bx1_top_l.gif",3,3));
	aNode = getChNode(ret,rowIdx,1);
	aNode.bgColor = "#73AABD";
	aNode.style.borderBottom = aNode.style.borderTop = "1px #EFEFEF solid";
	aNode.innerText = " ";
	aNode = getChNode(ret,rowIdx,2)
	aNode.width = 3;
	aNode.appendChild(makeImgNode(UMenuBaseUrl+"bx1_top_r.gif",3,3));
	aNode = getChNode(ret,++rowIdx,0);
	aNode.bgColor = "#73AABD";
	aNode.style.borderLeft = aNode.style.borderRight = "1px #EFEFEF solid";
	aNode.innerText = " ";
	aNode = getChNode(ret,rowIdx,1);
	aNode.bgColor = "#EFEFEF";
	aNode.style.padding = "4 0 4 0";
	aNode = aNode.appendChild(makeTableNode())
	aNode.width="100%";
	aNode.style.tableLayout ="fixed";
	aNode.bgColor = "#EFEFEF";
	aNode = getChNode(ret,rowIdx,2);
	aNode.bgColor = "#73AABD";
	aNode.style.borderLeft = aNode.style.borderRight = "1px #EFEFEF solid";
	aNode.innerText = " ";
	getChNode(ret,++rowIdx).height = (5-bType);
	aNode = getChNode(ret,rowIdx,0)
	aNode.appendChild(makeImgNode(UMenuBaseUrl+"bx"+bType+"_dw_l.gif",3,(5-bType)));
	aNode = getChNode(ret,rowIdx,1);
	aNode.bgColor = "#CECBCE"
	aNode.vAlign = "top"
	aNode=aNode.appendChild(makeTagNode("DIV"));
	aNode.style.width = "100%";
	aNode.style.height = "3px";
	aNode.style.backgroundColor = "#73AABD";
	aNode.style.borderBottom = aNode.style.borderTop = "1px #EFEFEF solid";
	aNode.innerText = " ";
	aNode = getChNode(ret,rowIdx,2)
	aNode.appendChild(makeImgNode(UMenuBaseUrl+"bx"+bType+"_dw_r.gif",3,(5-bType)));
	return ret;
}
function getContentTable(targObj) {
	return getChNode(targObj,1,1).firstChild;
}
function UMenuChangeNick() {
	return ChangeNick();
}
function UMenuChangeNotify() {
	return ChangeUserNotify();
}
function UMenuAddBody(targObj, bLast) {
	if ( bLast ) 
		document.body.appendChild(targObj);
	else 
		document.body.insertBefore(targObj,document.body.firstChild);
	
	return targObj;
}
function UMenuAddSubMenu(targObj) {
	var sObj
	if ( (sObj = userContextMenuObj) && (sObj = sObj.userContextMenu) && (sObj = sObj.nextSibling) ) 
		document.body.insertBefore(targObj,sObj);
	else
		document.body.appendChild(targObj);
	
	return targObj;
}
function initARoomLvMenu() {
	var retNode = makeContextMenuTable(2, 90), ret = getContentTable(retNode);
	addMenuItem(ret, "", "置于顶层", 	1, 	0, "onmousedown", new Function("AMenuItemMoveZ(true, true)"));
	addMenuItem(ret, "", "置于底层", 	1, 	0, "onmousedown", new Function("AMenuItemMoveZ(false, true)"));
	addMenuItem(ret, "", "上移一层", 	1, 	0, "onmousedown", new Function("AMenuItemMoveZ(true, false)"));
	addMenuItem(ret, "", "下移一层", 	1, 	0, "onmousedown", new Function("AMenuItemMoveZ(false, false)"));
	
	return UMenuAddSubMenu(retNode);
}


function AMenuItemFlipH() {
	ciRoomFocusItemFlipH();
}
function AMenuItemMoveCenter() {
	ciRoomFocusItemMoveCenter();
}
function AMenuItemSetLink() {
	ciRoomFocusItemSetLink();
}
function AMenuItemRemove() {
	ciRemoveItem();
}
function AMenuItemBuy() {
	ciBuyItem();
}
function initUserContextMenu() {
	var retNode = makeContextMenuTable(1, 110), ret = getContentTable(retNode);
	
	addMenuItem(ret, bf_aitem_flip, "改变物品方向", 	1, 	0, "onmousedown", AMenuItemFlipH);
	addMenuItem(ret, bf_aitem_lv, "叠放次序", 1, 0, "onmouseover", AMenuLvChange);
	addMenuItem(ret, bf_aitem_linkData, "linkdata", 	1, 	0, "onmousedown", AMenuItemSetLink);
	addMenuItem(ret, bf_aitem_center, "放在中间", 	1, 	0, "onmousedown", AMenuItemMoveCenter);
	addMenuItem(ret, bf_aitem_remove, "移走该物品", 	1, 	0, "onmousedown", AMenuItemRemove);
	
	return UMenuAddBody(retNode);
}
function initOtherUserContextMenu() {
        var retNode = makeContextMenuTable(1, 110), ret = getContentTable(retNode);

        addMenuItem(ret, bf_aitem_flip, "查看详细信息",         1,      0, "onmousedown", AMenuItemBuy);
        //addMenuItem(ret, bf_aitem_lv, "叠放次序", 1, 0, "onmouseover", AMenuLvChange);
        //addMenuItem(ret, bf_aitem_linkData, "linkdata",         1,      0, "onmousedown", AMenuItemSetLink);
        //addMenuItem(ret, bf_aitem_center, "1111放在中间",   1,      0, "onmousedown", AMenuItemMoveCenter);
        //addMenuItem(ret, bf_aitem_remove, "移走该物品",         1,      0, "onmousedown", AMenuItemRemove);

        return UMenuAddBody(retNode);
}
function moveContextMenu(targObj, xpos, ypos) {
	var top, left, xMax;
	xMax = document.body.scrollLeft + document.body.clientWidth - targObj.style.pixelWidth - 10;
	targObj.style.pixelTop=ypos;
	left = Math.min(xMax, xpos);
  if ( userContextMenuObj.userContextMenu != targObj && xpos != left ) 
  	left = userContextMenuObj.userContextMenu.style.pixelLeft - targObj.style.pixelWidth+15;
  targObj.style.pixelLeft = left;
  if ( !bDivShow(targObj.style.display) ) {
  	//targObj.filters.blendTrans.apply();
  	setDivShown(targObj, true);
  	//targObj.filters.blendTrans.play();
  }
}
function open_user_menu(user_id,menu_type, forother, face,chname,m_cate,s_cate) {
	var bMyId, tBodyObj, my_id, targFlag;
	if ( user_id == "" ||  typeof(menu_type) != "number" ||  menu_type == 0 )
		return;
	else if ( isNaN(face) || face == 0 )
		face = "";
	face = Number(face);	
	if ( !userContextMenuObj.userContextMenu ) 
		if(!forother)
			user_context_menu = userContextMenuObj.userContextMenu = initUserContextMenu();
		else
			user_context_menu = userContextMenuObj.userContextMenu = initOtherUserContextMenu();
	else 
		resetContextMenu();
	userContextMenuObj.user_id = user_id;
	userContextMenuObj.srcElement = ( ISObject(event) ? event.srcElement : false );
	userContextMenuObj.face = ( isNaN(face) || face == 0 ? "" : face);
	userContextMenuObj.chname = ( chname ? chname : "" );
	userContextMenuObj.m_cate = ( m_cate ? m_cate : "" );
	userContextMenuObj.s_cate = ( s_cate ? s_cate : "" );
	
	tBodyObj = getContentTable(userContextMenuObj.userContextMenu).firstChild;
	if ( typeof(user_id) == "number" )
		bMyId = false;
	else {
		bMyId = ((my_id = getAppletUserID()) != "" && strIrcIdEq(user_id,my_id) );
		if ( bMyId && (bf_info & menu_type) ) {
			menu_type &= ~bf_info;
			menu_type |= bf_myinfo;
		}
	}
	for(i=0;i<tBodyObj.childNodes.length;i++) {
		targFlag = Number(tBodyObj.childNodes[i].menuFlag);
		if ( objOnlyLoginInfo[targFlag] && my_id == "" ) 
			continue;
		setDivShown(tBodyObj.childNodes[i], 
							!( !( menu_type & targFlag ) 
							|| ( tBodyObj.childNodes[i].bOnlySet == 1 && bMyId )
							|| ( tBodyObj.childNodes[i].bOnlySet == 2 && !bMyId ) ));
	}
	moveContextMenu(userContextMenuObj.userContextMenu, event.x + document.body.scrollLeft, event.y+ document.body.scrollTop);
}
function user_menu_open(targ_url,w,h) {
	appletOpenWin(targ_url,w,h);
}
function user_menu_open2(targ_url,w,h) {
	var l=(screen.width-w)/2, t=(screen.height-h)/2, targ_win = window.open(targ_url,"","resizable=no,scrollbars=1,width="+w+",height="+h+",top="+t+",left="+l);
}
function hideSubMenu() {
	setDivShown(userContextMenuObj.faceContextMenu, false);
	setDivShown(userContextMenuObj.aRLvContextMenu, false);
	setDivShown(userContextMenuObj.WelcContextMenu, false);
}
function delete_context_menu() {
	resetContextMenu();
	if ( typeof(pop_menu_voice) == "object" ) 
		setDivShown(pop_menu_voice, false);
}
function bShowaRLvContextMenu() {
	return bDivShow(userContextMenuObj.aRLvContextMenu);
}
function setDivShown(targObj, bShow) {
	if ( ISObject(targObj) )
		targObj.style.display = ( bShow ? "" : "none");
}
function bDivShow(targObj) {
	return ( ISObject(targObj) && targObj.style.display == "" );
}
function isSrcOwnerObj(targObj) {
	var ret = false, aNode = userContextMenuObj.srcElement;
	if ( ISObject(targObj) && ISObject(aNode) ) 
		while(!(ret = ( aNode == targObj )) && ISObject(aNode = aNode.parentNode) &&  aNode != document.documentElement );
	return ret;
}
function resetContextMenu() {
	with(userContextMenuObj) {
		user_id = "";
		face = "";
		mcate = "";
		scate = "";
		chname = "";
		srcElement = false;
	}
	setDivShown(userContextMenuObj.userContextMenu, false);
	hideSubMenu();
}
function initContextMenu() {
	this.userContextMenu = false;
	this.faceContextMenu = false;
	this.aRLvContextMenu = false;
	this.WelcContextMenu = false;
	this.user_id = "";
	this.face = "";
	this.mcate = "";
	this.scate = "";
	this.chname = "";
	this.srcElement = false;
}
UMenuBaseUrl = "images/img_home/";
baseFlag = 0x01;
bf_info 	= baseFlag;
bf_memo 	= (baseFlag<<=1);
bf_date 	= (baseFlag<<=1);
bf_addf 	= (baseFlag<<=1);
bf_delf 	= (baseFlag<<=1);
bf_delbf 	= (baseFlag<<=1);
bf_addbf 	= (baseFlag<<=1);
bf_shop		= (baseFlag<<=1);
//bf_want		= (baseFlag<<=1);
bf_cj			= (baseFlag<<=1);
bf_myinfo = (baseFlag<<=1);
bf_cloth  = (baseFlag<<=1);
bf_gItem  = (baseFlag<<=1);
//bf_sendNCard = (baseFlag<<=1);
bf_faceChange	= (baseFlag<<=1);
bf_changenick = (baseFlag<<=1);
bf_changenotify = (baseFlag<<=1);
bf_HompyInvite = (baseFlag<<=1);
bf_ViewHompy = (baseFlag<<=1);
bf_ViewNCBook = (baseFlag<<=1);
bf_WelcMusic = (baseFlag<<=1);

bf_aitem_lv 	= (baseFlag<<=1);
bf_aitem_flip = (baseFlag<<=1);
bf_aitem_center = (baseFlag<<=1);
bf_aitem_remove = (baseFlag<<=1);
bf_aitem_content = (baseFlag<<=1);
bf_aitem_linkData = (baseFlag<<=1);

//def_bf = bf_info | bf_memo | bf_shop | bf_want | bf_sendNCard | bf_faceChange | bf_HompyInvite;
def_bf = bf_info | bf_memo | bf_shop | bf_faceChange | bf_HompyInvite | bf_ViewHompy;
bf_type0 = def_bf | bf_addf;
bf_type1 = def_bf | bf_date | bf_addf;
bf_type2 = def_bf | bf_date | bf_delf;
bf_type3 = def_bf | bf_delbf;
bf_type4 = bf_type1 | bf_addbf;
bf_type5 = bf_type0 | bf_addbf;
bf_type6 = (bf_type1 & ~bf_info & ~bf_faceChange);
bf_type7 = bf_type2 | bf_cj;
bf_type8 = bf_faceChange | bf_myinfo | bf_cloth | bf_gItem | bf_ViewHompy | bf_ViewNCBook | bf_WelcMusic;

bf_azit = bf_aitem_lv | bf_aitem_flip | bf_aitem_center | bf_aitem_remove | bf_aitem_content;

objOnlyLoginInfo = new Object();
objOnlyLoginInfo[bf_memo] = 
	objOnlyLoginInfo[bf_date] = 
	objOnlyLoginInfo[bf_addf] = 
	objOnlyLoginInfo[bf_delf] = 
	objOnlyLoginInfo[bf_HompyInvite] = 
	objOnlyLoginInfo[bf_delbf] = 
	objOnlyLoginInfo[bf_addbf] = true;

var userContextMenuObj = new initContextMenu();
user_context_menu = userContextMenuObj.userContextMenu;
document.onmousedown = delete_context_menu;
