/* Author: Mihai Bazon, September 2002
 * http://students.infoiasi.ro/~mishoo
 *
 * Table Of Content generator
 * Version: 0.5
 *
 * Feel free to use this script under the terms of the GNU General Public
 * License, as long as you do not remove or alter this notice.
 */

 /* modified by Troy D. Hanson, September 2006. License: GPL */
 /* modified by Stuart Rackham, October 2006. License: GPL */
 /* modified by Pascal Rapaz, March 2007. License: GPL 
  *   Added Scroll to move in correct position when the page contain a top banner.
  *     To change the banner size, just replace the value of 'scrollStep' in
  *     generateToc method.
  */

function getText(el) {
  
  var text = "";
  
  for (var i = el.firstChild; i != null; i = i.nextSibling) {
    if (i.nodeType == 3 /* Node.TEXT_NODE */) {// IE doesn't speak constants.
      text += i.data;
    } else if (i.firstChild != null) {
      text += getText(i);
    } //end if
  } //end for
  
  return text;
} //end getText

function TocEntry(el, text, toclevel) {
  
  this.element = el;
  this.text = text;
  this.toclevel = toclevel;
} //end TocEntry

function tocEntries(el, toclevels) {

  var result = new Array;
  var re = new RegExp('[hH]([2-'+(toclevels+1)+'])');

  // Function that scans the DOM tree for header elements (the DOM2
  // nodeIterator API would be a better technique but not supported by all
  // browsers).
  var iterate = function (el) {

    for (var i = el.firstChild; i != null; i = i.nextSibling) {

      if (i.nodeType == 1 /* Node.ELEMENT_NODE */) {
        var mo = re.exec(i.tagName)
        if (mo) {
          result[result.length] = new TocEntry(i, getText(i), mo[1]-1);
		} // end if

        iterate(i);
      } //end if
    } //end for
  } //end iterator
  
  iterate(el);
  return result;
} //end tocEntries

// This function does the work. toclevels = 1..4.
function generateToc(toclevels) {

	generateToc(toclevels, 0);
} //end 

function generateToc(toclevels, bannerSize) {


  //set value with banner size
  var scrollStep = -140;

  var toc = document.getElementById("toc");
  var entries = tocEntries(document.getElementsByTagName("body")[0], toclevels);
  for (var i = 0; i < entries.length; ++i) {
    var entry = entries[i];
    if (entry.element.id == "") {

      entry.element.id = "toc" + i;
    } //end if
    
    var a = document.createElement("a");
    a.href = "#" + entry.element.id;
    
    a.onclick = function() { isClicked(1); };
    a.onmouseout = function() { scroll(scrollStep); };
    
    a.appendChild(document.createTextNode(entry.text));
    var div = document.createElement("div");
    div.appendChild(a);
    div.className = "toclevel" + entry.toclevel;
    toc.appendChild(div);
  } //end for
} //end generateToc

/**
 * state: 0 = not clicked, 1 = clicked
 */
function isClicked(state) {
  clicked = state;
} //end isClicked

/**
 * step: vertical step (in pixel) to move
 */
function scroll(step) {
  var ie = document.all;
  
  var crtScrollYPos = ie ? document.documentElement.scrollTop : window.pageYOffset;
  var docHeight = ie ? document.body.scrollHeight : document.height;
  var screenHeight = screen.height;
  
  if (clicked == 1 && crtScrollYPos < (docHeight - screenHeight - 2*step)) {
    window.scrollBy(0, step);
    isClicked(0);
  } //end if
} //end scroll
