// this sets up content so that text is revealed while you mouse over a trigger
// the expected structure is such that all expanding text is surrounded by a block element, within which the triggers are either a unique type 
// of element (eg. h3), or have a specific class name.  The content to be revealed should appear in a single block element immediately after
// the trigger.
// hideNextOnly (global): boolean, if true only the block that follows the trigger is hidden, 
//	otherwise all block elements up to the next trigger are hidden

var delayRemoval = true;
var lastShown;

function hideall (container, triggerName, classNm){
	// container: string, id of block element surrounding all the expanding content
	// triggerName: string, tag name of block element you click on to expand/collapse content
	// classNm: string, empty string or name of class used to identify trigger elements
	// 	this helps if you use the same elements for trigger and hidden content 
	// hideNextOnly (global): boolean, if true only the block that follows the trigger is hidden, 
	//	otherwise all block elements up to the next trigger are hidden
	if (document.getElementById && document.getElementsByTagName){ 
		// get a list of trigger nodes
		var triggers = document.getElementById(container).getElementsByTagName(triggerName);

		// set  class names and functions on trigger and hidden content
		for (var i=0;i<triggers.length;i++){ 
			if (classNm == '' || triggers[i].className == classNm) {
				// set up triggers
				var trigger = triggers[i];
				trigger.onmouseover = function() {unhide(this, classNm);};
				if (! delayRemoval) { trigger.onmouseout = function() {hide(this, classNm);}; }
				
				nextElement = nextElementAfter(trigger);
				if (nextElement.className) {
					nextElement.className = nextElement.className+' hiddenContent';
					}
				else nextElement.className = 'hiddenContent';
				}
			}
		}
	return false;
	}
				
				
function nextElementAfter ( node ) { //alert('node='+node);
	if (node.nextSibling == null) { return false; }
	var newnode = node.nextSibling; //alert('nextsibling=',node.nextSibling);
	while ( newnode.nodeType != 1 ) {
		if (newnode.nextSibling == null) { return false; }
		newnode = newnode.nextSibling;
		}  //alert('newnode='+newnode);
	return newnode;
	}

function unhide (trigger, classNm) {
	if (lastShown) { lastShown.className = lastShown.className.replace(/revealedContent/, 'hiddenContent'); }
	nextElement = nextElementAfter(trigger);//alert('nextEl='+nextElement);
	if (nextElement) { 
		nextElement.className = nextElement.className.replace(/hiddenContent/, 'revealedContent'); 
		lastShown = nextElement;
		}
	}

function hide (trigger, classNm) {
	nextElement = nextElementAfter(trigger);
	if (hideNextOnly) {
		nextElement.className = nextElement.className.replace(/revealedContent/, 'hiddenContent');
		}
	else {
		while (nextElement && nextElement.className != classNm) {
			nextElement.className = nextElement.className.replace(/revealedContent/, 'hiddenContent');
			nextElement = nextElementAfter(nextElement);
			}
		}
	}


// -------------------------------- here be rubbish ------------------------------------------------------				

function oldhideall () {
		var links = document.getElementsByTagName("div");

		// add the event handler on each toc1
		var i = 0; var link;
		while (i < links.length) {
		 	link = links.item(i);
			if (link.className == 'item') { alert(link.nodeName);
				removeChildrenFromScreen(link); 
   				link.style.cursor = "pointer";
				}
   			i ++;
			}      
		}

function removeChildrenFromScreen(e) { //alert ('hello' );
	//var node = e.nextSibling; alert( node.nodeName );
	//var node = e.childNodes[2]; //alert( node.nodeName );
	var node = nextElementAfter(e.firstChild);
	alert(node.nodeName);
   	if (node.nodeType == 1) {
		node.style.display = "none";
   		}
	e.onmouseover = function() {reveal(this);};
	}


function reveal(e) { //alert('here');
	// remove any currently displayed text first
	var details = document.getElementsByTagName("div");
	var i = 0; var detail;
	while (i < details.length) {
		detail = details.item(i);
		if (detail.className == 'detail' && detail.firstChild.nodeType == 1) { //alert( detail.nodeName); alert(detail.firstChild.nodeName);
			detail.firstChild.style.display = 'none'; 
			}
		i ++;
		}      

	// var descriptionNode = e.nextSibling; 
	// var descriptionNode = e.childNodes[2]; 
	descriptionNode = nextElementAfter( e.firstChild );
	var targetID = e.parentNode.className; //alert(target);
	var targetNode = document.getElementById(targetID).firstChild; //alert(targetNode.nodeName);
	var copiedNode = descriptionNode.cloneNode(true);
	copiedNode.style.display = 'block';
	var removedNode = document.getElementById(targetID).replaceChild(copiedNode, targetNode);
	}


function firstElementIn ( node ) { 
	var currentnode = node.firstChild;
	while ( currentnode.nodeType != 1 ) {
		currentnode = currentnode.nextSibling;
		}
	return currentnode;
	}

function allElementsAfter ( node ) { 
	var newnode = node.nextSibling;
	while ( newnode.nodeType != 1 ) {
		newnode = newnode.nextSibling;
		}
	return newnode;
	}

function nextElementNamed ( nType, node ) {
	var newnode = node.nextSibling; 
	while ( newnode.nodeName != nType ) {
		newnode = newnode.nextSibling;
		}
	return newnode;
	}

