//Hides all elements with the specified class
function closeAllByClass(tclassName)
{
	var allDivs = document.getElementsByTagName('div');
	for( i = 0; i < allDivs.length; i++)
	{
		if(allDivs[i].className == tclassName)
		{
			allDivs[i].style.display = 'none';
		}
	}
}




/* ------------------
	-Toggle Id-
	Toggles the visibility of element whose id is given.
	
	Takes: the element making the call(this), and the class name of the element to be toggled
	Returns: nothing
	Depends on findNearestByClass & findChildByClass
--------------------*/


function toggleId(element_id)
{
	var target_element = document.getElementById(element_id);
	//if target is visible, then hide, else show. 
	return target_element.style.display = (target_element.style.display == 'block') ? target_element.style.display = 'none' : target_element.style.display = 'block';
}


/* ------------------
	-Toggle Nearest Class-
	Toggles the visibility of the first sibling node with the class name given.
	Also works on uncles and aunts
	Takes: the element making the call(this), and the class name of the element to be toggled
	Returns: nothing
	Depends on findNearestByClass & findChildByClass
--------------------*/
function toggleNearestClass(element, theclass)
{
	//find the node that needs to toggle
	var target_element = findNearestByClass(element, theclass);
	//If not found give up
	if(target_element.className != theclass)
	{
		alert('toggle_sibling: Sibling not found. Cant display Dropdown.');
		return;
	}

	//
	if(target_element.style.display == 'block')
	{
		target_element.style.display = 'none'
		element.title = "Click to Expand";
	}
	else
	{
		target_element.style.display = 'block'
		element.title = "Click to Close";
	}
	return;
	
}

/*-----------------------
	-Find Nearest By Class-
	Returns the closest relative of the element given that has the class name given.
	Takes: starting element, and the class name to search for
	Returns:  element found, returns null if none are found.
	Depends on findChildByClass
-------------------------*/
function findNearestByClass(element, theclass)
{		
		//check siblings and decendants
		var target_element = findChildByClass(element.parentNode, theclass)
		if(target_element != null)
		{
			return target_element;
		}

		//Check parent's siblings
		target_element = findNearestByClass(element.parentNode.parentNode, theclass)
		if(target_element != null)
		{
			return target_element;
		}

		return null;
}

/*-----------------------
	-Find Child By Class-
	Returns the first sibling of the element given that has the class name given.
	Takes: starting element, and the class name to search for
	Returns:  element found, returns null if none are found.
-------------------------*/
function findChildByClass(element, theclass)
{
	//check self
	if(element.className == theclass)
	{
		return element;
	}
	//Check children
	if (element.hasChildNodes())
	{
		for(i=0; i < element.childNodes.length; i++)
		{
			if(element.childNodes[i].nodeType == 1)
			{
				if(element.childNodes[i].className == theclass)
				{
					return element.childNodes[i];
				}
			}


		}
		//Checking Grandchildren
		for(i=0; i < element.childNodes.length; i++)
		{

			if(element.childNodes[i].nodeType == 1)
			{
				target_element = findChildByClass(element.childNodes[i], theclass);
				if(target_element != null && target_element.className == theclass)
				{
					return target_element;
				}
			}

		}
	}
	return null;
}