/*
   Author: Edgar A Ibarra Jr
   Date:   07-04-08

   Filename: trinidad.js

   Global Variables:
   sections
      An array contain the HTML elements used as section headings in Calvin's work on the Trinity in Spanish

   Functions List:
   levelNum(node)
      Returns the level number of the object node. If the object node does not
      represent a section heading, the function returns the value -1.

   eventSource(e)
      Returns the source of an event under either the W3C or Internet Explorer
      event models

   makeTOC()
      Generate a table of contents as a nested list for the contents of the "doc" 
      element within the current Web page. Store the nested list in the "toc"
      element.

   createList()
      Goes through the child nodes of the "doc" element searching for section headings.
      When it finds a section heading, a new entry is added to the table of contents

*/



var sections = new Array("h3");

function levelNum(node) {
   for (var i=0; i<sections.length; i++) {
      if (node.nodeName==sections[i].toUpperCase()) return i;
   }
   return -1;
}

function eventSource(e) {
   var IE = document.all ? true:false;
   var DOM = document.addEventListener ? true: false;
   if (IE) return event.srcElement;
   else if (DOM) return e.currentTarget;
}

function makeTOC() {
   TOC = document.getElementById("toc");
   TOC.innerHTML="<h2>Índice</h2>";
   TOCList = document.createElement("ul");  //changed to ul from ol
   TOC.appendChild(TOCList);

   tdcDOC = document.getElementById("main");  //changed trinidadDOC to tdcDOC
   createList(TOCList);
}

function createList(list){
   var prevLevel = 0;
   var headNum = 0;
//changed trinidadDOC to tdcDOC
   for (var n=tdcDOC.firstChild; n!=null; n=n.nextSibling){
  	nodeLevel = levelNum(n);
	if(nodeLevel != -1){

	   //insert ids for the section headings
	   headNum++;
	   if (n.id == ""){
	      sectionId = "head"+headNum;
	      n.id = sectionId;
	   }
	   else sectionId = n.id;

	   listItem = document.createElement("li");
	   listItem.id="TOC"+sectionId;

	   linkItem = document.createElement("a");
	   linkItem.innerHTML = n.innerHTML;
	   linkItem.href="#"+sectionId;

	   listItem.appendChild(linkItem);

	   if (nodeLevel == prevLevel){
	      //append listitem to the list
	      list.appendChild(listItem);
	   }
	   else if (nodeLevel > prevLevel){
	      //append listitem to a new list
 	      //change the prevLevel value
	      newList = document.createElement("ul");  
	      newList.appendChild(listItem);
 	      list.appendChild(newList);
	      list = newList;
	      prevLevel = nodeLevel;
           }
	  
 	   else if (nodeLevel < prevLevel){
	      //append listitem to the list of that level
	      //change the prevLevel value
	      levelUp = prevLevel - nodeLevel;
	      for (j=1; j<=levelUp; j++) list=list.parentNode;
	      list.appendChild(listItem);
	      prevLevel = nodeLevel;
	   }
	}
  }
}
