// Find an object 
function MM_findObj(n, d) { //v4.01
  var p,i,x;  
  if(!d) d=document; 
  if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n];
  for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++)
    x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n);
  return x;
}

// Get window dimension, default is height.
function P7_getWindowDimension(the_dimension){ //v2.3 by PVII
  var height=0,width=0,return_value=0;
  if(window.innerWidth){
        height=window.innerHeight;width=window.innerWidth;
    }else if(document.body){
        height=document.body.clientHeight;width=document.body.clientWidth;
        if(document.body.offsetHeight==height&&document.documentElement&&document.documentElement.clientHeight){
            height=document.documentElement.clientHeight;}
        if(document.body.offsetWidth==width&&document.documentElement&&document.documentElement.clientWidth) {
            width=document.documentElement.clientWidth;}
    }
    return_value=(the_dimension=="width")?width:height;
    return return_value;
}

// Fix for Opera
function P7_OpResizeFix(a) { //v1.1 by PVII
if(!window.opera){return;}if(!document.p7oprX){
 document.p7oprY=window.innerWidth;document.p7oprX=window.innerHeight;
 document.onmousemove=P7_OpResizeFix;
 }else{if(document.p7oprX){
  var k=document.p7oprX-window.innerHeight;
  var j=document.p7oprY - window.innerWidth;
  if(k>1 || j>1 || k<-1 || j<-1){
  document.p7oprY=window.innerWidth;document.p7oprX=window.innerHeight;
  location.reload();}}}
}
P7_OpResizeFix();

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

// Initialize the menu (called upon page load).
function P7_setTabBar(){ //v0.8 beta script by PVII

 //     i       : counter
 //     g       : document element being handled
 //     gg      : either `g` (if `document.layers`) or `g.style`
 //     menu_id : id of menu element
 //     tu      : ?? not referenced elsewhere
 //     args    : arguments to `P7_setTabBar`
 var i,g,gg,menu_id,tu,args=P7_setTabBar.arguments;

 // P7TabProp
 //
 // An array of the arguments to `P7_setTabBar`. On the first page, they
 // are: "1,9,2,'s13','sms2'". Meanings: 
 //     1      : how-manyth topmenu we're at now
 //     9      : number of menu items to look for per page (the largest
 //              of len(topmenus) and len(submenus) ..)
 //     2      : ??
 //     's13'  : the ID of the current page's submenu item.
 //     'sms2' : a style defined in `v4m2.css` and in `v5m2.css`
 P7TabProp=new Array();
 for(i=0;i<args.length;i++){P7TabProp[i]=args[i];}
 //DBG alert('P7TabProp: '+P7TabProp);

 // `p7TabBar_topmenus`, `p7TabBar_submenus`: Arrays of menu divs.
 p7TabBar_topmenus = new Array();
 p7TabBar_submenus = new Array();
 for(i=0;i<P7TabProp[1];i++) // for the number of toplevel items

    {menu_id="p7topmenu"+(i+1); // name the topmenu
    if((g=MM_findObj(menu_id))!=null) // find it
        {p7TabBar_topmenus[i]=menu_id;} // remember it
    else{alert('P7TabBar:missing topmenu:'+menu_id);return;}

    // Look for submenus on this page
    menu_id="p7submenu"+(i+1); // name the submenu
    if((g=MM_findObj(menu_id))!=null)      // find it
        {p7TabBar_submenus[i]=menu_id;     // remember it
        // If the document has layers (i.e. we have a 'recent'
        // browser?), `gg` is the object, otherwise it's the object's
        // CSS `style`.
        gg=(document.layers)?g:g.style;
        gg.visibility="hidden";} // initialize all submenus as invisible
    else{alert('P7TabBar:missing submenu:'+menu_id);return;}
    //DBG alert('p7TabBar_submenus: '+p7TabBar_submenus); 
    }

 // `P7TabH` is a 750x200 px layer at 0,0. Mouseover triggers
 // `P7_trigTabBar`. It is always hidden.
 //
 g=MM_findObj('P7TabH');
 gg=(document.layers)?g:g.style;gg.visibility="hidden";

 // P7TabProp[3] = 's13'
 // P7TabProp[4] = 'sms2'
 // 
 // If the submenuitem with id 's13' is found, 
 // if the browser supports `g.className`, 
 // then the style of `P7TabH` is set to 'sms2'.
 // TODO: I don't know why it's set to 'sms2'.
 if((g=MM_findObj(P7TabProp[3]))!=null){
    if(g.className){g.className=P7TabProp[4];}
    }
 document.P7TabBar=true;
}

// Called upon menu mouseover.
function P7_trigTabBar(moused_topmenu){ //v0.8 beta script by PVII

 // check if TabBar was successfully initialized.
 if(!document.P7TabBar){return;}

 // P7TabProp[0] = 1 on the 1st page, 2 on the 2nd ..
 var i,g,current_topmenu_nr=P7TabProp[0]-1,tF=false;

 // Hide all inactive submenus
 for(i=0;i<p7TabBar_submenus.length;i++){
    g=MM_findObj(p7TabBar_submenus[i]);gg=(document.layers)?g:g.style;
    // If this topmenu is active, show its submenu
    if(p7TabBar_topmenus[i]==moused_topmenu){
        tF=true;gg.visibility="visible";
        //DBG g=MM_findObj('P7TabH');
        //DBG gg=(document.layers)?g:g.style;gg.visibility="visible";
        }
    else{gg.visibility="hidden";}
 }

 // If `!tF` here, display the submenu for the current page.
 if(!tF){
    g=MM_findObj(p7TabBar_submenus[current_topmenu_nr]);
    gg=(document.layers)?g:g.style;
    gg.visibility="visible";
    //DBG g=MM_findObj('P7TabH');gg=(document.layers)?g:g.style;gg.visibility="hidden";
    }
}

function P7_styleFixA(tLay1,theWidth1,tLay2,theWidth2,theMargin){ //v0.4 beta script by PVII
 var w,totW,tL,tL2,doc,args=P7_styleFixA.arguments;w=P7_getWindowDimension("width");
 P7STXA=new Array();for(var i=0;i<args.length;i++){P7STXA[i]=args[i];}
 totW=theWidth1+theWidth2+theMargin;tL=parseInt((w-totW)/2);
 tL=(tL<=0)?1:tL;tL2=tL+theWidth1+theMargin;
 doc="<style>\n#"+tLay1+"{left: "+tL+"px}\n#"+tLay2+"{left: "+tL2+"px}\n</style>";
 document.P7StA=true;document.write(doc);
}
function P7_styleRR(){ //v0.4 beta script by PVII
 var g,gg,pa,w,totW,tL,tL2;if(document.layers){MM_reloadPage(false);
 }else{if(document.P7StA){pa=(window.opera)?'':'px';
 w=P7_getWindowDimension("width");totW=P7STXA[1]+P7STXA[3]+P7STXA[4];
 tL=parseInt((w-totW)/2);tL=(tL<=0)?1:tL;tL2=tL+P7STXA[1]+P7STXA[4];
 if((g=MM_findObj(P7STXA[0]))!=null){g.style.left=tL+pa;}
 if((g=MM_findObj(P7STXA[2]))!=null){g.style.left=tL2+pa;}}}
}
