/*
 * Copyright (C) 2004-2005, Esben Høgh-Rasmussen
 */

/*
 * Check if we got a DOM-capable browser.
 */
var DOM=(document.createElement && document.getElementsByTagName);

/* Toggles the visibility state of an object.
 * The elements involved have the following ID's, where
 * # is a number:
 *  'toggle#' - The clicked link
 *  'onoff#'  - The element whose visibility is controlled
 *  'navimg#' - The image representing the controlling button
 */
function toggle() {
  if (!DOM || !this.id) return(false);
  num=this.id;
  num=num.substring(6,num.length);
  var self=this;
  /* var self=document.getElementById('toggle' + num); */
  var ctrl=document.getElementById('onoff' + num);
  var img=document.getElementById('navimg' + num);
  if (!img) { alert('No image'); return }
  if (!ctrl) { alert('No control'); return }
 /* Change visibility state.
  */
  if (ctrl.style.display=='none') {
    ctrl.style.display='block';
    if (img) {
      img.src='gfx24/sec_col.png';
      img.alt='Collapse';
    }
  } else {
    ctrl.style.display='none';
    if (img) {
      img.src='gfx24/sec_exp.png';
      img.alt='Expand';
    }
  }
 /* To work around a bug in some browsers, we
  * need to hide and show the topmost li-element
  * to update the layout :-(
  */
  var node=ctrl;
  var top=null;
  while (node) {
    if (node.nodeName=='UL' && node.className=='navlist') break;
    if (node.nodeName=='LI') top=node;
    node=node.parentNode;
  }
  if (top) {
    var tmp=top.style.display;
    top.style.display='none';
    top.style.display=tmp;
  }
  return(false);
}

/* Collapse the navigation tree when the page loads.
 *
 * All expandable links should start as simple
 * navigation separators to hide them from browsers
 * without JavaScript or DOM.
 *
 * The idea is that we get a fully expanded tree in
 * browsers that can't support the menu.
 */
window.onload=init;
function init() {
  if (!DOM) return;
  var x=document.getElementsByTagName('img');
  for (var i=x.length-1;i>=0;i--) {
    var id=x[i].getAttribute('id');
    if (!id || id.substring(0,6)!='navtog') continue;
    var num=id.substring(6,id.length);
    /* Create navigation link */
    var y=document.createElement('a');
    y.id='toggle'+num;
    y.onclick=toggle;
    y.href='#';
    /* Create and append image. */
    var z=document.createElement('img');
    z.id='navimg' + num;
    z.className='navsec';
    y.appendChild(z);
   /* Replace element with expand/collapse link and
    * pretend the user clicked the link to collapse it.
    */
    x[i].parentNode.replaceChild(y,x[i]);
    y.onclick();
  }
}
