//
// This code is modified from the DOM2 Demo available
// at http://www.w3.org/DOM/demo.js
//

// Redisplay the elements following the target node on the screen
// and set the event handlers appropriatly
function displayChildrenOnScreen(e) {
    if (e.button == 0) { // if left button
	// get the target node, then its next sibling
	var node = e.currentTarget.nextSibling;

	while (node != null) {
	    if (node.nodeType == 1) {
		// assume all are block-level
		node.style
		    .setProperty("display", "block", "");
	    }
	    node = node.nextSibling;
	}
	// remove the "+ " added by removeChildrenFromScreen
	e.currentTarget.removeChild(e.currentTarget.firstChild);

	// add the "- " for the UI feedback
     var sqr = document.createElement("img");
     sqr.setAttribute("src", "/images/-.gif");
     sqr.setAttribute("height", 9);
     sqr.setAttribute("width", 9);
     sqr.setAttribute("hspace", 3);
     sqr.setAttribute("vspace", 1);	
	e.currentTarget.insertBefore(sqr,e.currentTarget.firstChild);
 
	// remove the activation of this function
	e.currentTarget
	    .removeEventListener("mousedown", displayChildrenOnScreen, false);
	// add the activation of removeChildrenFromScreen
	e.currentTarget
	    .addEventListener("mousedown", removeChildrenFromScreen, false);
    }
}

// Remove the elements following the target node from the screen
// and set the event handlers appropriatly
function removeChildrenFromScreen(e) {
    
    if (e.button == 0) { // if left button	
	// get the target node, then its next sibling
	var node = e.currentTarget.nextSibling;
	while (node != null) {
	    if (node.nodeType == 1) {
		node.style
		    .setProperty("display", "none", "");
	    }
	    node = node.nextSibling;
	}

	// remove the "- " added by removeChildrenFromScreen
	e.currentTarget.removeChild(e.currentTarget.firstChild);

	// add the "+ " for the UI feedback
     var sqr = document.createElement("img");
     sqr.setAttribute("src", "/images/+.gif");
     sqr.setAttribute("height", 9);
     sqr.setAttribute("width", 9);
     sqr.setAttribute("hspace", 3);
     sqr.setAttribute("vspace", 1);
	e.currentTarget.insertBefore(sqr,e.currentTarget.firstChild);
	
	// remove the activation of this function
	e.currentTarget
	    .removeEventListener("mousedown", removeChildrenFromScreen, false);
	// add the activation of addChildrenOnScreen
	e.currentTarget
	    .addEventListener("mousedown", displayChildrenOnScreen, false);
    }
}

function init() {

    if (!(document.implementation
	  && document.implementation.hasFeature)) {
      // no DOM support :-(
      return;
    }

    // if the implementation supports Mouse events
    // and CSS from DOM Level 2, hide content under
    // headings until the heading is clicked.

    if (document.implementation.hasFeature("CSS", "2.0")
	&& document.implementation.hasFeature("MouseEvents", "2.0")) {
	
	var firstItal = document.getElementsByTagName("i").item(0);
	var body = firstItal.parentNode;
	var newItal = document.createElement("i");
	newItal.appendChild(document.createTextNode("Click on a heading to see more"));
	body.insertBefore(newItal, firstItal);

	// using directly the tag name
	var h2s = document.getElementsByTagName("h2");
	var i = 0;
	
	// add the event handler on each h2 and hide the contents
	while (i < h2s.length) {
	    var h2 = h2s.item(i);

 	    // hide the children
	    var node = h2.nextSibling;
	    while (node != null) {
	      if (node.nodeType == 1) {
		   node.style.setProperty("display", "none", "");
	      }
	      node = node.nextSibling;
	    }

	    // add the "+ " for the UI feedback
         var sqr = document.createElement("img");
         sqr.setAttribute("src", "/images/+.gif");
         sqr.setAttribute("height", 9);
         sqr.setAttribute("width", 9);
         sqr.setAttribute("hspace", 3);
         sqr.setAttribute("vspace", 1);
	    h2.insertBefore(sqr,h2.firstChild);
         h2.addEventListener("mousedown", displayChildrenOnScreen, false);
	    h2.style.cursor = "pointer";
	    i ++;
	}      
    }
}
