// Copyright (c) 2001 World Wide Web Consortium,
// (Massachusetts Institute of Technology, Institut National de
// Recherche en Informatique et en Automatique, Keio University). All
// Rights Reserved. This program is distributed under the W3C's Software
// Intellectual Property License. This program is distributed in the
// hope that it will be useful, but WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE.
// See W3C License http://www.w3.org/Consortium/Legal/ for more details.
//
// $Id: demo.js,v 1.9 2003/02/09 04:19:55 plehegar Exp $

// This is a demo of DOM Level 2

// 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);
	 
	// 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;
	}
	// add the "+ " for the UI feedback
	e.currentTarget.insertBefore(document.createTextNode('+ '),
				     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);
    }
}


// Note: the two following functions could be replaced with the
// Node.textContent property added in DOM Level 3.

function getConcatenedTextContent(node) {
    var _result = "";
    if (node == null) {
	return _result;
    }
    var childrens = node.childNodes;
    var i = 0;
    while (i < childrens.length) {
	var child = childrens.item(i);
	switch (child.nodeType) {
	case 1: // ELEMENT_NODE
	case 5: // ENTITY_REFERENCE_NODE
	    _result += getConcatenedTextContent(child);
	    break;
	case 3: // TEXT_NODE
	case 2: // ATTRIBUTE_NODE
	case 4: // CDATA_SECTION_NODE
	    _result += child.nodeValue;
	    break;
	case 6: // ENTITY_NODE
	case 7: // PROCESSING_INSTRUCTION_NODE
	case 8: // COMMENT_NODE
	case 9: // DOCUMENT_NODE
	case 10: // DOCUMENT_TYPE_NODE
	case 11: // DOCUMENT_FRAGMENT_NODE
	case 12: // NOTATION_NODE
	    // skip
	    break;
	}
	i ++;
    }
    return _result;
}

function getTextContent(node) {
    var _result = "";
    if (node == null) {
	return _result;
    }
    switch (node.nodeType) {
    case 1: // ELEMENT_NODE
    case 5: // ENTITY_REFERENCE_NODE
	_result += getConcatenedTextContent(node);
	break;
    case 3: // TEXT_NODE
    case 2: // ATTRIBUTE_NODE
    case 4: // CDATA_SECTION_NODE
    case 7: // PROCESSING_INSTRUCTION_NODE
    case 8: // COMMENT_NODE
	_result += node.nodeValue;
	break;
    case 6: // ENTITY_NODE
    case 9: // DOCUMENT_NODE
    case 10: // DOCUMENT_TYPE_NODE
    case 11: // DOCUMENT_FRAGMENT_NODE
    case 12: // NOTATION_NODE
	// skip
	break;
    }
    return _result;
}

function init() {

    if (!(document.implementation
	  && document.implementation.hasFeature)) {
      // no DOM support :-(
      return;
    }

    // test if the implementation support DOM Level 1 HTML
    if (document.implementation.hasFeature("HTML", "1.0")) {

	// generate the table of contents
	var olist = document.createElement("ol");

	// getElementsByTagName use the tagName property of the Element
	// interface to find them.
	var h2s = document.getElementsByTagName("h2");
	var i = 0;
	var li, a;

	while (i < h2s.length) {
	    var h2 = h2s.item(i);
	    var parent = h2.parentNode;

	    // add an id attribute if missing
	    if (parent.getAttribute("id") == "") {
		parent.setAttribute("id", "generated" + i );
	    }
	    
	    li = document.createElement("li");
	    a = document.createElement("a");
	    a.setAttribute("href", "#" + parent.getAttribute("id"));
	    a.appendChild(document.createTextNode(getTextContent(h2)));
	    li.appendChild(a);
	    olist.appendChild(li);
	    i ++;
	}
	
	var firstDiv = document.getElementsByTagName("div").item(0);
	var body = firstDiv.parentNode;
	var newDiv = document.createElement("div");
	var title = document.createElement("h2");
	title.appendChild(document.createTextNode("Table of contents"));
	newDiv.appendChild(title);
	newDiv.appendChild(olist);
	body.insertBefore(newDiv, firstDiv);
    }	


    // if the implementation supports Mouse events and CSS from DOM Level 2,
    // let's do some fancy demo.
    if (document.implementation.hasFeature("CSS", "2.0")
	&& document.implementation.hasFeature("MouseEvents", "2.0")) {

	
	// using directly the tag name
	var h2s = document.getElementsByTagName("h2");
	var i = 0;
	
	// add the event handler on each h2
	while (i < h2s.length) {
	    var h2 = h2s.item(i);
	    h2.addEventListener("mousedown", 
				removeChildrenFromScreen, false);
	    h2.style.cursor = "pointer";
	    i ++;
	}      
    }
}

