// JavaScript Document
function MenuEvent(){
    this._Args = arguments;
}
MenuEvent.prototype = {    
    //遍历
    _TraversalBindEvent:function(closeStyle,openStyle){
        for(var i = 0; i<this._Args.length; i++){
            var menuObj = this._GetElement(this._Args[i])
            menuObj.onmouseover = this._BindEvent(menuObj,"onmouseover",closeStyle,openStyle);
            menuObj.onmouseout = this._BindEvent(menuObj,"onmouseout",closeStyle,openStyle);
            //偏移，让两个Div的边框重叠
            if(i>0){menuObj.style.margin = "0 0 0 -1";}
        }
    },
    //获取对象
    _GetElement:function(elementID){
        var menuObj = window.document.getElementById(elementID);
        if(!menuObj || menuObj.tagName.toLowerCase()!="div")
        {
            throw "主菜单对象中至少有一个是非法对象!";
        }
        return menuObj;
    },
    //绑定事件
    _BindEvent:function(menuObj,onmouse,closeStyle,openStyle){
        var obj = this;
        switch(onmouse){
            case "onmouseover":
                    return function(){
                        obj._onMouseOverModel(menuObj,openStyle);
                    }
                break;
            case "onmouseout":
                    return function(){
                        obj._onMouseOutModel(menuObj,closeStyle)
                    }
                break;
        }
    },
    //事件模型
    _onMouseOverModel:function(menuObj,openStyle){
        //切换样式，打开菜单
        menuObj.className = openStyle;
        //子对象链带事件
        this._ChildBindEvent(menuObj,"onmouseover");
    },
    //事件模型
    _onMouseOutModel:function(menuObj,closeStyle){
        //切换样式，关闭菜单
        menuObj.className = closeStyle;
        //子对象链带事件
        this._ChildBindEvent(menuObj,"onmouseout");
    },
    //子对象跟随父对象的事件（这里并不是子对象自身触发的事件，而是父对象的事件连带触发的事件，这里还可以继续扩展子对象的样式）
    _ChildBindEvent:function(menuObj,onmouse){
        for(var i=0; i<menuObj.childNodes.length; i++){
            var menuItemObj = menuObj.childNodes[i];
            //避免非对象 和 非文档标签对象
            if( !menuItemObj || !menuItemObj.tagName ){
                continue;
            }
            //避免非div标签
            if( menuItemObj.tagName.toLowerCase()!="div" ){
                continue;
            }
            //通过父对象过滤孙子
            if( menuItemObj.parentNode.id != menuObj.id ){
                continue;
            }
            switch(onmouse){
                case "onmouseover":
                    menuItemObj.style.height = "auto";
                    menuItemObj.style.visibility = "visible";
                    break;
                case "onmouseout":
                    menuItemObj.style.height = "0px";
                    menuItemObj.style.visibility = "hidden";
                    break;
            }
        }
    }
}
//Class MenuEvent - OVER

//入口程序
window.onload = function(){
    try{
        new MenuEvent("menuTag1")._TraversalBindEvent("menuTag","menuTagOpen");
    }catch(error){
        alert(error);
    }
}
