function $(e){var d=document;if(typeof e=='string'){if(d.getElementById){e=d.getElementById(e);}else if(d.all){e=d.all[name];}else if(d.layers){e=d.layers[name];}}return e;}
function addOnLoad(f) {if(window.onload) {var o=window.onload;window.onload=function(){o();f();};}else{window.onload=f;}}
function getMouseX(e) { e = e ? e : window.event; return e.pageX ? e.pageX : (e.clientX ? e.clientX+document.body.scrollLeft+document.documentElement.scrollLeft : 0); }

var expandable, expander, expand_startWidth = 0, expand_startPos = 0, mouseIsUp = true, expand_heightAlso = false;
//set the initial position of the resize-bar
function expand_start(e){
  expand_startPos = getMouseX(e);
  expand_startWidth = parseInt(expandable.style.width.replace(/px/gi,''));
  mouseIsUp = false;
}

//reset the position of the resize-bar when the mouse moves while button down
function expand_update(e){
  if (!mouseIsUp) {
    var newWidth = expand_startWidth + getMouseX(e) - expand_startPos;
    newWidth = (newWidth<5?5:newWidth)+'px';
    expandable.style.width = newWidth;
    if (expandable.expandHeightAlso) {
      	expandable.style.height = expander.style.height = newWidth;
    }
  }
}

function create_expander(elem, expandHeightAlso) {
  expandable = $(elem);
  expandable.className += " expandable";
  expandable.expandHeightAlso = expandHeightAlso;
  expander = document.createElement('div');
  expander.className = 'expander clearfix';
  expander.style.height = parseInt(expandable.style.height.replace(/px/gi,'')) + 'px';
  expander.onmousedown = expand_start;
  expandable.parentNode.insertBefore(expander, expandable.nextSibling);
  var clear = document.createElement('br');
  clear.style.clear = 'both';
  expander.parentNode.insertBefore(clear, expander.nextSibling);
}

document.onmousemove = expand_update;
document.onmouseup = function() { mouseIsUp = true; };

function addNamedLink(link, e) {
  var a = document.createElement('A');
  e.parentNode.insertBefore(a, e).name = link;
  return 1;
}

function createRefLink(href) {
  var a = document.createElement('A');
  a.className = 'ref';
  a.href = '#'+href;
  return a;
}

addOnLoad(function makeToc() {
  var list = $('toc'), content = $('content'), all = content.getElementsByTagName('*');
  var reH = /^H[1-6]$/, reX = /[^A-Za-z_0-9]/g;
  var prevLvl = 0, l;
  for (var i = 0, e; (e = all[i]); i++) {
    if (e.tagName.match(reH)) {
      var lvl = parseInt(e.tagName.substr(1), 10), text = e.innerText ? e.innerText : e.textContent, link, prev = e.previousSibling;

      if (e.name) { i+=addNamedLink(link = e.name, e); }
      else if (prev && prev.tagName == 'A' && prev.name) { link = prev.name; }
      else { i+=addNamedLink(link = text.replace(reX, ''), e); }

      if (lvl > prevLvl) {      for (l = prevLvl; l < lvl; l++) { list = list.appendChild(document.createElement('UL')); } }
      else if (lvl < prevLvl) { for (l = lvl; l < prevLvl; l++) { list = list.parentNode; } }
      prevLvl = lvl;

      e.appendChild(document.createTextNode(" "));
      e.appendChild(createRefLink(link)).appendChild(document.createTextNode("\xB6"));
      list.appendChild(document.createElement('LI')).appendChild(createRefLink(link)).appendChild(document.createTextNode(text));
    }
  }
});

function openToc() { $('toc').className = 'open'; }
function closeToc() { $('toc').className = ''; }