
var last=[];
var mnuTimer=0;
var mnuTimerSub=0;
var mnuTimerOver=[];
var subMoveX=3;
var subMoveY=2;
var hisSub=[];
var hisItem=[];
var hisParent=[];

function mnuShowSubApplyAcl(item,acllevel) {

    var ch,t;

    if (!(ch=item.childNodes))
        return;
    for (t=0;t<ch.length;t++) {

        var id;

        if (ch[t].id && ch[t].id.substring(0,4)=='acl@') {

            if (acllevel>=ch[t].id.substring(ch[t].id.indexOf('@')+1,99)) {

                showElement(ch[t],-1);
                ch[t].style.position='static';
            }
            else {

                showElement(ch[t],0);
                ch[t].style.position='absolute';
            }
        }

        mnuShowSubApplyAcl(ch[t],acllevel);
    }
}

function mnuShowSub(source_item,sub,parent,horizontal,flags,acllevel) {

    var table_width,el,div,x;

    el='.'+sub;
    el=el.substring(1,2);

    if (el<'0' || el>'9') {
        for(el=0;el<mnuMenus.length;el++)
            if (mnuMenus[el][2]==sub)
                break;
        if (el==mnuMenus.length)
            return;
        sub=el;
    }

    div=getElement("mnu"+sub);

    if (source_item.offsetParent)
        el=source_item;
    else
        el=getElement(source_item);

    if (!el || !div)
        return;

    if (flags & 2)
        mnuShowSubApplyAcl(div,acllevel);

    var doc,pageSX,pageSY,posX,posY,style,scrollbar,e,part,table;

    doc=document[document.compatMode=='CSS1Compat' ? 'documentElement':'body'];
    pageSX=bIe ? doc.scrollLeft+doc.clientWidth:pageXOffset+innerWidth;
    pageSY=bIe ? doc.scrollTop+doc.clientHeight:pageYOffset+innerHeight;
    style=mnuStyles[mnuMenus[sub][0]];

    scrollbar=bNs ? 16:0;

    x=0;
    e=el.offsetParent;
    posX=bNs4 ? el.pageX:el.offsetLeft;
    posY=bNs4 ? el.pageY:el.offsetTop;
    while(e && e.tagName.toLowerCase()!="body") {

        if (x<2 && e.tagName.toLowerCase()=="table") {

            table_width=e.offsetWidth;
            x++;
        }

        posX+=e.offsetLeft;
        posY+=e.offsetTop;
        e=e.offsetParent;
    };

    if (!(flags & 4)) {

        if (horizontal) {

            if ((posY+div.offsetHeight)>pageSY)
                posY=posY-div.offsetHeight;
            else
                posY=posY+el.offsetHeight+style[5];
        }
        else {

            var sy=flags&1?2*el.offsetHeight/3:subMoveY;
            part=scrollbar+(bOp && !bOp6 ? div.style:div.offsetHeight);
            if ((posY-style[4]+sy+part)>pageSY)
                posY=pageSY-part;
            else
                posY=posY-style[4]+sy;

            part=flags&1?2*el.offsetWidth/3:table_width-subMoveX-(style[4]+style[5])*2;
            if ((posX+div.offsetWidth+part+scrollbar)<pageSX)
                posX=posX+part;
            else
                posX=posX-div.offsetWidth+subMoveX;
        }
    }

    if (posX<0)
        posX=0;
    if (posY<0)
        posY=0;

    cbsItemPos_set(div,posX,posY);
    showElement(div,1);

    e=hisSub.length;
    hisSub[e]=sub;
    hisItem[e]=source_item;
    hisParent[e]=parent;
    setTimeout("mnuOver();",100);
}

function mnuOverItem(over,table,curr,sub,horizontal) {

    var t,a,b;

    while(table && table.tagName.toLowerCase()!="table")
        table=table.offsetParent;

    if (!table)
        return;

    if ((!last.length || last[0]!=table) && mnuTimerSub) {

        clearTimeout(mnuTimerSub);
        mnuTimerSub=0;
    }

    if (!over) {

        for (t=0;t<hisSub.length;t++) {

            if (hisItem[t]==table.id)
                return;
        }

        a=mnuStyles[mnuMenus[curr][0]][2];
        b=mnuStyles[mnuMenus[curr][0]][0];
    }
    else {

        mnuOver();

        a=mnuStyles[mnuMenus[curr][0]][3];
        b=mnuStyles[mnuMenus[curr][0]][1];
    }

    for (t=0;t<table.tBodies.length;t++) {

        var e,el;

        e=table.tBodies[t];
        if (e.tagName.toLowerCase()=="tbody") {

            for (var r=0;r<e.rows.length;r++) {
                for (var c=0;c<e.rows[r].cells.length;c++) {

                    if (e.rows[r].cells[c].id.substring(0,4)=='mnu@') {

                        var i,id;

                        el=e.rows[r].cells[c];
                        id=el.id.substring(4,el.id.length);

                        for (i=0;i<document.links.length;i++) {

                            if (document.links[i].id==("lnk@"+id)) {

                                document.links[i].style.color=a;
                                if (bNs4)
                                    document.links[i].style.bgcolor=b;
                                else
                                    document.links[i].style.background=b;
                            }
                        }

                        for (i=0;i<document.images.length;i++) {

                            if (document.images[i].id.substring(0,4)=="img@") {

                                el=document.images[i];
                                id2=el.id.substring(el.id.indexOf('@')+1,el.id.indexOf('*'));

                                if (id==id2) {

                                    if (over)
                                        f=el.id.substring(el.id.indexOf(':')+1,el.id.length);
                                    else
                                        f=el.id.substring(el.id.indexOf('*')+1,el.id.indexOf(':'));

                                    if (f!=el.src)
                                        el.src=f;
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    table.style.color=a;
    if (bNs4)
        table.style.bgcolor=b;
    else
        table.style.background=b;

    if (over) {

        if (hisSub.length>0 && hisSub[hisSub.length-1]!=curr && hisSub[hisSub.length-1]!=sub)
            mnuCloseSub(curr);

        if (last[0] && last[0]!=table)
            mnuOverItem(0,last[0],last[1],last[2],last[3]);

        last[0]=table;
        last[1]=curr;
        last[2]=sub;
        last[3]=horizontal;

        if (sub!=-1 && !(hisSub.length && hisSub[hisSub.length-1]==sub))
            mnuTimerSub=setTimeout("mnuShowSub(\""+table.id+"\","+sub+","+curr+","+horizontal+",0,0)",10);
    }
    else {

        last=[];
    }
}

function mnuOver() {

    if (mnuTimer)
        clearTimeout(mnuTimer);
    mnuTimer=0;
}

function mnuTimeStart() {

    if (mnuTimer && bOp)
        clearTimeout(mnuTimer);
    mnuTimer=setTimeout("mnuCloseAll()",500);
}

function mnuClick(target) {

    mnuCloseAll();

    var wndFlags='directories=1,location=1,menubar=1,resizable=1,scrollbars=1,status=1,toolbar=1';
    
    if (target.substring(0,4)=='new:')
        window.open(target.substring(4),'',wndFlags);
    else
    if (target.substring(0,11)=='javascript:')
        document.location.href=target;
    else
        document.location.href=target;
}

function mnuCloseSub(stop) {

    var t;

    for (t=hisSub.length-1;t>=0;t--) {

        if (hisSub[t]==stop)
            break;

        e=getElement("mnu"+hisSub[t]);
        showElement(e,0);

        e=getElement(hisItem[t]);
        hisItem[t]=-1;
        if (hisParent[t]!=-1)
            mnuOverItem(0,e,hisParent[t],-1,0);
    }

    hisSub2=[];
    while (t>=0) {

        hisSub2[t]=hisSub[t];
        t--;
    }

    hisSub=hisSub2;
}

function mnuCloseAll() {

    if (last[0]) {

         mnuOverItem(0,last[0],last[1],last[2],last[3]);
         last=[];
    }


    if (hisSub.length>0)
        mnuCloseSub(-2);
}


