//-------------------------------------------------------
// RePublico JavaScipt TOC routines
// (c) 2001, RepubliCorp
// Version 1.2
// Author: John Bradnam
//-------------------------------------------------------

var oSel       = null;
var oPrevSel   = null;
var oBtnDown   = null;
var oRootFrame = null;

//-------------------------------------------------------
//  Shared routines
//-------------------------------------------------------

function urlToIndex(sUrl) {
  var wIndex = -1;
  var wFil   = sUrl.lastIndexOf("/");
  var wExt   = sUrl.lastIndexOf(".");
  var wAnc   = sUrl.lastIndexOf("#");
  if (wAnc != -1)
    var sExt = sUrl.substr(wExt,wAnc - wExt);
  else
    var sExt = sUrl.substr(wExt);
  var sName  = sUrl.substr(wFil+1,wExt - wFil - 1);
  if (sExt != sRpoExt)
    sName = sName + sExt;
  wIndex   = 0;
  while ((wIndex < wTocItems) && (aRpoToc[wIndex].substr(0,aRpoToc[wIndex].indexOf("|")) != sName))
    wIndex = wIndex + 1;
  if (wIndex == wTocItems)
    wIndex = -1;
  return wIndex;
} //urlToIndex

function mouseonText() {
  var oPar = event.srcElement;
  if (event.type == "mouseover") {
    if (oPar.style.backgroundColor != sSelBack)
      oPar.style.color = sOvrFore;
    oPar.style.textDecoration = "underline";
  }
  else {
    if (oPar.style.backgroundColor != sSelBack)
      oPar.style.color = sTxtFore;
    oPar.style.textDecoration = "none";
  }
} //mouseonText
    
function clickTopic() {
  var sWord = null;
  var oPar  = event.srcElement;
  if (oSel != null) {
    oSel.style.backgroundColor = "transparent";
    oSel.style.color = sTxtFore;
  }
  oSel = oPar;
  oSel.style.backgroundColor = sSelBack;
  oSel.style.color = sSelFore;
  var sUrl  = oSel.getAttribute("href");
  if (sUrl.charAt(0) == '.')
    oRootFrame.frames(sTocTarget).location.href = "." + sUrl;
  else if (sUrl.lastIndexOf(":") == -1) {
    if (sUrl.lastIndexOf(".") == -1)
      oRootFrame.frames(sTocTarget).location.href = "../" + sUrl + sRpoExt;
	else
      oRootFrame.frames(sTocTarget).location.href = "../" + sUrl;
  }
  else
    oRootFrame.location.href = sUrl;
  sWord = oSel.getAttribute("word");
  if (sWord != null)
    setTimeout("_clickTopic('" + sWord + "')",200);
} //clickTopic

function setRootFrame() {
  try{
    if (window.top.frames(0).name == "annotateframe")
      oRootFrame = window.top.frames(2).document;
    else
      oRootFrame = window.top;
  }
  catch (wError) {
    oRootFrame = window.top;
  }
} //setRootFrame()

//-------------------------------------------------------
//  TOC routines
//-------------------------------------------------------

function alterImages(oDiv) {
  var wFil  = 0;
  var sPath = "";
  var sName = "";
  var oImg  = oDiv.firstChild;
  while (oImg != null) {
    if (oImg.tagName == "IMG") {
      wFil  = oImg.src.lastIndexOf("/");
      sPath = oImg.src.substr(0,wFil+1);
      sName = oImg.src.substr(wFil+1);
      if (sName == "minus-top.gif")
        oImg.src = sPath + "plus-top.gif";
      else if (sName == "plus-top.gif")
        oImg.src = sPath + "minus-top.gif";
      else if (sName == "minus-cont.gif")
        oImg.src = sPath + "plus-cont.gif";
      else if (sName == "plus-cont.gif")
        oImg.src = sPath + "minus-cont.gif";
      else if (sName == "minus-top-cont.gif")
        oImg.src = sPath + "plus-top-cont.gif";
      else if (sName == "plus-top-cont.gif")
        oImg.src = sPath + "minus-top-cont.gif";
      else if (sName == "minus-end.gif")
        oImg.src = sPath + "plus-end.gif";
      else if (sName == "plus-end.gif")
        oImg.src = sPath + "minus-end.gif";
      if (sName == sImgOpn)
        oImg.src = sPath + sImgClo;
      else if (sName == sImgClo)
        oImg.src = sPath + sImgOpn;
    }
    oImg = oImg.nextSibling;
  } //while
} //alterImages

function clickFolder() {
  var oImg = event.srcElement;
  if (oImg.tagName == "IMG") {
    var oDiv = oImg.parentElement;
    if (oDiv.tagName == "DIV") {
      var oFdr = oDiv.nextSibling;
      if (oFdr.tagName == "DIV") {
        if (oFdr.style.display == "none")
          oFdr.style.display = "";
        else
          oFdr.style.display = "none";
        alterImages(oDiv);
      }
    }
  }
} //clickFolder

function insertSymbols(oRoot,wLevel) {
  var oImg   = null;
  var oSpn   = null;
  var oDiv   = null;
  var wIndex = 0;
  var wUrl   = 0;
  setRootFrame();
  eval(sTocDoc);
  if (wLevel == 0)
    oDiv     = oTocDoc.body.firstChild.firstChild;
  else
    oDiv     = oRoot;
  while (oDiv != null) {
    if (oDiv.className == "")
      insertSymbols(oDiv.firstChild,wLevel + 1);
    else {
      oSpn            = oTocDoc.createElement("span");
      wIndex          = oDiv.id.substr(1).valueOf();
      wUrl            = aRpoToc[wIndex].indexOf("|");
      oSpn.innerText  = aRpoToc[wIndex].substr(wUrl+1);
      oSpn.setAttribute("href",aRpoToc[wIndex].substr(0,wUrl));
      oSpn.attachEvent('onmouseover',mouseonText);
      oSpn.attachEvent('onmouseout',mouseonText);
      oSpn.attachEvent('onclick',clickTopic);
      oSpn.style.cursor = "hand";
      oDiv.insertBefore(oSpn,oDiv.firstChild);
      oImg            = oTocDoc.createElement("img");
      oImg.src        = "gap.gif";
      oDiv.insertBefore(oImg,oDiv.firstChild);
      if (oDiv.className == "book") {
        oImg          = oTocDoc.createElement("img");
        oImg.src      = sImgOpn;
        oImg.id       = "sym";
        oDiv.insertBefore(oImg,oDiv.firstChild);
        oImg          = oTocDoc.createElement("img");
        if ((oDiv.nextSibling.nextSibling != null) && 
            (oDiv.nextSibling.nextSibling.tagName == "DIV")) {
          if (oDiv.previousSibling != null)
            oImg.src  = "minus-cont.gif";
          else if (oDiv.parentElement.tagName == "DIV")
            oImg.src  = "minus-cont.gif";
          else
            oImg.src  = "minus-top-cont.gif";
        }
        else if (oDiv.previousSibling != null)
          oImg.src    = "minus-end.gif";
        else if (oDiv.parentElement.tagName == "DIV")
          oImg.src    = "minus-end.gif";
        else
          oImg.src    = "minus-top.gif";
        oImg.attachEvent('onclick',clickFolder);
        oImg.style.cursor = "hand";
      }
      else {
        oImg          = oTocDoc.createElement("img");
        oImg.src      = sImgPag;
        oImg.id       = "sym";
        oDiv.insertBefore(oImg,oDiv.firstChild);
        oImg          = oTocDoc.createElement("img");
        if (oDiv.nextSibling != null)
          oImg.src    = "branch-cont.gif";
        else
          oImg.src    = "branch-end.gif";
      }
      oDiv.insertBefore(oImg,oDiv.firstChild);
      oPar            = oDiv.parentElement;
      for (wIndex = 0;wIndex < wLevel;wIndex++) {
        oImg       = oTocDoc.createElement("img");
        if ((oPar.tagName != "DIV") || 
            (oPar.nextSibling == null) || 
            (oPar.nextSibling.tagName != "DIV"))
          oImg.src = "blank.gif";
        else
          oImg.src = "vert-line.gif";
        oDiv.insertBefore(oImg,oDiv.firstChild);
        oPar       = oPar.parentElement;
      } //for
    }
    oDiv           = oDiv.nextSibling;
  } //while
} //insertSymbols

function setSelect(oDiv,fOn) {
  if (wTocSelect != -1) {
    var oPar = oDiv.firstChild;
    while (oPar.nextSibling != null)
      oPar = oPar.nextSibling;
    if (oSel != null) {
      oSel.style.backgroundColor = "transparent";
      oSel.style.color = sTxtFore;
    }
    if (fOn) {
      setRootFrame();
      eval(sTocDoc);
      var oRange = oTocDoc.body.createTextRange();
      var sTitle = aRpoToc[wTocSelect].substr(aRpoToc[wTocSelect].indexOf("|")+1);
      while ((oRange.findText(sTitle)) && (oRange.parentElement() != oPar))
        oRange.collapse(false);
      oSel = oPar;
      oSel.style.backgroundColor = sSelBack;
      oSel.style.color = sSelFore;
    }
  }
} //setSelect

function collapseToc(oRoot) {
  var oImg       = null;
  var oAnc       = null;
  var wIndex     = 0;
  var wUrl       = 0;
  var oDiv       = oRoot;
  wTocSelect     = -1;
  while (oDiv != null) {
    if (oDiv.tagName != "DIV")
      ;
    else if (oDiv.className == "")
      collapseToc(oDiv.firstChild);
    else if (oDiv.className == "book") {
      oFdr = oDiv.nextSibling;
      if ((oFdr.tagName == "DIV") && (oFdr.style.display != "none")) {
        oFdr.style.display = "none";
        alterImages(oDiv);
      }
    }
    oDiv = oDiv.nextSibling;
  } //while
} //collapseToc

function setCurrent(sPageUrl) {
  if (sPageUrl == null) {
    try {
      sPageUrl = oRootFrame.frames[sTocTarget].location.href;
    }
    catch(wError) {
      setTimeout("setCurrent(null)",250);
    }
  }
  setRootFrame();
  eval(sTocDoc);
  var oRoot  = oTocDoc.body.firstChild.firstChild;
  var oDiv   = null;
  var oFdr   = null;
  var sName  = "";
  var wIndex = urlToIndex(sPageUrl);
  if ((wIndex != -1) && (wIndex != wTocSelect)) {
    if (wTocSelect != -1) {
      oDiv = oTocDoc.all("L" + wTocSelect.toString(10));
      if (oDiv != null) {
        setSelect(oDiv,false);
        wTocSelect = -1;
      }
    }
    oDiv = oTocDoc.all("L" + wIndex.toString(10));
    if (oDiv != null) {
      if ((oSel == null) || (oSel.parentElement != oDiv))
        collapseToc(oRoot);
      wTocSelect = wIndex;
      oFdr       = oDiv;
      while (oFdr.parentElement.tagName == "DIV") {
        oFdr = oFdr.parentElement;
        if (oFdr.style.display != "") {
          oFdr.style.display = "";
          alterImages(oFdr.previousSibling);
        }
      } //while
      setSelect(oDiv,true);
      if (oDiv.className == "book") {
        oFdr = oDiv.nextSibling;
        if (oFdr.style.display != "") {
          oFdr.style.display = "";
          alterImages(oDiv);
        }
      }
    }
  }
} //setCurrent

//-------------------------------------------------------
//  table routines
//-------------------------------------------------------

function onResizeBody(wFrame) {
  setRootFrame();
  eval(sTocDoc);
  var oTblDoc = oTocDoc.body.firstChild.document.frames(wFrame).document;
  var oBanner = oTblDoc.body.firstChild;
  var oScroll = oBanner.nextSibling;
  if (oTblDoc.body.clientHeight > oBanner.clientHeight)
    oScroll.style.height = oTblDoc.body.clientHeight - oBanner.clientHeight - 1;
  else
    oScroll.style.height = 0;
  oScroll.style.width    = oTblDoc.body.clientWidth;
} //onResizeBody

function displayTopics(oSel,sWord) {
  setRootFrame();
  eval(sTocDoc);
  var oTblDoc  = oTocDoc.body.firstChild.document.frames(1).document;
  var oBanner  = oTblDoc.body.firstChild;
  var oScroll  = oBanner.nextSibling;
  var oNobr    = oScroll.firstChild;
  while (oNobr.firstChild != null)
    oNobr.removeChild(oNobr.firstChild);
  if (oSel != null) {
    var oSeek    = null;
    var aFound   = oSel.getAttribute("found");
    var sTopic   = "";
    var sText    = "";
    var wName    = 0;
    for (wIndex = 0;wIndex < wTocItems;wIndex++) {
      if (aFound[wIndex] == true) {
        sTopic     = aRpoToc[wIndex];
        wName      = sTopic.indexOf("|");
        sText      = sTopic.substr(wName+1);
        oPar       = oTblDoc.createElement("p");
        oPar.id    = "L" + wIndex.toString(10);
        oPar.className = "topic";
        oPar.innerText = sText;
        oPar.setAttribute("href",sTopic.substr(0,wName));
        oPar.setAttribute("word",sWord);
        oPar.attachEvent('onmouseover',mouseonText);
        oPar.attachEvent('onmouseout',mouseonText);
        oPar.attachEvent('onclick',clickTopic);
        oPar.style.cursor = "hand";
        oSeek = oNobr.firstChild;
        sText = sText.toLowerCase();
        while ((oSeek != null) && (oSeek.innerText.toLowerCase() < sText))
          oSeek = oSeek.nextSibling;
        if (oSeek == null)
          oNobr.appendChild(oPar);
        else
          oNobr.insertBefore(oPar,oSeek);
      }
    } //for
  }
  onResizeBody(1);
} //displayTopics

function mergeFoundItems(aFound,sIdTable) {
  var sIdArray  = "";
  var wId       = 0;
  var wIdBar    = 0;
  sIdArray      = sIdArray.concat(sIdTable);
  while (wIdBar != -1) {
    wIdBar      = sIdArray.indexOf("|");
    if (wIdBar == -1)
      wId       = sIdArray.valueOf();
    else {
      wId       = sIdArray.substr(0,wIdBar).valueOf();
      sIdArray  = sIdArray.substr(wIdBar + 1);
    }
    aFound[wId] = true;
  } //while
} //mergeFoundItems

function onChangeText() {
  if (event.propertyName == "value") {
    setRootFrame();
    eval(sTocDoc);
    var oTblDoc = oTocDoc.body.firstChild.document.frames(0).document;
    var oRange  = oTblDoc.body.createTextRange();
    var oEdit   = oTblDoc.body.firstChild.firstChild;
    var oPar    = null;
    var sWord   = "";
    if (oSel != null) {
      oSel.style.backgroundColor = "transparent";
      oSel.style.color = sTxtFore;
      oSel = null;
      window.parent.document.frames(1).displayTopics(null,"");
    }
    if (oEdit.value != "") {
      var fFound = false;
      while ((fFound == false) && (oRange.findText(oEdit.value))) {
        oPar = oRange.parentElement();
        if ((oPar != null) && (oPar.innerText.toLowerCase().indexOf(oEdit.value.toLowerCase()) == 0))
          fFound = true;
        else
          oRange.collapse(false);
      } //while
      if (fFound == true) {
        var aFound  = new Array(wTocItems);
        for (wIndex = 0;wIndex < wTocItems;wIndex++)
          aFound[wIndex] = false;
        mergeFoundItems(aFound,oPar.getAttribute("term"));
        oSel = oPar;
        oSel.setAttribute("found",aFound);
        oSel.style.backgroundColor = sSelBack;
        oSel.style.color = sSelFore;
        oRange.scrollIntoView();
        if (oEdit.id == "find")
          sWord = oPar.innerText;
        window.parent.document.frames(1).displayTopics(oSel,sWord);
      }
    }
  }
} //onChangeText

function SelectWords(sWord,fOn) {
  if (sWord != "") {
    //Show all hidden text
    var wIndex = 0;
    var oDiv   = oRootFrame.frames(sTocTarget).document.all.tags("DIV");
    if (oDiv != null) {
      for (wIndex = 0;wIndex < oDiv.length;wIndex++) {
        if ((oDiv[wIndex].style != null) && (oDiv[wIndex].style.display == "none")) {
          oDiv[wIndex].style.visibility = "hidden";
          oDiv[wIndex].style.display = "";
          oDiv[wIndex].setAttribute("xdk","search",0);
        }
      } //for
    }
    // highlight text
    var oRange = oRootFrame.frames(sTocTarget).document.body.createTextRange();
    var fFirst = true;
    while (oRange.findText(sWord)) {
      if (fOn == true) {
        oRange.execCommand("BackColor",false,sSelBack);
        oRange.execCommand("ForeColor",false,sSelFore);
        if (fFirst == true)
          oRange.scrollIntoView();
        fFirst = false;
      }
      else
        oRange.execCommand("RemoveFormat",false,null);
      oRange.collapse(false);
    } //while
    //Hide all hidden text
    if (oDiv != null) {
      for (wIndex = 0;wIndex < oDiv.length;wIndex++) {
        if (oDiv[wIndex].getAttribute("xdk",0) == "search") {
          oDiv[wIndex].style.display = "none";
          oDiv[wIndex].style.visibility = "visible";
          oDiv[wIndex].removeAttribute("xdk",0);
        }
      } //for
    }
  }
} //SelectWords

function clickTerm() {
  setRootFrame();
  eval(sTocDoc);
  var oTblDoc = oTocDoc.body.firstChild.document.frames(0).document;
  var oEdit   = oTblDoc.body.firstChild.firstChild;
  var oPar    = event.srcElement;
  oEdit.value = oPar.innerText;
} //clickTerm

function _clickTopic(sWord) {
  var oTarget = oRootFrame.frames(sTocTarget).document;
  if ((oTarget.readyState == "complete") && (oTarget.body != null))
    SelectWords(sWord,true);
  else
    setTimeout("_clickTopic('" + sWord + "')",200);
} //_clickTopic

function tableLoad(oTable,wTerms) {
  setRootFrame();
  eval(sTocDoc);
  var oTblDoc  = oTocDoc.body.firstChild.document.frames(0).document;
  var oBanner  = oTblDoc.body.firstChild;
  var oScroll  = oBanner.nextSibling;
  var oNobr    = oScroll.firstChild;
  var oEdit    = oBanner.firstChild;
  if (wTerms != 0) {
    var sTerm    = "";
    var wName    = 0;
    var wIndex   = 0;
    for (wIndex  = 0;wIndex < wTerms;wIndex++) {
      sTerm          = oTable[wIndex];
      wName          = sTerm.indexOf("|");
      oPar           = oTblDoc.createElement("p");
      oPar.id        = "L" + wIndex.toString(10);
      oPar.className = "term";
      oPar.innerText = sTerm.substr(0,wName);
      oPar.setAttribute("term",sTerm.substr(wName+1));
      oPar.attachEvent('onmouseover',mouseonText);
      oPar.attachEvent('onmouseout',mouseonText);
      oPar.attachEvent('onclick',clickTerm);
      oPar.style.cursor = "hand";
      oNobr.appendChild(oPar);
    } //for
  }
  onResizeBody(0);
  if ((oEdit != null) && (oEdit.tagName == "INPUT"))
    oEdit.focus();
} //tableLoad

//-------------------------------------------------------
//  find routines
//-------------------------------------------------------

function onFindChangeText() {
  if (event.propertyName == "value") {
    setRootFrame();
    eval(sTocDoc);
    var oTblDoc = oTocDoc.body.firstChild.document.frames(0).document;
    var oBanner = oTblDoc.body.firstChild;
    var oEdit   = oBanner.firstChild;
    var sEdit   = oEdit.value.toLowerCase();
    var oScroll = oBanner.nextSibling;
    var oNobr   = oScroll.firstChild;
    var sTerm   = "";
    var wName   = 0;
    var wIndex  = 0;
    oSel        = null;
    oPrevSel    = null;
    while (oNobr.firstChild != null)
      oNobr.removeChild(oNobr.firstChild);
    if (sEdit.length != 0) {
      var aFound  = new Array(wTocItems);
      for (wIndex = 0;wIndex < wTocItems;wIndex++)
        aFound[wIndex] = false;
      for (wIndex = 0;wIndex < wFtsItems;wIndex++) {
        sTerm     = aRpoFts[wIndex];
        if (sTerm.substr(0,sEdit.length) == sEdit) {
          wName          = sTerm.indexOf("|");
          oPar           = oTblDoc.createElement("p");
          oPar.id        = "L" + wIndex.toString(10);
          oPar.className = "term";
          oPar.innerText = sTerm.substr(0,wName);
          oPar.setAttribute("term",sTerm.substr(wName+1));
          oPar.attachEvent('onmouseover',mouseonText);
          oPar.attachEvent('onmouseout',mouseonText);
          oPar.attachEvent('onclick',clickTerm);
          oPar.style.cursor = "hand";
          oNobr.appendChild(oPar);
          mergeFoundItems(aFound,sTerm.substr(wName+1));
        }
      } //for
      oSel = oTblDoc.createElement("p");
      oSel.setAttribute("found",aFound);
      oSel.setAttribute("word",sEdit);
      setTimeout("UpdateTopics()",500);
    }
    else
      window.parent.document.frames(1).displayTopics(null,"");
    onResizeBody(0);
  }
} //onFindChangeText

function UpdateTopics() {
  if ((oSel != null) && (oPrevSel != oSel)) {
    oPrevSel = oSel;
    window.parent.document.frames(1).displayTopics(oSel,oSel.getAttribute("word"));
  }
} //function

//-------------------------------------------------------
//  toolbar routines
//-------------------------------------------------------

function buttonSetGraphic(oTable,sImage) {
  oTable.firstChild.firstChild.firstChild.firstChild.src = oTable.getAttribute(sImage);
} //buttonSetGraphic

function defaultButton(sTable) {
  oBtnDown = document.all(sTable);
  oBtnDown.className = "buttonDown";
  buttonSetGraphic(oBtnDown,"onimage");
} //defaultButton

function mouseoverButton(oTable) {
  if (oTable.className == "buttonOff")
    oTable.className = "buttonOver";
  else
    oTable.className = "buttonPress";
  buttonSetGraphic(oTable,"onimage");
} //mouseoverButton

function mouseoutButton(oTable) {
  if (oTable.className == "buttonOver") {
    oTable.className = "buttonOff";
    buttonSetGraphic(oTable,"offimage");
  }
  else
    oTable.className = "buttonDown";
} //mouseoutButton

function mousedownButton(oTable) {
  if (oTable.className == "buttonOver")
    oTable.className = "buttonPress";
} //mousedownButton

function mouseupButton(oTable) {
  if ((oBtnDown != null) && (oBtnDown != oTable)) {
    oBtnDown.className = "buttonOff";
    buttonSetGraphic(oBtnDown,"offimage");
  }
  if (oBtnDown == oTable)
    oTable.className = "buttonOver";
  else {
    oBtnDown = oTable;
    oBtnDown.className = "buttonPress";
    buttonSetGraphic(oBtnDown,"onimage");
    window.parent.frames(1).location.href = oBtnDown.getAttribute("href");
  }
} //mouseupButton

//---------------- End of RePublicoToc.js ----------------------
