/*
nav.js
Written by Gabriel Grant
Feb 2006
*/



//takes a node (leaf) either as a string id or as an object and returns it's closest ancestor of a given type (parent)
//returns false if no such ancestor is found, and returns itself (as an object) if it is of the given type
function climbTreeTo(leaf, parent)
{
	parent = parent.toLowerCase();
	leaf=$(leaf);
	//opera.postError('Leaf tagName: '+leaf.tagName.toLowerCase());
	//opera.postError('Parent tagName: '+parent);
	if(leaf.tagName.toLowerCase() == "html")
	{
		//opera.postError('No '+parent+'s present. Returning false');
		return false
	}
	else if(leaf.tagName.toLowerCase() == parent)
	{
		//opera.postError('Leaf = '+leaf.tagName.toLowerCase()+' Parent = '+parent+' are equal, returning leaf');
		return leaf
	}
	else
	{

		leaf = leaf.parentNode;
		leaf = climbTreeTo(leaf, parent);
	}
	return leaf
}





function hide(element)
{
	element.style.display = 'none';
}

function show(element)
{
	element.style.display = 'block';
}


function $()
{
  var elements = new Array();

  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);

    if (arguments.length == 1)
      return element;

    elements.push(element);
  }

  return elements;
}


function getElementsByClassName(className, parentElement)
{
	var children = ($(parentElement) || document.body).getElementsByTagName('*');
	var elements = new Array();
	for (i=0;i<children.length;i++)
	{
		if (children[i].className.match(new RegExp("(^|\\s)" + className + "(\\s|$)")))
		{
			elements.push(child);
		}
	}
    return elements;
}

function hasClass(element, className)
{
	if (element.className.match(new RegExp("(^|\\s)" + className + "(\\s|$)")))
	{
		return true;
	}
	else
	{
		return false;
	}
}

function eventElement(event)
{
	return event.target || event.srcElement;
}

/*var navContainer = $("navContainer");
//var activeSection = getElementsByClassName("activeSection", "navContainer");
var activeLink = getElementsByClassName("activeLink", "navContainer");
*/
function toggleNav(section)
{
	while ( section.tagName.toLowerCase() != "li" )
	{
		section = section.parentNode;
	}

	/*var sectionList = section.firstChild;

	while (sectionList.tagName.toLowerCase() != "ul")
	{
		sectionList = sectionList.nextSibling;
	}*/

	var sectionList = section.getElementsByTagName('ul');
	sectionList = sectionList[0];

	//opera.postError(section.tagName);
	//opera.postError(sectionList.tagName);

	if (( (hasClass(section, "activeSection")) && (sectionList.style.display != 'none'))||(sectionList.style.display == 'block' && !(hasClass(section, "activeSection")) ))
	{
		hide(sectionList);
	}
	else
	{
		show(sectionList);
	}

	return false;
}

/* Add this to each section link
onClick="toggleNav(this);"

*/
