/*
 * date:  2003-01-23
 * info:  http://inspire.server101.com/js/xc/
 */

var xcNode = [];

function xcShow(nodeToExpandId) {
  var thisNode = document.getElementById(nodeToExpandId);
  var collapseWidgetHref = "javascript:xcHide(\'" + thisNode.getAttribute("id") + "\');";
  thisNode.style.display = "block"; // this shows the UL of child links
  
  var title = thisNode.parentNode.getElementsByTagName("a").item(0).title;
  thisNode.parentNode.getElementsByTagName("a").item(0).setAttribute("href" , collapseWidgetHref);  // this changes the expand/collapse icon
  thisNode.parentNode.getElementsByTagName("a").item(0).firstChild.data = "[-]";
  thisNode.parentNode.getElementsByTagName("a").item(0).title = title.replace("(expand menu)", "(collapse menu)");
}


function xcHide(nodeToCollapseId) {
  var thisNode = document.getElementById(nodeToCollapseId);
  var expandWidgetHref = "javascript:xcShow(\'" + thisNode.getAttribute("id") + "\');";
  thisNode.style.display = "none"; // this hides the UL of child links

  var title = thisNode.parentNode.getElementsByTagName("a").item(0).title;
  thisNode.parentNode.getElementsByTagName("a").item(0).setAttribute("href" , expandWidgetHref);  // this changes the expand/collapse icon
  thisNode.parentNode.getElementsByTagName("a").item(0).firstChild.data = "[+]";
  thisNode.parentNode.getElementsByTagName("a").item(0).title = title.replace("(collapse menu)", "(expand menu)");
}

function xcCtrl(m, c, s, v, f, t) {
  var a = document.createElement('a');  

  a.setAttribute('href', 'javascript:xc'+f+'(\''+m+'\');');
  a.setAttribute('title', t);
  a.appendChild(document.createTextNode(v));

  var d = document.createElement('div');
  d.className = c+s;
  d.appendChild(a);
  
  return xcNode[m+s] = d;
}

// added by rjw
//
// returns true if xc should work in this browser
// (uses same criteria as xcSet() does)
//
function xcViable() {
  return (document.getElementById && document.createElement) ? true : false;
}

//
// displays 'expand all' link
//   (which should be enclosed in an element with id 'xcexpandall')
// t is the display value to use (such as 'block' or 'inline')
//
function xcDisplayExpandAll(t) {
  if (xcViable()) {
    var obj = document.getElementById('xcexpandall');  // get element enclosing the link
    obj.style.display = t;                             // set display style to something visible
  }
}

//
// opens all closed nodes
// arguments are same as first two originally passed to xcSet
//
function xcExpandAll(m, c) {
  if (xcViable()) {
    m = document.getElementById(m).getElementsByTagName('ul');  // get array of ULs (submenus) within main menu
    var i, d, nodeold, nodenew;
    for (i = 0; i < m.length; i++) {                            // for each UL
      try {
        d = m[i].getAttribute('id');
        xcShow(d);       										//  replace it with the [-] node
      } catch(e) {
        alert(e);
      }
    }
  }
}

function xcExpandAll2(m, c) {
  if (xcViable()) {
    m = document.getElementById(m).getElementsByTagName('ul');  // get array of ULs (submenus) within main menu
    var i, d, nodeold, nodenew;
    for (i = 0; i < m.length; i++) {                            // for each UL
      try {
        d = m[i].getAttribute('id');
        m[i].style.display = 'block';                           // display it
        nodenew = xcNode[d+'c'];                                // get previously created nodes for [+] and [-]
        nodeold = xcNode[d+'x'];
        if (m[i].parentNode.firstChild.className == (c+'x'))    // if displaying the [+] node,
          m[i].parentNode.replaceChild(nodenew, nodeold);       //  replace it with the [-] node
      } catch(e) {
        alert(e);
      }
    }
  }
}

//
// xcSet2() - called in body onload() - sets up xc menus
// m is the ul tag that holds the main menu items
//
function xcSet2(m, c) {
  
  if (xcViable()) {
    var k = -1;
    if (typeof document.URL == "string") {
      k = document.URL.indexOf("#"); // navigating to an anchor
    }

    // the core of this function is taken from the original xcSet() - the only
    // difference is the check if k < 0
    m = document.getElementById(m).getElementsByTagName('ul');
	// d is the "ul" id
	// p is the parent node of the "ul"
    var d, p, x, h, i, j;
    for (i = 0; i < m.length; i++) {
      if (d = m[i].getAttribute('id')) {
        xcCtrl(d, c, 'x', '[+]', 'Show', m[i].getAttribute('title')+' (expand menu)');
        x = xcCtrl(d, c, 'c', '[-]', 'Hide', m[i].getAttribute('title')+' (collapse menu)');
        p = m[i].parentNode;
		
        if (k < 0) { // not navigating to an anchor
          if (h = !p.className) {
            j = 2;
            while ((h = !(d == arguments[j])) && (j++ < arguments.length));
            if (h) {
              m[i].style.display = 'none';
              x = xcNode[d+'x'];
            }
          }
        }
        p.className = c;
        p.insertBefore(x, p.firstChild);
      }
    }

    xcDisplayExpandAll('block');          // show 'expand all' link
    // get the anchor name and try to find it on the page
    // if found, set focus to that link
    if (k >= 0) {
      var anchorname = document.URL.substring(k+1, document.URL.length);
      if (anchorname != "") {
        var linkobj = document.getElementById(anchorname);
        if (linkobj && linkobj.focus) {
          linkobj.focus();
        }
      }
    }
  }
}


/* UTILITIES */

function clearNode(thisNodeId)
{
	var emptyNode;
	var thisNode=document.getElementById(thisNodeId);

	if(thisNode.tagName=="" || thisNode.tagName==null) {
       emptyNode=document.createTextNode("");
    }
    else {
      var tag=thisNode.tagName;
      emptyNode=document.createElement(tag);
	}
	emptyNode.setAttribute("id",thisNodeId);
	thisNode.parentNode.replaceChild(emptyNode,thisNode);
}


/* Original Functions


function xcSet(m, c) {
  if (document.getElementById && document.createElement) {

    m = document.getElementById(m).getElementsByTagName('ul');
    var d, p, x, h, i, j;

	  for (i = 0; i < m.length; i++) {
	    try {
	      d = m[i].getAttribute('id');
	      xcCtrl(d, c, 'x', '[+]', 'Show', m[i].getAttribute('title')+' (expand menu)');
	      x = xcCtrl(d, c, 'c', '[-]', 'Hide', m[i].getAttribute('title')+' (collapse menu)');
	
	      p = m[i].parentNode;
	      
	      try {
	        h = !p.className;
	        j = 2;
	        while ((h = !(d == arguments[j])) && (j++ < arguments.length));
	        if (h) {
	          m[i].style.display = 'none';
	          x = xcNode[d+'x'];
	        }
	      } catch(e) {
	        alert(e);
	      }
	      p.className = c;
	      p.insertBefore(x, p.firstChild);
	    } catch(e) {
	      alert(e);
	    }
	  }

  } // end top if
}

function xcCtrl(m, c, s, v, f, t) {
  var a = document.createElement('a');  
  a.setAttribute('href', 'javascript:xc'+f+'(\''+m+'\');');
  a.setAttribute('title', t);
  a.appendChild(document.createTextNode(v));

  var d = document.createElement('div');
  d.className = c+s;
  d.appendChild(a);

  return xcNode[m+s] = d;
}

function xcShow(m) {
  xcXC(m, 'block', m+'c', m+'x');
}


function xcHide(m) {
  xcXC(m, 'none', m+'x', m+'c');
}


function xcXC(e, d, s, h) {
  var nodeId = document.getElementById(e);
  nodeId.style.display = d;
  alert(s + " " + h);
  nodeId.parentNode.replaceChild(xcNode[s], xcNode[h]);
}
*/