// autoContentsGenerator.js
// Finds searchedForElements in a div and creates a contents menu of anchor links to jump to those sections
var debug = false;

var contentsMenuDiv = 'topanchormenu';
var contentDiv = 'content';
var jumpToTopLinkId = 'jumptotoplink';
var searchForElement = 'H2';

function buildContentsMenu()
{
    // get contentDiv object
    var contentObj = getObject(contentDiv);
    // get all searchForElements in contentDiv
    var elements = contentObj.getElementsByTagName(searchForElement);
    
    // get contentsMenu Object
    var contentsMenuObj = getObject(contentsMenuDiv);
    // make visible
    contentsMenuObj.style.display = 'block';
    
    var listItems = contentsMenuObj.getElementsByTagName('LI');
    var currContentsItem = listItems[0].getElementsByTagName('A')[0];
    
    // for each element
    for (var i = 0; i < elements.length; i++)
    {
        var currElement = elements[i];
        // if not the first element, insert a jump to top link before it
        if (i > 0)
        {
            var topLinkObj = getObject(jumpToTopLinkId);
            var topLinkObjClone = topLinkObj.cloneNode(true);
            currElement.parentNode.insertBefore(topLinkObjClone, currElement);
        }
        // create and insert an anchor reference before element
        // create
        var anchorRef = document.createElement('A');
        anchorRef.name = 'jumplink' + i;
        anchorRef.id = 'jumplink' + i;
        // insert
        currElement.parentNode.insertBefore(anchorRef, currElement);
        
        // add new entry to the contentsMenu
        currContentsItem.href = '#jumplink' + i;
        currContentsItem.hash = '#jumplink' + i;
        currContentsItem.innerHTML = currElement.innerHTML;
        
        // if not the last element, clone and add to contents menu
        if (i + 1 < elements.length)
        {
            var parent = currContentsItem.parentNode.parentNode; // because the A is within an LI
            var newListItem = currContentsItem.parentNode.cloneNode(true);
            
            currContentsItem = newListItem.getElementsByTagName('A')[0];
            parent.appendChild(newListItem);    
        }
    }
}


//create function, it expects 2 values.
function insertAfter(newElement,targetElement) 
{	
    // target is what you want it to go after. Look for this elements parent.	
    var parent = targetElement.parentNode; 	
    // if the parents lastchild is the targetElement...	
    if (parent.lastchild == targetElement) 
    {		
        // add the newElement after the target element.		
        parent.appendChild(newElement);		
    } 
    else 
    {		
        // else the target has siblings, insert the new element between the target and it's next sibling.		
        parent.insertBefore(newElement, targetElement.nextSibling);		
    }
}

// basic defensive function to get object
function getObject(id)
{
	if (document.getElementById)
	{
		if (document.getElementById(id))
		{	
			return document.getElementById(id);
		}
		else
		{
			trace('Element with id = ' + id + ' does not exist');
		}
	}
	else
	{
		trace('getElementbyId not supported');
	}
}

function trace(str) // debug function
{
	if (debug) alert(str);
}

function setOnLoad()
{
	if (window.onload != null) 
	{
		var currentLoadFunc = window.onload;
		window.onload = function()
				{
				    currentLoadFunc();

				    // setup functions here
				    buildContentsMenu();
				    //alert('setting up autoContentsGenerator');
				};        
	}
	else
	{
	    window.onload = function()
				{
				    // setup functions here
				    buildContentsMenu();
				    //alert('setting up autoContentsGenerator');
				};   
	}
}

setOnLoad();
