/*
  TOC controls written by Scriptorium Publishing Services for TxDOT. 

*/

function detectBrowser () {
   var browser=navigator.appName;
   var version=navigator.appVersion;
   var ret_version=parseFloat(version);
   var ret_browser;
   if (browser=="Microsoft Internet Explorer") {
      ret_browser = "IE";
   } else  {
      ret_browser = "Other";
   }
   return(ret_browser);
}

function move_highlight (a_tag) {
   var i;
   // Find the a tag that has the current highlight and clear it.
   var all_As = document.getElementById("toclist").getElementsByTagName("a");
   for (i=0;i<all_As.length;i++) {
       if (all_As[i].style.backgroundColor != "") {
          all_As[i].style.backgroundColor = "";
          break;
       }
   }
   // Apply the highlight to the selected "a" tag.
   a_tag.style.backgroundColor = "#99CCFF";
}

function check_highlight () {
   // The direction of the separators is dependent on the browser...but I need to check IE viewing a web page.
   var browser = detectBrowser();
   var sep = '/';
   // Firefox automatically swaps the separators.  IE doesn't for file: protocol.
   if (browser == 'IE' && document.URL.match('^file:')) {
     sep = '\\';  
   }

  	var URL_array = document.URL.split(sep);
	  var URL_filename = URL_array[URL_array.length-1];
  
   // Find the href in the TOC
   // Get all a tags in the TOC.
   var all_As = document.getElementById("toclist").getElementsByTagName("a");
   var i;
   for (i=0;i<all_As.length;i++) {
      var a_href = new String();
      // IE expands the URL in the href attribute.
      if (browser == 'IE') {
       	a_href = all_As[i].getAttribute("href");
        var a_href_array = a_href.split('/'); 
	       a_href = a_href_array[a_href_array.length-1]; 
      } else {
          // Firefox only keeps the filename portion. 
          a_href = all_As[i].getAttribute("href"); 
      }
      if (a_href == URL_filename) {
          // Add highlighting.
          all_As[i].style.backgroundColor = "#99CCFF";
          // Scroll hidden elements into view.
          all_As[i].focus();
          break;
      }
   } 
}

function toggle_toc(span) {

   var myul = span.parentNode.getElementsByTagName('ul')[0];
   
   if (myul) {
      if ( myul.style.display == "none") {
         // Turn on this section
         myul.style.display = "block";
         myul.style.paddingRight = "3px";
         span.replaceChild(document.createTextNode('\u25BC'),span.childNodes[0]);
         span.className = 'toc_gizmo';
      } else {
         // Turn off this section
         myul.style.display = "none";
         span.replaceChild(document.createTextNode('\u25BA'),span.childNodes[0]);
         span.className = 'toc_gizmo';
      }
   }
}
// Debugging tool
function fNodeInfo(node) {
   alert(node.nodeType + " * " + node.nodeValue + " * " + node.nodeName );
}

/* The function toggle_search works in conjunction with the .searchLink style definition in txDOT_manuals and its counterpart in pages.xsl 
function toggle_search(search_text_div) {
    var search_div = document.getElementById("searchbar");
    if (search_div.style.display == "none") {
       search_div.style.display = "block";
       search_text_div.replaceChild(document.createTextNode('Hide search box'),search_text_div.childNodes[0]);
    } else {
       search_div.style.display = "none";
       search_text_div.replaceChild(document.createTextNode('Click to Search'),search_text_div.childNodes[0]);
    }
}                       */