function revealDetails(element,node) {
  var detailsNode = findSubSequentNodeByClass(element,"details");
  removeAllChildren(detailsNode);
  detailsNode.appendChild(node); 
}

function removeAllChildren(node) {
  while(node.hasChildNodes()) {
    node.removeChild(node.childNodes[0]);
  }
}

function tooltipNode(nodes) {
  // Create a div
  var rv = document.createElement("div");

  // Set it's class to highlight
  var attr = document.createAttribute("class");
  attr.value = "tooltip";
  rv.setAttributeNode(attr);

	var closer = document.createElement("span");

	var image = document.createElement("img");
	closer.appendChild(image);
	attr = document.createAttribute("src");
	attr.value = "http://www.physics.rutgers.edu/~hagbard/killcorner.png";
	image.setAttributeNode(attr);
	
	// Set closers class
	attr = document.createAttribute("class");
	attr.value = "closer";
	closer.setAttributeNode(attr);
	
	// Set closers title
	attr = document.createAttribute("title");
	attr.value = "Click to close";
	closer.setAttributeNode(attr);
	
	// Set closer onclick
	closer.onclick = function() { removeAllChildren(closer.parentNode.parentNode); }
	
	rv.appendChild(closer);

  for(var i = 0; i < nodes.length; i++) {
    rv.appendChild(nodes[i]);
  }

  return rv;
}

function DescriptionNode() { 
	divs = Array();
	
	this.addSection = function() {
		// Create a div
	 	var div = document.createElement("div");

	 	// Set it's class to highlight
  	var attr = document.createAttribute("class");
  	attr.value = "highlight";
  	div.setAttributeNode(attr);
  	divs.push(div);
  	return new DescriptionSectionNode(div);
  }
  
  this.getNode = function() { 
  	return tooltipNode(divs);
  }
}

function DescriptionSectionNode(div) {
	
	this.addSection = function() {
		// Create a div
	 	var newdiv = document.createElement("div");

	 	// Set it's class to highlight
  	var attr = document.createAttribute("class");
  	attr.value = "highlight";
  	newdiv.setAttributeNode(attr);
  	div.appendChild(newdiv);
  	return new DescriptionSectionNode(newdiv);
  }
	
	this.addBoldLine = function(text) {
		// Add a bold node
  	var bold = document.createElement("b");
  	div.appendChild(bold);
		div.appendChild(document.createElement("br"));
  
  	// Add text to the bold node
  	textNode = document.createTextNode(text);
  	bold.appendChild(textNode);
	}	

	this.addLine = function(text) {
		this.addText(text);
		this.addNode(document.createElement("br"));
	}
	
	this.addText = function(text) {
		// Add a text node for income
		textNode = document.createTextNode(text);
		div.appendChild(textNode);
	}
	
	this.addSubscriptText = function(text) {
		// Add a subscript node
		var subscript = document.createElement("sub");
		div.appendChild(subscript);
		
		// Add text to the subscript node
		textNode = document.createTextNode(text);
		subscript.appendChild(textNode);
	}
	
	this.addNode = function(node) {
		div.appendChild(node);
	}
}
      
function getNodeByClass(parentNode,className) {
  var currentNode = parentNode;
  var newNode = null;
  if(parentNode == null){
    return null;
  } 
  if(currentNode.className == className){
    return currentNode;
  }
  var i = 0;
  for(i=0;i < parentNode.childNodes.length;i++) {
    currentNode = parentNode.childNodes[i];
    newNode = getNodeByClass(currentNode,className);
    if(newNode != null) {
      return newNode;
    }
  }
  return null;
}

function findSubSequentNodeByClass(node,className) {
  var currentNode = node;
  var newNode;
  if(node == null) {
    return null;
  }
  while(currentNode.nextSibling != null) {
    currentNode = currentNode.nextSibling;
    newNode = getNodeByClass(currentNode,className);
    if(newNode != null) {
      return newNode;
    }
  }
  return findSubSequentNodeByClass(node.parentNode,className);
}

function detailedCell(text,format,title,onclick) {
	// Add a cell to row
	var cell = document.createElement("td");
	
	if(title != null ) {	
	  // Add title to cell
	  var attr = document.createAttribute("title");
	  attr.value = title;
	  cell.setAttributeNode(attr);
  }
      
  if(onclick != null) {
  	// Set onclick
  	if(typeof onclick == 'function'){
  		cell.onclick = onclick;
  	} else {
		  attr = document.createAttribute("onclick");
		  attr.value = onclick;
	  	cell.setAttributeNode(attr);
	  }
  }
      	      
  // Add text to cell
  var tn;
  if(format == null) {
  	tn = document.createTextNode(text);
  } else { 
  	tn = formatNode(text,format);
  }
  cell.appendChild(tn);
  return cell;
}

function generic_onload(myfunc) {
	//setup onload function
	if(typeof window.addEventListener != 'undefined')
	{
		//.. gecko, safari, konqueror and standard
		window.addEventListener('load', myfunc, false);
	}
	else if(typeof document.addEventListener != 'undefined')
	{
		//.. opera 7
		document.addEventListener('load', myfunc, false);
	}
	else if(typeof window.attachEvent != 'undefined')
	{
		//.. win/ie
		window.attachEvent('onload', myfunc);
	}
	
	//** remove this condition to degrade older browsers
	else
	{
		//.. mac/ie5 and anything else that gets this far
		
		//if there's an existing onload function
		if(typeof window.onload == 'function')
		{
			//store it
			var existing = onload;
			
			//add new onload handler
			window.onload = function()
			{
				//call existing onload function
				existing();
				
				//call myfunc onload function
				myfunc();
			};
		}
		else
		{
			//setup onload function
			window.onload = myfunc;
		}
	}
}
	