﻿var activemenu=null;
    var timer=null;
    function mover(spanid,overcss){
        clearTimer();
        if(activemenu!=null){
            if(activemenu.id.split("_")[0]!=spanid&&spanid.length==5){
                activemenu.style.visibility='hidden';
                document.getElementById(activemenu.id.replace('_childs','')).className=document.getElementById(activemenu.id.replace('_childs','')).defaultcss;
            }
        }
        if(overcss!=0){
            document.getElementById(spanid).className=overcss;
        }
        if( document.getElementById(spanid+"_childs")!=null ){
            itop = 0;
            ileft = 0;
            var oTmp = document.getElementById(spanid);
            do {
                   ileft += oTmp.offsetLeft;
                   itop += oTmp.offsetTop;
                   oTmp = oTmp.offsetParent;
            } while(oTmp.tagName!="BODY");      
            
            document.getElementById(spanid+"_childs").style.left=(ileft)+"px";
            document.getElementById(spanid+"_childs").style.top=(itop+document.getElementById(spanid).offsetHeight)+"px";
            document.getElementById(spanid+"_childs").style.visibility="visible";
            activemenu=document.getElementById(spanid+"_childs");
        }
        
        if(overcss==0){
            activemenu=document.getElementById(spanid);
        }
        
    }
    function clearTimer()
    {
        if (timer != null)
            window.clearTimeout(timer);
        timer = null;
    }

    function mout(spanid,oldcss){
        clearTimer();
        currentcss=document.getElementById(spanid).className;        
        if(oldcss!=0){
            document.getElementById(spanid).className=oldcss;
        }
        if(spanid.length==5){
            document.getElementById(spanid).className=currentcss;
        }
        timer=window.setTimeout("activemenu.style.visibility='hidden';document.getElementById(activemenu.id.replace('_childs','')).className=document.getElementById(activemenu.id.replace('_childs','')).defaultcss;",500);
    }
    
    function menu(id,title,url,target,cssclass,overcssclass){
        if(id=="" || title==""){
            throw "菜单对象必须指定id值和title值";
        }
        this.id=id;
        this.title=title;
        this.url=(url==null)?"#":url;
        this.target=(target==null)?"":" target='"+target+"'";
        this.cssclass=(cssclass==null)?"":cssclass;
        this.overcssclass=(overcssclass==null)?"":overcssclass;
        this.childs=new Array();        
        this.spanchilds="";
    }
    menu.prototype.add=function(m){
        this.childs.push(m);
    }
    menu.prototype.toString=function(){
        var mystr="";
        mystr="<span id='[id]' class='[class]' onmouseover='mover(this.id,\""+this.overcssclass+"\")' onmouseout='mout(this.id,\""+this.cssclass+"\")' defaultcss=\""+this.cssclass+"\" overcss=\""+this.overcssclass+"\"><a href='[url]'[target]>[title]</a></span>";
        mystr=mystr.replace("[id]",this.id);
        mystr=mystr.replace("[class]",this.cssclass);       
        mystr=mystr.replace("[title]",this.title);
        mystr=mystr.replace("[url]",this.url);
        mystr=mystr.replace("[target]",this.target);
        if(this.childs.length>0){
            this.spanchilds="<span id='"+this.id+"_childs' onmouseover='mover(this.id,0)' onmouseout='mout(this.id,0)' class='child_block'>";
            for(i=0;i<this.childs.length;i++){
                this.spanchilds+=this.childs[i]+"<br>";
            }
            this.spanchilds+="</span>";
        }
        mystr+=this.spanchilds;
        return mystr;
    }