function PopupMenu () {}

PopupMenu.authorName = null;
PopupMenu.menuTable = new HashMap ();

PopupMenu.ownerElement	= null;
PopupMenu.parameters = null;

PopupMenu.id = "qiezi_popup_menu";
PopupMenu.innerID = "qiezi_popup_menu_inner";
PopupMenu.actionID= "qiezi_ajax_action";
PopupMenu.actionInnerID		= "qiezi_ajax_action_inner";
PopupMenu.actionDraggerID	= "qiezi_ajax_action_dragger";
PopupMenu.menuDraggerID	= "qiezi_ajax_menu_dragger";
PopupMenu.menuIconID	= "qiezi_popup_menu_icon";

//记录当前对象的边框风格信息
PopupMenu.borderStyle = null;
PopupMenu.borderWidth = 0;
PopupMenu.borderColor = "";
PopupMenu.backgroundColor = "";

PopupMenu.x = 0;
PopupMenu.y = 0;

PopupMenu.pw = 100;
PopupMenu.ph = 100;

PopupMenu.aw = 260;
PopupMenu.ah = 160;

PopupMenu.menuTable.put ("sendMessage", {"id":"sendMesasge","name":"发送消息","access_mode":"form","page":"/v1.0/ajax/user/sendMessageForm.jsp?qzuser_seqno={id}"});
PopupMenu.menuTable.put ("sendNote", {"id":"sendNote","name":"留言","access_mode":"form","page":"/v1.0/ajax/user/sendNoteForm.jsp?qzuser_seqno={id}"});
PopupMenu.menuTable.put ("addAsContact", {"id":"addAsContact","name":"加为联系人","access_mode":"form","page":"/v1.0/ajax/user/addContactorForm.jsp?qzuser_seqno={id}"});
PopupMenu.menuTable.put ("login", {"id":"login","name":"请先登录","access_mode":"form","page":"/v1.0/ajax/user/loginForm.jsp"});
PopupMenu.menuTable.put ("sugest", {"id":"sugest","name":"用户建议","access_mode":"form","page":"/v1.0/ajax/user/sendSuggestForm.jsp"});

PopupMenu.menuTable.put ("addGroupAdmin", {"id":"addGroupAdmin","name":"设成管理员","access_mode":"jsfunction","jsfunction":"addGroupAdmin({id},{qzgroup_seqno})"});
PopupMenu.menuTable.put ("deleteGroupAdmin", {"id":"deleteGroupAdmin","name":"取消管理员","access_mode":"jsfunction","jsfunction":"deleteGroupAdmin({id},{qzgroup_seqno})"});
PopupMenu.menuTable.put ("deleteGroupUser", {"id":"deleteGroupUser","name":"踢出该群组","access_mode":"jsfunction","jsfunction":"deleteGroupUser({id},{qzgroup_seqno})"});


PopupMenu.menuTable.put ("setAsCover", {"id":"setAsCover","name":"设为封面","access_mode":"jsfunction","jsfunction":"UIAction.setAsCover({id},{album_id})"});
PopupMenu.menuTable.put ("tuyaPhoto", {"id":"tuyaPhoto","name":"涂鸦","access_mode":"jsfunction","jsfunction":"Tuya.open(PopupMenu.ownerElement, PopupMenu.parameters)"});
PopupMenu.menuTable.put ("rotatePhoto", {"id":"rotatePhoto","name":"翻转","access_mode":"form","page":"/v1.0/ajax/photo/rotatePhotoForm.jsp?qzphoto_seqno={id}"});
PopupMenu.menuTable.put ("editPhoto", {"id":"editPhoto","name":"编辑","access_mode":"form","page":"/v1.0/ajax/photo/updatePhotoForm.jsp?qzphoto_seqno={id}&qzalbum_seqno={album_id}"});
PopupMenu.menuTable.put ("commentPhoto", {"id":"commentPhoto","name":"评论","access_mode":"form","page":"/v1.0/ajax/photo/sendCommentForm.jsp?qzphoto_seqno={id}&qzalbum_seqno={album_id}"});
PopupMenu.menuTable.put ("addPhotoIntoFavorite", {"id":"addPhotoIntoFavorite","name":"收藏","access_mode":"jsfunction","jsfunction":"UIAction.favoritePhoto({id})"});
PopupMenu.menuTable.put ("recommendPhotoToArea", {"id":"recommendPhotoToArea","name":"推荐到群组","access_mode":"form","page":"/v1.0/ajax/photo/recommendToGroupForm.jsp?qzphoto_seqno={id}&qzalbum_seqno={album_id}"});
PopupMenu.menuTable.put ("recommendPhotoToUser", {"id":"recommendPhotoToUser","name":"推荐给好友","access_mode":"form","page":"/v1.0/ajax/photo/recommendToFriendForm.jsp?qzphoto_seqno={id}&qzalbum_seqno={album_id}"});
PopupMenu.menuTable.put ("recommendPhotoToSelf", {"id":"recommendPhotoToSelf","name":"推荐到首页","access_mode":"jsfunction","jsfunction":"UIAction.recommendPhotoToSelf({id})"});
PopupMenu.menuTable.put ("viewPhotoExif", {"id":"viewPhotoExif","name":"查看Exif","access_mode":"form","page":"/v1.0/ajax/photo/viewPhotoExif.jsp?qzphoto_seqno={id}"});
PopupMenu.menuTable.put ("deletePhoto", {"id":"deletePhoto","name":"删除","access_mode":"jsfunction","jsfunction":"UIAction.deletePhoto({id},{album_id})"});
PopupMenu.menuTable.put ("sendPhotoToMail", {"id":"sendPhotoToMail","name":"发送到邮箱","access_mode":"form","page":"/v1.0/ajax/photo/sendToEmailForm.jsp?qzphoto_seqno={id}"});
PopupMenu.menuTable.put ("address", {"id":"address","name":"图片地址","access_mode":"form","page":"/v1.0/ajax/photo/getPhotoURLForm.jsp?qzphoto_seqno={id}"});

PopupMenu.getMenuItem = function (id)
{
	return PopupMenu.menuTable.get (id);
}

PopupMenu.showOperIcon = function (ownerElement, parameters)
{
	PopupMenu.ownerElement	= ownerElement;
	PopupMenu.parameters	= parameters;
	PopupMenu.backgroundColor = ownerElement.style.backgroundColor;
	PopupMenu.borderStyle = ownerElement.style.borderStyle;
	PopupMenu.borderWidth = ownerElement.style.borderWidth;
	PopupMenu.borderColor = ownerElement.style.borderColor;
	
	ownerElement.style.backgroundColor = "#DDE8F7";
	ownerElement.style.borderStyle = "solid";
	ownerElement.style.borderWidth = 1;
	ownerElement.style.borderColor = "#4A71A9";
	
	var type	= parameters.type;
	var id		= parameters.id;
	var style	= parameters.style;
	var div = document.all[PopupMenu.menuIconID];
	
	if (div == null)
	{
		div = document.createElement ("div");
		div.id = PopupMenu.menuIconID;
	}
	
	if (style == "pop")
	{
		div.style.width = 13;
		div.style.height= 13;
		var img = new Image ();
		img.id = PopupMenu.menuIconID + "_img";
		img.src = "/v1.0/images/image_1110/arrow_image75.gif";
		div.appendChild (img);
		
		if(ownerElement.style.height != '')
		{
			div.style.position = "absolute";
			
			div.style.top = parseInt(ownerElement.style.height)-parseInt(div.style.height);
			div.style.left= parseInt(ownerElement.style.width)-parseInt(div.style.width);
			
			ownerElement.style.position = "relative";
			ownerElement.appendChild (div);
			ownerElement.orignClickFun = ownerElement.onclick;
			
			div.onmouseenter = function (){ownerElement.onclick=function(){};}
			div.onmouseleave = function (){ownerElement.onclick = ownerElement.orignClickFun;}
			div.onclick		= function () {PopupMenu.showMenu (PopupMenu.parameters);}
		}
	}
	else
	{
		ownerElement.style.borderWidth = 0;
		
		div.style.width = 17;
		div.style.height= 16;
		var img = new Image ();
		img.id = PopupMenu.menuIconID + "_img";
		img.src = "/v1.0/images/arrow_image75_01.gif";
		img.onmouseenter = function() {this.src="/v1.0/images/arrow_image75_02.gif";}
		img.onmouseleave = function() {this.src="/v1.0/images/arrow_image75_01.gif";}
		div.appendChild (img);
		
		div.style.position = "absolute";
		div.style.top = 0;
		div.style.left= 0;
		
		var nodes = ownerElement.childNodes;
		nodes[nodes.length-1].style.position = "relative";
		nodes[nodes.length-1].appendChild (div);
		
		div.onclick=function () {PopupMenu.showMenu (PopupMenu.parameters);}
	}
	
	div.style.visibility = "visible";
}

PopupMenu.hideOperIcon = function (ownerElement)
{
	ownerElement.style.backgroundColor = PopupMenu.backgroundColor;
	ownerElement.style.borderStyle = PopupMenu.borderStyle;
	ownerElement.style.borderWidth = PopupMenu.borderWidth;
	ownerElement.style.borderColor = PopupMenu.borderColor;
	
	if (document.all[PopupMenu.menuIconID] != null)
		document.all[PopupMenu.menuIconID].parentElement.removeChild (document.all[PopupMenu.menuIconID]);
}

PopupMenu.showMenu = function (parameters)
{
	PopupMenu.parameters = parameters;
	
	if (parameters.container)
		PopupMenu.ownerElement = parameters.container;
	
	var type	= parameters.type;
	var id		= parameters.id;
	var album_id= parameters.album_id;
	var qzgroup_seqno=parameters.qzgroup_seqno;
	var style	= parameters.style;
	
	PopupMenu.createPopupMenu ();
	var div = document.all[PopupMenu.id];
	
	var vLeft=0;
	var vTop = 0;
	var obj = PopupMenu.ownerElement;
 
	//循环的方法取得对象的绝对偏移
	while(obj != null && obj !=document.body)
	{
		vLeft += obj.offsetLeft;
		vTop +=  obj.offsetTop;
		obj = obj.offsetParent;
	}
	
	
	PopupMenu.x = Math.min (parseInt (vLeft), parseInt(document.body.clientWidth)-parseInt(div.style.width)) + document.body.scrollLeft;
	PopupMenu.y = Math.min (parseInt (vTop+PopupMenu.ownerElement.offsetHeight), (parseInt(document.body.clientHeight)-parseInt(div.style.height) + document.body.scrollTop));
	
	if(type == "qzphoto" && (style == null || style == ""))
	{
		PopupMenu.x = Math.min (parseInt (event.clientX), parseInt(document.body.clientWidth)-parseInt(div.style.width)) + document.body.scrollLeft;
		PopupMenu.y = Math.min (parseInt (event.clientY), parseInt(document.body.clientHeight)-parseInt(div.style.height)) + document.body.scrollTop;
	}
	
	div.style.left = PopupMenu.x;
	div.style.top  = PopupMenu.y;
	div.style.display = "";
	div.style.visibility = "visible";
	document.all[PopupMenu.innerID].innerHTML = "正在读取菜单...";
	Ajax.sendGetRequest ("/v1.0/popupMenu.jsp?type=" + type + "&ID=" + id + "&album_id=" + album_id+"&qzgroup_seqno="+qzgroup_seqno, true, null, 'PopupMenu.displayMenuItems()');
}

PopupMenu.hideMenu = function ()
{
	if (document.all[PopupMenu.innerID] != null)
		document.all[PopupMenu.innerID].innerHTML = "";
	
	var div = document.all[PopupMenu.id];
	
	if (div != null)
	{
		div.style.display = "none";
		div.style.visibility = "hidden";
	}
}

PopupMenu.createPopupMenu = function ()
{
	if (document.all[PopupMenu.id] == null)
	{
		var div = document.createElement ("div");
		div.id = PopupMenu.id;
		div.style.backgroundColor = "#FFFFFF";
		div.style.borderStyle = "solid";
		div.style.borderWidth = 1;
		div.style.borderColor = "#555555";
		div.style.left = 0;
		div.style.top = 0;
		div.style.width = PopupMenu.pw;
		div.style.height = PopupMenu.ph;
		
		div.style.position = "absolute";
		div.style.display = "none";
		div.style.visibility = "hidden";
		
		div.innerHTML = "<table width=100% border=0 cellpadding=0 cellspacing=0><tr height=15><td style='cursor:move;background-color:#ffffff;border-bottom-width:1;border-bottom-style:solid;border-bottom-color:#eeeeee' id='" + PopupMenu.menuDraggerID + "' align=right><img src='/v1.0/images/menu_close_03.gif' style='cursor:default' onmouseover='this.src=\"/v1.0/images/menu_close_04.gif\"' onmouseout='this.src=\"/v1.0/images/menu_close_03.gif\"' onclick='PopupMenu.hideMenu()'></td></tr><tr><td id='" + PopupMenu.innerID + "'></td></tr></table>";
		
		document.body.appendChild (div);
		
		var dragger = document.all[PopupMenu.menuDraggerID];
		
		dragger.onmousedown = function ()
		{
			PopupMenu.beDragging = true;
			
			PopupMenu.x = parseInt (event.clientX) + document.body.scrollLeft;
			PopupMenu.y = parseInt (event.clientY) + document.body.scrollTop;
			
			this.setCapture ();
		}
		
		dragger.onmousemove = function ()
		{
			if (PopupMenu.beDragging)
			{
				var x = parseInt (event.clientX) + document.body.scrollLeft;
				var y = parseInt (event.clientY) + document.body.scrollTop;
				
				var div = document.all[PopupMenu.id];
				
				div.style.left = parseInt (div.style.left) + x - PopupMenu.x;
				div.style.top = parseInt (div.style.top) + y - PopupMenu.y;
				
				PopupMenu.x = x;
				PopupMenu.y = y;
				
				if (parseInt (div.style.left) < 0)
				{
					div.style.left = 0;
					PopupMenu.x = 0;
				}
				
				if (parseInt (div.style.top) < 0)
				{
					div.style.top = 0;
					PopupMenu.y = 0;
				}
			}
		}
		
		dragger.onmouseup = function ()
		{
			PopupMenu.beDragging = false;
			this.releaseCapture ();
		}
	}
}

PopupMenu.createActionDiv = function (title, formPage)
{
	if (document.all[PopupMenu.actionID] == null)
	{
		var div = document.createElement ("div");
		div.id = PopupMenu.actionID;
		div.style.borderStyle = "solid";
		div.style.borderWidth = 1;
		div.style.borderColor = "#555555";
		div.style.left = 0;
		div.style.top = 0;
		div.style.width = PopupMenu.aw;
		div.style.height = PopupMenu.ah;
		
		div.style.position = "absolute";
		div.style.display = "none";
		div.style.visibility = "hidden";
		
		div.innerHTML = "<table width=100% height=100% border=0 cellpadding=0 cellspacing=0><tr height=15><td style='cursor:move;background-color:#e1e1e1'><table width=100% border=0 cellpadding=0 cellspacing=0><tr><td style='cursor:move;background-color:#e1e1e1' id='" + PopupMenu.actionDraggerID + "'>&nbsp;</td><td style='background-color:#e1e1e1' width=20 align=right><img src='/v1.0/images/popup_close_common.gif' style='cursor:default' onmouseover='this.src=\"/v1.0/images/popup_close_focus.gif\"' onmouseout='this.src=\"/v1.0/images/popup_close_common.gif\"' onclick='PopupMenu.hideActionDiv()'></img></td></tr></table></td></tr><tr><td><table width=100% height=100% border=0 cellpadding=0 cellspacing=0><tr><td><iframe onload='PopupMenu.resizeActionDiv(this.contentWindow.document.body.scrollWidth,this.contentWindow.document.body.scrollHeight)' id='" + PopupMenu.actionInnerID + "' frameborder='0' src='' width='100%' height='100%'></iframe></td></tr></table></td></tr></table>";
		
		document.body.appendChild (div);
		
		var dragger = document.all[PopupMenu.actionDraggerID];
		
		dragger.onmousedown = function ()
		{
			PopupMenu.beDragging = true;
			
			PopupMenu.x = parseInt (event.clientX) + document.body.scrollLeft;
			PopupMenu.y = parseInt (event.clientY) + document.body.scrollTop;
			
			this.setCapture ();
		}
		
		dragger.onmousemove = function ()
		{
			if (PopupMenu.beDragging)
			{
				var x = parseInt (event.clientX) + document.body.scrollLeft;
				var y = parseInt (event.clientY) + document.body.scrollTop;
				
				var div = document.all[PopupMenu.actionID];
				
				div.style.left = parseInt (div.style.left) + x - PopupMenu.x;
				div.style.top = parseInt (div.style.top) + y - PopupMenu.y;
				
				PopupMenu.x = x;
				PopupMenu.y = y;
				
				if (parseInt (div.style.left) < 0)
				{
					div.style.left = 0;
					PopupMenu.x = 0;
				}
				
				if (parseInt (div.style.top) < 0)
				{
					div.style.top = 0;
					PopupMenu.y = 0;
				}
			}
		}
		
		dragger.onmouseup = function ()
		{
			PopupMenu.beDragging = false;
			this.releaseCapture ();
		}
	}
	
	var title_str = "";
	
	if (PopupMenu.authorName == null)
		title_str = title;
	else
	{
		if (PopupMenu.authorName.length > 16)
			title_str = "[" + PopupMenu.authorName.substring(0,16) + "...]" + title;
		else
			title_str = "[" + PopupMenu.authorName + "]" + title;
	}
	
	document.all[PopupMenu.actionDraggerID].innerHTML = "<font color=#000000>" + title_str + "</font>";
	document.all[PopupMenu.actionInnerID].src = "about:blank";
	document.all[PopupMenu.actionInnerID].src = formPage;
}

PopupMenu.displayMenuItems = function ()
{
	if (!Ajax.http_request.responseText || Ajax.http_request.responseText == "")
	{
		PopupMenu.hideMenu ();
		return;
	}
	
	var response = eval ('(' + Ajax.http_request.responseText + ')');
	
	if ( response.menuItems == null || response.menuItems.length == 0)
	{
		PopupMenu.hideMenu ();
		return;
	}
	
	PopupMenu.authorName = response.authorName;
	
	var s = "<table width=100% border=0 cellpadding=0 cellspacing=4>";
	
	for (var i = 0; response.menuItems != null && i < response.menuItems.length; i++)
	{
		var item = PopupMenu.getMenuItem (response.menuItems[i].menuID);
		
		if (item == null)
			continue;
		
		s += "<tr><td style='cursor:hand;padding-bottom:2px;padding-top:2px;padding-right:2px;padding-left:2px;' onmouseenter=\"this.style.backgroundColor='#DDE9F9'\" onmouseleave=\"this.style.backgroundColor=''\" onclick='PopupMenu.hideMenu();";
		
		if (item.access_mode == "form")
		{
			var page_url = item.page;
			page_url = page_url.replace (/\{type\}/, PopupMenu.parameters.type);
			page_url = page_url.replace (/\{id\}/, PopupMenu.parameters.id);
			page_url = page_url.replace (/\{album_id\}/, PopupMenu.parameters.album_id);
			s += "PopupMenu.showActionDiv(\"" + item.name + "\",\"" + page_url + "\");";
		}
		else if (item.access_mode == "jsfunction")
		{
			var jsfunction_express = item.jsfunction;
			jsfunction_express = jsfunction_express.replace (/\{type\}/, PopupMenu.parameters.type);
			jsfunction_express = jsfunction_express.replace (/\{id\}/, PopupMenu.parameters.id);
			jsfunction_express = jsfunction_express.replace (/\{album_id\}/, PopupMenu.parameters.album_id);
			jsfunction_express = jsfunction_express.replace (/\{qzgroup_seqno\}/, PopupMenu.parameters.qzgroup_seqno);
			s += jsfunction_express;
		}
		
		s += "'>" + item.name + "</td></tr>";
	}
	
	s += "</table>";
	
	document.all[PopupMenu.innerID].innerHTML = s;
}

PopupMenu.showActionDiv = function (title, formPage)
{
	PopupMenu.createActionDiv (title, formPage);
	
	var div = document.all[PopupMenu.actionID];
	
	PopupMenu.x = Math.min (PopupMenu.x, parseInt(document.body.clientWidth)-parseInt(div.style.width) + document.body.scrollLeft);
	PopupMenu.y = Math.min (PopupMenu.y, parseInt(document.body.clientHeight)-parseInt(div.style.height) + document.body.scrollTop);
	
	div.style.left = PopupMenu.x;
	div.style.top  = PopupMenu.y;
	
	div.style.display = "";
	div.style.visibility = "visible";
}

PopupMenu.hideActionDiv = function ()
{
	var div = document.all[PopupMenu.actionID];
	
	if (div != null)
	{
		div.style.display = "none";
		div.style.visibility = "hidden";
	}
}

PopupMenu.resizeActionDiv = function (w, h)
{
	var div = document.all[PopupMenu.actionID];
	
	if (div != null)
	{
		div.style.width = Math.max (w, PopupMenu.aw);
		div.style.height= Math.max (h+25, PopupMenu.ah);
	}
}

//file_url=/photo/orign/91/o1934523043.jpg?random=1254536443
PopupMenu.refreshImage = function (file_url)
{
  var nodes = this.ownerElement.childNodes;
  
  var file_name = file_url.substring (file_url.lastIndexOf ("/") + 1);
  file_name = file_name.substring (0, file_name.lastIndexOf ("."));
  
  var random_feed = "";
  
  if (file_url.indexOf ("?random") > 0)
    random_feed = file_url.substring (file_url.indexOf ("?random"));
  
  var ownerSize = PopupMenu.getSize (this.ownerElement);
  
  for (var i = 0; nodes != null && i < nodes.length; i++)
  {
    if (nodes[i].tagName == "IMG" && nodes[i].src != null && nodes[i].src.indexOf (file_name) >= 0)
    {
      if (!nodes[i].id)
        nodes[i].id = new Date().getTime();
      
      var img = new Image ();
      img.nodeID = nodes[i].id;
      
      img.onload = function ()
      {
        var imageNode = document.getElementById (this.nodeID);
        
        if (imageNode != null)
        {
          imageNode.width = this.width;
          imageNode.height= this.height;
          imageNode.src = this.src;
          
          if (imageNode.parentElement)
          {
            imageNode.parentElement.style.width = this.width;
            imageNode.parentElement.style.height=this.height;
          }
        }
      }
      
      if (nodes[i].src.indexOf ("?random") < 0)
        img.src = nodes[i].src + random_feed;
      else
        img.src = nodes[i].src.substring(0,nodes[i].src.indexOf ("?random")) + random_feed;
      
      break;
    }
  }
}

PopupMenu.refreshComment = function (commented_times)
{
  var commentImgID = this.ownerElement.id+"_comment";
  var commentImg = document.getElementById(commentImgID);
  
  if(commentImg)
  {
    commentImg.title = "评论数("+commented_times+")";
  }
  else
  {
    this.ownerElement.style.position = "relative";
		commentImg = new Image ();
		commentImg.id = commentImgID;
		commentImg.style.position = "absolute";
		commentImg.border = 0;
		commentImg.style.width = 15;
		commentImg.style.height = 15;
		commentImg.style.top = parseInt(this.ownerElement.style.height) - 18;
		commentImg.style.left = 3;
		commentImg.title = "评论数("+parseInt(commented_times)+")";
		this.ownerElement.appendChild (commentImg);
		
		commentImg.src = "/v1.0/images/image_1110/icon_has_comment.gif";
  }
}

PopupMenu.getSize = function (htmlObj)
{
  if (htmlObj.tagName == "IMG")
    return {"w":parseInt(htmlObj.width),"h":parseInt(htmlObj.height)};
  else
    return {"w":parseInt(htmlObj.style.width),"h":parseInt(htmlObj.style.height)};
}