/*
  collapse.js

  by Jelks H. Cabaniss, III
  November, 1998 c.e.  (for IE4)
  Updated June, 2002 c.e. (for DOM UAs)

  Script for collapsing and expanding sections under a heading.
  The only thing you have to do  to take advantage of it is  to
  give your "headings" and corresponding "sections" unique IDs:

    <div id="headOne"> ... </div>
    <div id="sectOne"> ... </div>

    <h1 id="headNews"> ... </h1>
    <p  id="sectNews"> ... </p>

  Also note if you use images (open & closed folders, + & -), 
  this is set up to handle ONE of two sets:
  *  open.gif and fold.gif  = Windows Explorer-style "folders"
     or
  *  plus.gif and minus.gif = + and - signs
  (You can snag each of these from my /images folder!)
  In such a case, markup your document appropriately by placing 
  the "closed" image in your "heading", i.e.:

    <h2 id="headDogs"><img id="imgheadDogs" alt="" 
        src="plus.gif" />All about Dogs ...</h2>
    <ul id="sectDogs">
      <li>Lassie</li>
      <li>Fido</li>
    </ul>

  (Omitted width & height attributes above for brevity's sake.)
  Note that by using "plus.gif", it will change to "minus.gif"
  when you run the script in the page.

  Note the markup is immaterial:  you can use H1-H6, P, UL, etc
  for *either* the "heading" or the section part. The important
  thing is to remember the heading's ID starts with "head"  and
  the sections's ID starts with "sect",  and they must end with
  the same string.
*/

var patternHead = new RegExp("^head(.*)");
var patternImg = new RegExp("^imghead(.*)");
var ie = (document.all) ? true : false;
window.onload = init;

function init() {
  if (document.layers)
    return; // Kick out Netscape 4

  document.body.onclick = msclicked;
  document.body.onmouseover = msover;
  document.body.onmouseout = msout;

  var srcID
  var cursorStyle = (ie) ? "hand" : "pointer";
  var all = (ie) ? document.all : document.getElementsByTagName("*");

  for (i=0; i < all.length; i++) {
    srcID = (ie) ? all[i].id : all[i].getAttribute("id")
    if (srcID) {
      var headKey = srcID.match(patternHead)
      if (headKey) {
        // collapse all sections on startup
          eval("document.getElementById('sect" + RegExp.$1 + "').style.display = 'none'");
          eval("document.getElementById('" + srcID + "').style.cursor = '" + cursorStyle + "'");
          eval("document.getElementById('" + srcID + "').title = 'Click to expand...'");
      }
    }
  }
}

function toggle(sect) {
  var sectStyle = "document.getElementById('sect" + sect + "').style.display";
  var headTitle = "document.getElementById('head" + sect + "').title = ";
  var sFoldedName;
  var sOpenedName;

  var headID = "head" + sect;
  var imgID = "imghead" + sect;
  var hasImg = (eval("document.getElementById('" + headID + "').innerHTML.match('" + imgID + "')") != null);
  if (hasImg) {
    var src = eval("document.getElementById('" + imgID + "').src");
    var reg = new RegExp("(^.*[/])([a-z.]+)$");
    var m = src.match(reg);
    if (m != null) {
      var sPath = m[1];
      var sFile = m[2];
      if (sFile == "fold.gif" || sFile == "open.gif") {
        sFoldedName = "fold.gif"
        sOpenedName = "open.gif"
      }
      else
      if (sFile == "plus.gif" || sFile == "minus.gif") {
        sFoldedName = "plus.gif"
        sOpenedName = "minus.gif"
      }
      //alert("[" + sFile + "] : " + sFoldedName
      var sFold = "document.getElementById('" + imgID + "').src = '" + sPath + sFoldedName + "'"
      var sOpen = "document.getElementById('" + imgID + "').src = '" + sPath + sOpenedName + "'"
    }
  }
  if (eval(sectStyle + " != 'none'")) {
    eval(sectStyle + " = 'none'");
    eval(headTitle + "'Click to expand...'");
    if (hasImg)
      eval(sFold);
  }
  else {
    eval(sectStyle + " = 'block'");
    eval(headTitle + "'Click to collapse...'");
    if (hasImg)
      eval(sOpen);
  }
}

function msclicked(e) {
  e = (e) ? e : ((window.event) ? window.event : "")
  var srcID = getSourceID(e);
  var headKey = srcID.match(patternHead);
  if (headKey)
    toggle(RegExp.$1);
  else {
    var imgKey = srcID.match(patternImg);
    if (imgKey != null)
      toggle(RegExp.$1);
  }
}

function msover(e) {
  e = (e) ? e : ((window.event) ? window.event : "")
  var srcID = getSourceID(e);
  var headKey = srcID.match(patternHead)
  if (headKey) 
    eval("document.getElementById('" + srcID + "').style.color = 'green'");
}

function msout(e) {
  e = (e) ? e : ((window.event) ? window.event : "")
  var srcID = getSourceID(e);
  var headKey = srcID.match(patternHead)
  if (headKey)
    eval("document.getElementById('" + srcID + "').style.color = 'navy'");
}

function getSourceID(e) {
  if (e.target) {   // DOM
    // Note: a nodeType of 3 is Text (#PCDATA):
    var elem = (e.target.nodeType == 3) ? e.target.parentNode : e.target
    return elem.getAttribute("id")
  } else if (e.srcElement) {  // IE object model
    return e.srcElement.id
  }
}

