/*
 *  $Id:$
 *
 *  2005-2006  Henning Manske / SUB Hamburg
 *
 */

var w3c = document.getElementById;
var opera = window.opera;
var ie  = document.all && !opera;

function dblock_init (text1, textOn, text2, textOff, text3) {
  var hash = window.location.hash.substr(1);
  var showAll;
  if (hash == 'all')
    showAll = true;
  else {
    _dbStatus = parseInt(hash);
    if (!_dbStatus || _dbStatus < 0)
      _dbStatus = 0;
  }
  var i = 0, el;
  while (el = document.getElementById('dbt' + i)) {
    el.className = 'dbt';
    el.onmouseover = function() { this.className = 'dbt-h' };
    el.onmouseout = function() { this.className = 'dbt' };
    el.onclick = function() {
      var num = this.id.substr(3);
      var dblock = document.getElementById('db' + num);
      if (dblock) {
        var p = Math.pow(2, num);
        var d2 = _dbStatus & p;
        if (d2) {
          _dbStatus &= ~p;
          dblock.style.display = 'none';
        }
        else {
          _dbStatus |= p;
          dblock.style.display = 'block';
        }
      }
      _dblock_update();
      window.location.hash = '#' + (_dbStatus == _dbAll ? 'all' : _dbStatus);
    };
    var dblock = document.getElementById('db' + i);
    var p = Math.pow(2, i);
    dblock.style.display = (showAll || _dbStatus & p) ? 'block' : 'none';
    i++;
  }
  _dbAll = Math.pow(2, i) - 1;
  if (showAll) _dbStatus = _dbAll;
  _dblock_update = function() {
    var el = document.getElementById('dbsw');
    if (el) {
      while (el.childNodes.length)
        el.removeChild(el.firstChild);
      el.appendChild(document.createTextNode(text1));
      var spanOn = document.createElement('span');
      spanOn.className = 'dbt';
      spanOn.onmouseover = function() { this.className = 'dbt-h' };
      spanOn.onmouseout = function() { this.className = 'dbt' };
      if (_dbStatus < _dbAll) {
        spanOn.onclick = function() { dblock_switch(1) };
        spanOn.appendChild(document.createTextNode(textOn));
        el.appendChild(spanOn);
        if (_dbStatus > 0)
          el.appendChild(document.createTextNode(text2));
      }
      if (_dbStatus > 0) {
        var spanOff = document.createElement('span');
        spanOff.className = 'dbt';
        spanOff.onmouseover = function() { this.className = 'dbt-h' };
        spanOff.onmouseout = function() { this.className = 'dbt' };
        spanOff.onclick = function() { dblock_switch(0) };
        spanOff.appendChild(document.createTextNode(textOff));
        el.appendChild(spanOff);
      }
      el.appendChild(document.createTextNode(text3));
    }
    stretch();
  };
  _dblock_update();
}



function dblock_switch (sw) {
  var display;
  if (sw) {
    window.location.hash = '#all';
    display = 'block';
    _dbStatus = _dbAll;
  }
  else {
    window.location.hash = '#';
    display = 'none';
    _dbStatus = 0;
  }
  var i = 0, dblock;
  while (dblock = document.getElementById('db' + i++))
    dblock.style.display = display;
  _dblock_update();
}



function mailto(n, s1, s2) {
  var el = document.getElementById('m' + n);
  if (el) {
    var s = s1 + '%40' + s2;
    var a = document.createElement('a');
    a.href = unescape('%6d%61%69%6c%74%6f%3a' + s);
    a.appendChild(document.createTextNode(unescape(s)));
    el.appendChild(a);
  }
}



function stretch() {
  var el = document.getElementById('c-frame');
  if (el) {
    if (!ie) el.style.height = 'auto';
    var cHeight = el.offsetHeight;
    var nHeight = document.getElementById('n').offsetHeight;
    var y = ie ? document.body.clientHeight : window.innerHeight;
    var rHeight = cHeight + 145 + 81;
    if (y > rHeight) {
      var newHeight = y - 145;
      if (newHeight < nHeight) newHeight = nHeight;
      el.style.height = (newHeight - 81 ) + 'px';
    }
    else if (y < rHeight) {
      if (nHeight > cHeight)
        el.style.height = (nHeight - 81) + 'px';
      else
        el.style.height = 'auto';
    }
  }
}



var nTop;

function nav_pos() {
  var el = document.getElementById('n');
  if (el) {
    if (!nTop) nTop = el.offsetTop;
    var y = ie ? document.body.scrollTop : window.pageYOffset;
    if (y > nTop) {
      if (ie) {
        setTimeout( function() {
          el.style.position = 'absolute';
          el.style.top = (y > nTop ? y : nTop) + 'px';
          el.style.left = '0';
          nav_pos();
        }, 250);
      }
      else {
        el.style.position = 'fixed';
        el.style.top = '0px';
      }
    }
    else
      el.style.position = 'static';
  }
}

window.onload = stretch;
window.onresize = stretch;
window.onscroll = nav_pos;
