﻿function csMenu(_object, _menu)
{
    this.IEventHander = null;
//    this.IFrameHander = null;
    this.IContextMenuHander = null;
	
	this.ZIndex = function(state){
		return function(){
			var ZIndexObj = _Felement.parentNode;
			alert(ZIndexObj.style.zIndex);
			//this.Hide(_menu);
			_menu.style.display="none";
//			removeClick();
//				function removeClick(){
//						for (var i=0; i<_menuList.length-1; i++){
//							removeEventHandler(_menuList[i],"click", this.ZIndex(handleState[i]));
//							//_menuList[i].onclick=function(){};
//						}		
//				}
			var html="";
				html+="<li><a href=\"javascript:void(0)\">移动到顶层</a></li>";
				html+="<li><a href=\"javascript:void(0)\">移动到下一层</a></li>";
				html+="<li><a href=\"javascript:void(0)\">移动到上一层</a></li>";
				html+="<li><a href=\"javascript:void(0)\">移动到底层</a></li>";
				html+="<li><a href=\"javascript:void(0)\">设置模块大小</a></li>";
				html+="<li><a href=\"javascript:void(0)\">移除模块</a></li>";
				$("h3RightMenu").innerHTML=html;
		}
	}
	
	this.Size = function(){
		alert("大小");
	}

	this.Remove = function(){
		alert("移除");
	}
	
    this.Show = function(_menu,evt)
    {
        var e = evt || window.event;
        _Felement = e.srcElement || e.target;	

        if (e.button == 2)
        {
            if (window.document.all)
            {
                this.IContextMenuHander = function(){return false;};
                document.attachEvent("oncontextmenu", this.IContextMenuHander);
            }
            else
            {
                this.IContextMenuHander = document.oncontextmenu;
                document.oncontextmenu = function(){return false;};
            }

            window.csMenu$Object = this;
            this.IEventHander = function(event){window.csMenu$Object.Hide(_menu,event);};

            if (window.document.all)
                document.attachEvent("onmousedown", this.IEventHander);
            else
                document.addEventListener("mousedown", this.IEventHander, false);

            _menu.style.left = e.clientX + "px";
            _menu.style.top = e.clientY + "px";
            _menu.style.display = "";

//绑定菜单事件
			_menuList = cleanWhitespaces(_menu);
			handleState = ["top","up","down","bot"];			
			for (var li=0; li<_menuList.length; li++){
				switch (li){
					case 0:
					case 1:
					case 2:
					case 3:
						addEventHandler(_menuList[li], "click", this.ZIndex(handleState[li]));
					break;
					case 4:
						addEventHandler(_menuList[li], "click", this.Size);
					break;
					case 5:
						addEventHandler(_menuList[li], "click", this.Remove);
					break;
					default:
						alert("...");
				}
			}
	
//            if (this.IFrameHander)
//            {
//                var _iframe = document.getElementById(this.IFrameHander);
//                _iframe.style.left = e.clientX;
//                _iframe.style.top = e.clientY;
//                _iframe.style.height = _menu.offsetHeight;
//                _iframe.style.width = _menu.offsetWidth;
//                _iframe.style.display = "";
//            }
        }
    };

    this.Hide = function(_menu,evt)
    {
        var e = evt || window.event;
        var _element = e.srcElement || e.target;
        do
        {
            if (_element == _menu)
            {
                return false;
            }
        }
        while (_element = _element.offsetParent);
        
        if (window.document.all)
         document.detachEvent("on"+e.type, this.IEventHander);
        else
         document.removeEventListener(e.type, this.IEventHander, false);
        
//        if (this.IFrameHander)
//        {
//            var _iframe = document.getElementById(this.IFrameHander);
//            _iframe.style.display = "none";
//        }
        
        _menu.style.display = "none";
        
        if (window.document.all)
         document.detachEvent("oncontextmenu", this.IContextMenuHander);
        else
         document.oncontextmenu = this.IContextMenuHander;
    };

    this.initialize = function(_object, _menu)
    {  
        window._csMenu$Object = this;
        var _eventHander = function(event){window._csMenu$Object.Show(_menu,event);};
	
//	_object.style.position = "relative";
	
     _menu.style.position = "absolute";
     _menu.style.display = "none";
     _menu.style.zIndex = "1000000";
     
        if (window.document.all)
        {
//            var _iframe = document.createElement('iframe');
//     document.body.insertBefore(_iframe, document.body.firstChild);
//            _iframe.id = _menu.id + "_iframe";
//            this.IFrameHander = _iframe.id;
//
//            _iframe.style.position = "absolute";
//            _iframe.style.display = "none";
//            _iframe.style.zIndex = "999999";
//            _iframe.style.border = "0px";
//            _iframe.style.height = "0px";
//            _iframe.style.width = "0px";
            
            _object.attachEvent("onmouseup", _eventHander);
        }
        else
        {
            _object.addEventListener("mouseup", _eventHander, false);
        }
    };

    this.initialize(_object, _menu);
}

//=======================需要显示的菜单,一般用DIV======================
//<div id="Menu1" style="background-color:White; border:1px solid #cccccc; padding:10px;">
//    <li>打开</li>
//    <li>打印</li>
//    <li>回复发件人</li>
//    <li>全部回复</li>
//    <li>转发</li>
//    <li>分配</li>
//    <li>垃圾邮件</li>
//    <li>删除</li>
//    <li>归档此邮件</li>
//    <li>分拣此邮件</li>
//</div>
//
//============================调用方法===============================
//
//var MM = new csMenu(document.getElementById("Table1"), document.getElementById("Menu1"));
//
//其中, document.getElementById("Table1") 就是需要显示菜单的区域控件.
//         document.getElementById("Menu1") 就是菜单DIV控件.
