﻿/* Table of Contents Generator
 *
 * Checks for two div ID's 'toc' and 'tocd' if both of those
 * elements do not exist, the function exits.
 * 'toc' is the container where the generated TableOfContents
 * outputs to. 'tocd' is the parent node of all the header elements
 * that will make up the content.
 */

function maketoc() {
    var container = document.getElementById('toc');
    var mNode = document.getElementById('tocd');
    if (!container || !mNode) {return;}
    
    var sections = [];
    var allNodes = [];
    var numbers = [0,0,0,0,0,0];
    var child;
    
    //Add Headers to 'sections' Array.
    //alert('FirstLoopEnter');
    for (child = mNode.firstChild; child != null; child = child.nextSibling) {
        allNodes.push(child);
        if (child.nodeType != 1 /*ELEMENT*/) {continue;}
        if (child.tagName == 'P') {continue;}
        if ( (child.tagName.length==2) && (child.tagName.charAt(0)=='H') ) {
            sections.push(child);
        }
    }
    //alert('FirstLoopExit');
    var parsedHeaderLevel = 0;
    var sectionString = "";
    
    //alert('SecondLoopEnter');   
    for (var i = 0; i < sections.length; i++) {
        
               
        parsedHeaderLevel = parseInt(sections[i].tagName.charAt(1));
        
        numbers[parsedHeaderLevel -1] += 1;
        for (var x = parsedHeaderLevel; x < 6; x++) {
            numbers[x] = 0;
        }
        
        sectionString = numbers[0];
        for (var x = 1; x < parsedHeaderLevel; x++) {
            sectionString += "." + numbers[x];
        }
        //document.writeln("[" + sectionString + "]<br/>");
        //document.writeln("[" + numbers[0] + " . " + numbers[1] + " . " + numbers[2] + " . " + numbers[3] + " . " + numbers[4] + " . " + numbers[5] + "]<br/> ");
        
        //TOC Entry
        var div = document.createElement('div');
        /* This Worked, but wouldn't style CSS wise in IE
        div.setAttribute('class','TOCTEntry ' + 'TOCTLevel' + parsedHeaderLevel);
        */
        div.className = "TOCTEntry " + "TOCTLevel" + parsedHeaderLevel;
        
        var link = document.createElement('a');
        /* This Worked, but wouldn't style CSS wise in IE
        link.setAttribute('id','TOCT' + sectionString);
        link.setAttribute('name','TOCT' + sectionString);
        link.setAttribute('href','#TOCH' + sectionString);
        */
        
        link.id = "TOCT" + sectionString;
        link.name = "TOCT" + sectionString;
        link.href = "#TOCH" + sectionString;
        /* HIGHLIGHT */
        //link.onclick = function() {new Effect.Highlight('TOCHighlight' + sectionString, {startcolor: '#FF0000', endcolor: '#FFFFFF', restorecolor: '#FFFFFF' });return true;};
        link.onclick = assignEvent(sectionString);
        
        link.innerHTML = sections[i].innerHTML;
                
        var span = document.createElement('span');
        span.innerHTML = sectionString + " ";
        
        div.appendChild(span);
        div.appendChild(link);
        container.appendChild(div);
       
       //If TOC is over 30 list then link to each element in the list
       //otherwise if its a small TOC then just link to the top element
       //Every time.
       
        var isBigList = "";
        if (sections.length < 30) {
            isBigList = "#TOCT1";
        } else {
            isBigList = "#TOCT" + sectionString;
        }
        
        //Super cheap way to do it, but it works.
        sections[i].innerHTML = '<span class="TOCHEntry TOCHLevel' + parsedHeaderLevel + '">' + sectionString + ' </span><a name="TOCH' + sectionString + '" id="TOCH' + sectionString + '" href="' + isBigList + '" class="hLinkBack">' + sections[i].innerHTML + '</a>';
                
        div = document.createElement('div');
        //div.className = "shift"; /* Testing */
        div.id = "TOCHighlight" + sectionString;
        div.name = "TOCHighlight" + sectionString;
        
        mNode.appendChild(sections[i]);
        var add = false;
        for (var x = 0; x < allNodes.length; x++) {
            if (allNodes[x] == sections[i]) {
                add = true;
            } else if (add && ((allNodes[x].nodeType == 1) && allNodes[x].tagName.charAt(0)=='H')) {
                add = false;
            }
            
            //Add only SubContent
            //if (add && (allNodes[x] != sections[i])) { div.appendChild(allNodes[x]);}
            
            //Add Header & SubContent
            if (add) { div.appendChild(allNodes[x]); }
        }
        mNode.appendChild(div);
        
        
    }
    //alert('SecondLoopExit');
}

//Helper fuction to escape 'Event Capture Scope Issues'
function assignEvent(i) {
    return function() {
        new Effect.Highlight('TOCHighlight' + i, {duration: '3', startcolor: '#E6E6E6', endcolor: '#FFFFFF', restorecolor: '#FFFFFF' });
        return true;
        };
}

if (window.addEventListener) {
	window.addEventListener("load", maketoc, false);
} else if (window.attachEvent) {
	window.attachEvent("onload", maketoc);
}