function Menu(){
    this.init.apply(this,arguments);
    try{document.execCommand("BackgroundImageCache",false,true)}catch(e){}
};
Menu.prototype={
    offset:{x:0,y:0},
    maskOffset:0,
    on:function (el,type,fn){//事件添加
        el.attachEvent?
            el.attachEvent('on'+type,function(){fn.call(el,event)}):
            el.addEventListener(type,fn,false);    
    },
    get:function (el){//获取对象
        return typeof el=="string"?document.getElementById(el):el;
    },
    rect:function(el){//获取位置
        var R=el.getBoundingClientRect(),r={};
        var d=document,dd=d.documentElement,db=d.body,X=Math.max;
        for(var k in R)r[k]=R[k];
        r.left+=X(dd.scrollLeft,db.scrollLeft)-(dd.clientLeft||db.clientLeft||0);
        r.top+=X(dd.scrollTop,db.scrollTop)-(dd.clientTop||db.clientTop||0);
        return r
    },
    getTitle:function (el){//用来配置非自身title
       return el 
    },
    init:function (nav,type,shadow){//初化绑定
        var _=this.get(nav).id.split('__'),title,reg,test,tags,iscur,e='mouseover',_Menu=this;
        this.opt=_;
        tags=this.get(nav).getElementsByTagName(_[1]);
        reg='\\b'+_[2]+'\\b|'+(_[3]?'\\b'+_[3]+'\\b':'');
        test=RegExp(reg);
        this.del=RegExp(reg,'g');
        iscur=RegExp('\\b'+_[2]+'\\b');
        this.opt=_.concat(['','']).slice(0,4);
        for(var i=0,n=0,l=tags.length;i<l;i++){
            title=tags[i];
            if(!test.test(title.className))continue;
            title.setAttribute('pop_menu_id',_[0]+'__'+(n++));
            type?this.blur(title):this.hover(title)
        };
        this.shadow=shadow?new Shadow():{show:function(){},hide:function(){}};
    },
    hover:function (title){//滑入
        var timer,body,_menu=this;
        body=this.get(title.getAttribute('pop_menu_id'));
        if(!body)return;
        body.bindTitle=title;
        var style=body.style;
        style.left='-9999px';
        style.display='block';
        this.onhover(title,mr,mt);
        this.onhover(body,mr,mt)
        function mr(){
            var T=_menu.getTitle(title);
            if(T.oldClass==undefined)T.oldClass=T.className;
            T.className=T.oldClass+' '+_menu.opt[2];
            _menu.align(body,T);
            _menu.onshow(body,title)            
        };
        function mt(){
            style.left='-9999px';
            var T=_menu.getTitle(title);
            if(T.oldClass==undefined)T.oldClass=T.className;
            T.className=T.oldClass+' '+_menu.opt[3];
            _menu.shadow.hide();
            _menu.onhide(body,title)
        }
    },
    onhover:function (el,enter,leave){
       if (! +'\v1') {
            el.onmouseenter = enter;
            el.onmouseleave = leave;
        }else {
            el.onmouseover =  enter;
            el.onmouseout = leave;
        }
    },
    stop:function (e){//事件传播终止
        e=e||window.event;
        if(e.stopPropagation)e.stopPropagation();
        e.cancelBubble = true
    },
    blur:function (title) {//点击
        var timer,body,_menu=this;
        body=this.get(title.getAttribute('pop_menu_id'));
        if(!body)return;
        var style=body.style;
        style.left='-9999px';
        style.display='block';
        this.on(title,'click',function (e){
            var T=_menu.getTitle(title);
            if(T.oldClass==undefined)T.oldClass=T.className;
            T.className=T.oldClass+' '+_menu.opt[2];
            _menu.align(body,T);
            _menu.onshow(body,title)
        });
        this.on(body,'click',function (e){
            _menu.stop(e)
        });
        this.on(document, 'click',function(e) {
            e=e||winode.event;
            var src=e.target||e.srcElement;
            do{
                if(src.getAttribute('pop_menu_id')==body.id)return;
                src=src.parentNode;
            }while(src&&src!=this)
            body.style.left='-999px';
            var T=_menu.getTitle(title);
            if(T.oldClass==undefined)T.oldClass=T.className;
            T.className=T.oldClass+' '+_menu.opt[3];
            _menu.shadow.hide();
            _menu.onhide(body,title)
        })
    },
    align:function (a,b){//位置对齐
        var pos=this.rect(b),dd=document.documentElement;
        var rflow=(dd.scrollLeft+dd.clientWidth<pos.left+a.offsetWidth);
        if(this.fix){
            a.style.left=this.fix()+this.offset.x+'px';
        }else{
            var left=(rflow?(pos.left-(a.offsetWidth-b.offsetWidth)):pos.left+this.offset.x);
            a.style.left=left+'px';
            if(this.right)a.style.left=pos.left+b.offsetWidth-1+'px';        
        };
        var $top=(this.right?pos.top:(pos.top+b.offsetHeight+this.offset.y));
        a.style.top=$top+'px';
        a.style.zIndex=10;
        this.shadow.show(a);
        var mask=a.getElementsByTagName('S');
        if(mask.length){
            var mk=mask[0];
            if(!this.right)mk.style.width=b.offsetWidth+this.maskOffset+'px';
            if(rflow){mk.style.right=0}else{mk.style.left=0};
            if(this.fix){
                mk.style.left=pos.left-this.fix()-this.offset.x+'px'
            };
        };
    },
    onshow:function(){},
    onhide:function(){},
    onchange:function(){}
};
/*
shadow
*/
function Shadow(isNew){
    var me=arguments.callee;
    if(!me.dom&&!isNew)
        me.dom=this.create();
    this.dom=isNew?this.create():me.dom;
};
Shadow.prototype={
    offset:2,
    ie6:window.ActiveXObject&&!window.XMLHttpRequest,
    create:function (){
        var div=document.createElement('DIV');
        document.body.appendChild(div);
        div.style.cssText='position:absolute;background:#000;filter:progid:DXImageTransform.Microsoft.Alpha(opacity=10);opacity:.1;left:-9999px;top:0;';
        return div
    },
    show:function (el){
        var r=this.rect(el),fix=0;
        var z=this.offset,s=this.dom.style;
        if(this.ie6)fix=parseInt(el.currentStyle.borderWidth)*2;
        s.width=r.right-r.left-fix+'px';
        s.height=r.bottom-r.top-fix+'px';
        s.left=r.left+z+'px';
        s.top=r.top+z+'px'      
    },
    hide:function (){
        this.dom.style.left='-9999px';
    },
    rect:function(el){
        var R=el.getBoundingClientRect(),r={};
        var d=document,dd=d.documentElement,db=d.body,X=Math.max;
        for(var k in R)r[k]=R[k];
        r.left+=X(dd.scrollLeft,db.scrollLeft)-(dd.clientLeft||db.clientLeft||0);
        r.top+=X(dd.scrollTop,db.scrollTop)-(dd.clientTop||db.clientTop||0);
        return r
    }
};
