/**********************************************************************/
/* Automatically include a Table of Contents in your page.            */
/*                                                                    */
/* Instructions                                                       */
/* 1. Load this Javascript file - add this to <head>                  */
/*      <script language="JavaScript" src="toc.js"> </script>         */
/* 2. Optionally adjust the validTagList statement to include the     */
/*    tags that you want included in the TOC.                         */
/* 3. Call buildTOC function from body onload.                        */
/*	<body onload="buildTOC()">                                    */
/* 4. Optionally add the following in the body at the point	      */
/*    where you want the TOC to appear                                */
/*	<div id=tocContainer>                                         */
/*	</div>                                                        */
/*    If not specified TOC will be positioned immediately after <body>*/
/*                                                                    */
/*                                                                    */
/*                                                                    */
/* Verified on:                                                       */
/* - IE (Win) 6.0                                                     */
/* - Mozilla Firefox 1.0.6                                            */
/* - Opera 8.02 (except accessKey alt-t)                              */
/*                                                                    */
/* 30 Aug 2005  Stephen Morley (sbm@netspace.net.au) Created          */
/*              Passed to and accepted by www.javascript.source as    */
/*                smpTOC.html                                         */
/* 20 Sep 2005  Stephen Morley  Modify to support DOM standard        */
/*              Passed to www.javascript.source as smpTOC.html        */
/* 28 Sep 2005  Stephen Morley  Include TOC title in js (used to need */
/*                to be catered for in html).                         */
/* 28 Sep 2005  Stephen Morley  Include accessKey in js (used to need */
/*                to be catered for in html).                         */
/* 28 Sep 2005  Stephen Morley  Change the div that gives the TOC     */
/*                position from 'TOC' to 'tocContainer'.              */
/*                                                                    */
/*                                                                    */
/**********************************************************************/

/*Dynamically load in getObjects.js */
var script = document.createElement('script'); 
script.type = 'text/javascript'; 
script.src = 'getObjects.js'; 
document.getElementsByTagName('head')[0].appendChild(script); 




var stateTOC = 'ON';

function toggleTOC()
{

	if (stateTOC == 'ON') 
	{
		stateTOC = 'OFF'
		document.getElementById('TOC').style.display = "none";
	}
	else
	{
		stateTOC = 'ON'
		document.getElementById('TOC').style.display = "inline";
	}
}



function buildTOC()
{
	

	/*************************************/
	/*Get desired tags and store in array*/
	/*************************************/

	/*Define list of tags to include in toc*/
	/*Each valid tag must be bounded by spaces to satisfy the test below. The beginning & end '.'s merely stop leading & trailing whitespaces being removed.*/
	validTagList = '. H1 H2 H3 H4 H5 H6 .';	




	/*Get list of all tags & store in array. */
	/*DOM Level 1 Standard*/
	if (document.body.childNodes) {
  		allTags = getObjects();
  	}
	/*Microsoft proprietry intermediate DOM  */
  	else if (document.all) {
		allTags = document.all;
	}
	/*Netscape proprietry intermediate DOM   */
  	else if (document.layers) {
		alert('Netscape layers? What to do, what to do ....');
  	}
	


	var tagDetail = new Array(1);
	j = 0;
	aNum = 0;
	for(i = 0; i < allTags.length; i++) 
	{
		if (validTagList.indexOf(' '+allTags[i].tagName+' ') > 0)
		{
			/*Add an anchor as a child to the tag so that toc link to it*/
			aNum = aNum + 1;
			var aNode = document.createElement("A");
			aNode.id = 'Z'+aNum;
			allTags[i].appendChild(aNode);
			
			

			/*Keep relevant data fron the tag to use in toc*/
			tagDetail[j] = allTags[i].tagName + "|";

			tagDetail[j] = tagDetail[j]+aNum + "|";

			if (allTags[i].childNodes.length > 0) {
				tagDetail[j] = tagDetail[j]+allTags[i].childNodes[0].nodeValue
			} 
				;
			j = j + 1;
		}
	}
	if (j == 0) {	/*No desired tags found so just end out. */
		return;
	};

	
	/*************************************/
	/*Build toc                          */
	/*************************************/

	/*Determine position of TOC within the document. If    */
	/*div 'tocContainer has been specified, then we will   */
	/*use that, otherwise we will be the first node under  */
	/*<body>.                                              */
	tocContainerNode = document.getElementById('tocContainer');
	if (tocContainerNode == null) {
		bodyNode = document.getElementsByTagName('BODY')[0];
		var divNode = document.createElement("DIV");
		bodyNode.insertBefore(divNode,bodyNode.childNodes[0]);
		divNode.id = 'tocContainer';
		tocContainerNode = document.getElementById('tocContainer');
	}

	
	/*Create an access key alt-t for user to go back to TOC.*/
	var akNode = document.createElement("A");
	tocContainerNode.appendChild(akNode);
	akNode.href = '#tocTitle';
	akNode.title="AccessKey: t";
	akNode.accessKey="t";

	/*Create the 'Table of Contents' title                  */
	var spanNode = document.createElement("SPAN");
	tocContainerNode.appendChild(spanNode);
	spanNode.id = 'tocTitle';
	spanNode.innerHTML = 'Table of Contents';
	spanNode.title="Toggle TOC on/off";
	spanNode.onclick=toggleTOC;
	spanNode.style.cursor = "pointer";

	/*Create a separate div for the TOC contents that can be*/
	/*toggled on/off.                                       */
	var divNode = document.createElement("DIV");
	tocContainerNode.appendChild(divNode);
	divNode.id = 'TOC';

	/*Create unorderedlist that will form the TOC and link */
	/*it to the element TOC which gives it the position.   */
	var ulNode = document.createElement("UL");
	tocId = document.getElementById('TOC');
	tocId.appendChild(ulNode);


	/*Go through our discovered tags and add into UL       */
	for(i = 0; i < tagDetail.length; i++)
	{
		thisDetail = tagDetail[i].split("|");

		var liNode = document.createElement("LI");
		liNode.className = thisDetail[0];
		ulNode.appendChild(liNode);
		
		var aNode = document.createElement("A");
		aNode.className = thisDetail[0];
		aNode.href = '#Z'+thisDetail[1];
   		liNode.appendChild(aNode);
		aNode.innerHTML = thisDetail[2];

		/*alert('i='+i + ' tagName='+aNode.tagName + ' href='+aNode.href + ' className='+aNode.className + ' text='+aNode.innerText);*/
		
	}
}


