var wDebugToC = false; // this can be set to true in the document to enable debugging info
function buildToC (){
   // deprecated - dummy function for compatibility
}
function buildPageContents (){
   var TocDiv = document.getElementById("ToC");
   if (!TocDiv) return;
   
 
   var linkref = 0;
   var ix = 0;
   var jx = 0; 
   var wLev = 0;
   var prevLev = 0;
   var wCurrLev = 1;
   var wElt = new Array();
   var firstHdr = true;
   var paraNo = new Array(0,0,0,0,0,0); // paraNo[0] = para nos for <h2> elts, paraNo[1] for <h3> etc. 
   var paraNumber = new String();
   var tempNode; // used to create new nodes to insert into document
//   var cntTitle = TocDiv.innerText ? TocDiv.innerText : TocDiv.textContent;
   if (TocDiv.innerText){
      cntTitle = TocDiv.innerText;
      TocDiv.innerText = "";
   }
   else {
      cntTitle = TocDiv.textContent;
      TocDiv.textContent = "";
   }

   // select the header tags out into a table 'wElt'
   var tagList = document.getElementsByTagName("*");
   for (ix = 0; ix < tagList.length; ix++) {
      if ("H2H3H4H5H6h2h3h4h5h6".indexOf(tagList[ix].tagName) != -1)
         wElt[jx++] = tagList[ix];
   }
   tempNode = document.createElement("a");
   tempNode.name = "PgC";
   tempNode.id = "PgC";
   TocDiv.appendChild(tempNode);
   tempNode = document.createElement("h2");
   tempNode.appendChild(document.createTextNode(cntTitle));
   TocDiv.appendChild(tempNode);

   var attachPt = TocDiv;   

   if (wDebugToC) alert (wElt.length + " headers");

   // go through table of header tags adding section numbers, anchor tags and building ToC
   for (ix = 0; ix < wElt.length; ix++)
   {
      wLev = wElt[ix].tagName.charAt(1); // 2 for <h2> etc.
      if (wDebugToC){
          alert ("tag (#" + ix + ") : " + wElt[ix].tagName + " Level: " +
            wLev  + " text: " + wElt[ix].childNodes[0].nodeValue);
      }
      // calculate and format paragraph number for ToC list and element headings
      paraNo [wLev - 2]++; // add 1 to the relevant paragraph number
      for (jx = wLev - 1; jx < 4; jx++){
         paraNo [jx] = 0;
      }
      paraNumber = paraNo [0];
      for (jx = 1; jx < wLev - 1; jx++){
         paraNumber += "." + paraNo [jx];
      }
      var paraNumTxt = document.createTextNode(paraNumber + " ");
      wElt[ix].insertBefore(paraNumTxt,wElt[ix].firstChild);
      // create anchor to link-to on element heading
      tempNode = document.createElement("a");
      tempNode.name = "x" + linkref;
      tempNode.id = "x" + linkref;
      wElt[ix].insertBefore(tempNode,wElt[ix].firstChild);

      while (wLev > wCurrLev){
         tempNode = document.createElement("ul");
         tempNode.style.listStyleType = "none";
         attachPt.appendChild(tempNode);
         attachPt = tempNode;
         wCurrLev++;
      }
      while (wLev < wCurrLev) {
         tempNode = attachPt;
         attachPt = tempNode.parentNode;
         wCurrLev--;
      }

      // the following inserts the link AFTER the heading (wElt[ix]) 
      wElt[ix].parentNode.insertBefore(ToCLink (),wElt[ix].nextSibling);
      
      // add heading to ToC list with link back to heading
      var listNode = document.createElement("li");
      attachPt.appendChild(listNode);
      tempNode = document.createElement("a");
      tempNode.href = "#x" + linkref;
      listNode.appendChild(tempNode);
      if (wElt[ix].innerText)
         tempNode.appendChild(document.createTextNode(wElt[ix].innerText));
      else
         tempNode.appendChild(document.createTextNode(wElt[ix].textContent));

      linkref++;
      prevLev = wLev; 
   }
   document.getElementById("content").appendChild(ToCLink ());

   function ToCLink ()
   {
      var imgNode = document.createElement("img");
      imgNode.src = "../cradur/styles/img/ToC.gif" ;
      //imgNode.src = "../cradur/styles/img/NavBarNext.gif" ;
      imgNode.style.border = "0px";
      var tempNode = document.createElement("a");
      tempNode.href = "#PgC" ;
      tempNode.title = cntTitle ;
      tempNode.alt = cntTitle ;
      tempNode.style.fontSize = "8pt";
      tempNode.appendChild(imgNode);
      return tempNode;
   }
}
//-----------------------------------------------------------------------------
function buildDynaLinks()
{
   addNavListener(document,"keyup",keyPressListner);
}
//------------------------------------------------------------------
function expandPic(){
   resizePic(+1);
}
function contractPic(){
   resizePic(-1);
}
function resetPic(){
   resizePic(0);
}
function resizePic(scale){
   var imgMin = 0.2;
   var imgDiv = document.getElementById("showGalImg");
   var currWidth = imgDiv.offsetWidth;
   imgDiv.style.width = "100%";
   var fullWidth = imgDiv.offsetWidth;
   var widthRatio = currWidth/fullWidth;
   if (scale < 0 & widthRatio > imgMin){
      widthRatio = Math.max (widthRatio/1.2 , imgMin);
   }
   else if (scale > 0 & widthRatio < 1){
      widthRatio = Math.min (widthRatio*1.2 , 1);
   }
   else if (scale == 0){
      widthRatio = 0.5;
   }
   var widthPct = Math.floor (widthRatio * 100) + "%";
   imgDiv.style.width = widthPct;
}
//------------------------------------------------------------------
function addNavListener (elem,evType,fn)
{
   if (elem.addEventListener)
      elem.addEventListener(evType,fn,false);
   else if (elem.attachEvent)
      elem.attachEvent('on' + evType,fn);
   else
      elem['on' + evType] = fn;
}
// -----------------------------------------------------------------------------
function keyPressListner(evt)
{
   var keyNode;
   var wEvent = (window.event ? window.event :  evt );
   var keyNum = wEvent.keyCode;
   //alert ("key pressed = " + keyNum);
   if ( keyNum == 118 && wEvent.ctrlKey == 1) // ctrl-F7
      keyNode = document.getElementById("nv2NavBarHome");
   else if ( keyNum == 119 && wEvent.ctrlKey == 1) // ctrl-F8
      keyNode = document.getElementById("nv2NavBarIndx");
   else if ( keyNum == 120 && wEvent.ctrlKey == 1) // ctrl-F9
      keyNode = document.getElementById("nv2idxPgKey");
   else if ( keyNum == 37 && wEvent.ctrlKey == 1)  // ctrl- <-
      keyNode = document.getElementById("nv2NavBarPrev");
   else if ( keyNum == 38 && wEvent.ctrlKey == 1)  // ctrl-uparrow
      keyNode = document.getElementById("nv2NavBarMenu");
   else if ( keyNum == 39 && wEvent.ctrlKey == 1)  // ctrl- ->
      keyNode = document.getElementById("nv2NavBarNext");
   else if ( keyNum == 40 && wEvent.ctrlKey == 1)  // ctrl- downarrow
      keyNode = document.getElementById("nv2FirstChild");
   else if ( keyNum == 191 && wEvent.ctrlKey == 1)  // ctrl- ?
      keyNode = document.getElementById("nv2NavBarHelp");
//   else if (wEvent.ctrlKey == 1)  alert (keyNum);

   if (keyNode && keyNode.onclick)
   {
	  event = evt; 
      cmd = keyNode.onclick.toString().substr(keyNode.onclick.toString().indexOf("{"));
      eval (cmd);
   }
}
function expCont (item,event){
   if (!event) var event = window.event;
      event.cancelBubble = true;
   if (event.stopPropagation) event.stopPropagation();

   wClass = item.className.split(" ");
   for (ix=0; ix < wClass.length; ix++){
	   wClass[ix] = (wClass[ix] == "contract") ? "expand" : 
		   (wClass[ix] == "expand") ? "contract" : wClass[ix];
   }
   item.className = wClass.join(" ");
}
//------------------------------------------------------------------
function getMapState()
{
   var stateString = "";
   var siteMapTable = document.getElementById("navigMenuFixed");
   if (siteMapTable)
   {
      var mapButtons =   siteMapTable.getElementsByTagName("li");
      for (ix = 0; ix < mapButtons.length; ix++)
      {
         if (mapButtons[ix].className.indexOf("contract") > -1 )
            stateString += "0";
         else if (mapButtons[ix].className.indexOf("expand") > -1)
            stateString += "1";
      }
   }
   else
      stateString = "*";
   return (stateString);
}
function yLoadPage(node,event){
   //alert ("yLoadPage");
   node.href += "&mapstate=" + getMapState();
   if (!event) var event = window.event;
      event.cancelBubble = true;
   if (event.stopPropagation) event.stopPropagation();

}
function xLoadPage(wHref,event){
   if (!event) var event = window.event;
      event.cancelBubble = true;
   if (event.stopPropagation) event.stopPropagation();

   var url = wHref;
   var mapState = getMapState()
   if (mapState != "*"){
      url +=  "&mapstate=" + getMapState();
   }
   window.top.location = url;
}
function loadScripts(){} // empty function can be overwritten in localJS

function slideShow (iname) {
   this.picIx = 0;
   this.fader;
   this.changer;
   this.opacity = 10;
   this.dir = "null";
   this.pics = "null";
   this.toggleButton = document.getElementById(iname+"Toggle");
   this.slideScreen = document.getElementById(iname+"SlideShow");
   var layers = this.slideScreen.getElementsByTagName("div");
   this.foregroundFrame = layers[0];
   this.backgroundFrame = layers[1];
   this.foregroundImg = this.foregroundFrame.getElementsByTagName("img")[0];
   this.backgroundImg = this.backgroundFrame.getElementsByTagName("img")[0];
   this.autoRun;
   this.changeInterval = 10;

   this.foregroundFrame.style.zIndex = 1;
   this.backgroundFrame.style.zIndex = 0;

   this.forwardFunc = iname+".changeForward()";
   this.backwardFunc = iname+".changeBackward()";
   this.startAuto = iname+".showSlidesAutoStart()";
   this.fadeFunc = iname+".fadeSlide()";
 
   this.changeForward = function (){
      this.picIx =  this.picIx < this.pics.length - 1 ? this.picIx + 1 : 0 ;
      this.backgroundImg.src = this.dir + this.pics[this.picIx];
      //document.getElementById("bgPic").value=this.backgroundImg.src;
      clearInterval(this.fader); // stop fader
      this.fader=setInterval(this.fadeFunc,200);
   }

   this.changeBackward = function (){
      this.picIx =  this.picIx > 0 ? this.picIx - 1 : this.pics.length - 1 ;
      this.backgroundImg.src = this.dir + this.pics[this.picIx];
      //document.getElementById("bgPic").value=this.backgroundImg.src;
      clearInterval(this.fader); // stop fader
      this.fader=setInterval(this.fadeFunc,200);
   }

   this.fadeSlide = function () {   
      this.opacity -=2;
      //document.getElementById('txt').value = this.opacity;
      //if (this.opacity < 0) this.opacity = 0.1;
      //this.redisplaySlide();
      if (this.opacity <= 0) {// completely faded
         clearInterval(this.fader); // stop fader
         // bring slide from background to foreground:
         this.foregroundImg.src = this.backgroundImg.src
         //document.getElementById("fgPic").value=this.foregroundImg.src;
         this.opacity = 10;
      }
      this.redisplaySlide();
   }   

   this.redisplaySlide = function () {
	   if (typeof this.foregroundFrame.style.opacity == "string"){
	      this.foregroundFrame.style.opacity = this.opacity/10;
	   }
	   else {
	      this.foregroundFrame.style.filter = "alpha(opacity=" + (10*this.opacity) + ")";
	   }
   }

   this.toggleAuto = function (){ 
      if (this.autoRun){
         this.stopAuto();
      }
      else {
         this.changeForward();  
         this.showSlidesAutoRun();
      }       
   }

   this.showSlidesAutoRun = function (){
      if (this.toggleButton){	   
         this.toggleButton.value = " | | ";
      }
      this.autoRun = true;
      this.changer=setInterval(this.forwardFunc,this.changeInterval*1000);
   }

   this.showSlidesAuto = function (){
      if (this.toggleButton){	   
          this.toggleButton.value = " | | ";
      }
      this.changer=setInterval(this.startAuto,2000);
   }

   this.showSlidesAutoStart = function (){
       clearInterval(this.changer); // stop changer
       this.changeForward();  
       this.showSlidesAutoRun();
   }


   this.stopAuto = function (){
      if (this.toggleButton){	   
         this.toggleButton.value = ">>";
      }
      this.autoRun = false;
      clearInterval(this.changer); // stop changer
      //clearInterval(this.fader); // stop changer
   }      

   this.setDir = function (dir){
      this.dir = dir;
      if (this.pics != "null")
      this.foregroundImg.src = this.dir + this.pics[this.picIx];
      this.backgroundImg.src = this.dir + this.pics[this.picIx];
   }

   this.setPics = function (pics){
      this.pics = pics;
      if (this.dir != "null")
         this.foregroundImg.src = this.dir + this.pics[this.picIx];
         this.backgroundImg.src = this.dir + this.pics[this.picIx];
   }
   
   this.manualBackward = function() {
      if (this.autoRun){
         this.stopAuto();
      }
      this.changeBackward ();   
   }   

   this.manualForward = function() {
      if (this.autoRun){
         this.stopAuto();
      }
      this.changeForward  ();  
   }   

   this.setInterval = function(wInterval) {
      this.changeInterval = wInterval < 5 ? 5 : wInterval;
   }

}   
// --- onload registration ------------------------------------
// --- David Flanagan - JavaScript The Definitive Guide (O'Reilly) ex 17.7
function runOnLoad(wfunc){
   if (runOnLoad.loaded){
      wfunc();
   }
   else {
      runOnLoad.funcs.push(wfunc);
   }
}
runOnLoad.funcs = [];
runOnLoad.loaded = false;
runOnLoad.run = function (){
   if (!runOnLoad.loaded){
      for (var ix = 0 ; ix < runOnLoad.funcs.length; ix++){
         try { runOnLoad.funcs[ix](); }
         catch (exp) {/* exception in one func shouldn't stop the others */}
      }
      runOnLoad.loaded = true;
      delete runOnLoad.funcs;
      delete runOnLoad.run;
   } 
}
// --- register runOnLoad.run as onload function
if (window.addEventListner){
   window.addEventListner("load",runOnLoad.run, false);
}
else if (window.attachEvent){
   window.attachEvent("onload",runOnLoad.run);
}
else {
   window.onload = runOnLoad.run;
}
//--- now register the functions to run on load:  
runOnLoad(buildDynaLinks);
runOnLoad(buildPageContents);

