/**
	Contains functions for toggling the visibility of tags.
	
	When a tag is of class "clicker-text", that tag is converted
	to class "clicker" on load. That tag becomes a button that toggles
	the visibility of the next tag.
	
	The script must be loaded in the HTML file where it is used, and
	the function toggleNextByTagAndClassName must be called on load
	with the appropriate parameters.
*/

/**
	Given an element, toggles the next element.
*/
function toggleNext(element) 
{
	var nextElement = element.nextSibling;
	
	while(nextElement.nodeType != 1) //what is this 1 for?
	{
		nextElement = nextElement.nextSibling;
	}

	/*element.style.backgroundImage = ((nextElement.style.display=="none") ? "url('/stylesheet/bullet2.png')"	: "url('../stylesheet/bullet.png')");*/
	/*element.style.backgroundColor = ((nextElement.style.display=="none")  ? "red" : "blue");*/
	nextElement.style.display = ((nextElement.style.display=="none") ? "block" : "none");	
}

/**
	Get all elements of the document with the given tag name and class.
*/
function getElementsByTagAndClassName(tag, className) 
{
	var tags = document.getElementsByTagName(tag);
	var elementList = new Array();
	
	for (var i = 0; i < tags.length; i++) 
	{
		var rExpression = new RegExp("(^|\\s)" + className + "(\\s|$)");
		
		if (rExpression.test(tags[i].className)) 
		{
			elementList.push(tags[i]);			
		}
	}
	
	return elementList;	
}

/**
	Makes clicker-text objects clicker objects, and override the onclick handler
	so that the text can serve qas toggle button.
*/
function toggleNextByTagAndClassName(tag, className) 
{
	var newClassName = "clicker";
	clickers = getElementsByTagAndClassName(tag, className);
	
	for (var i = 0; i < clickers.length; i++) 
	{
		clickers[i].className += " " + newClassName;
		clickers[i].onclick = function() 
		{
			toggleNext(this)
		};
		
		//make them invisible *now*
		toggleNext(clickers[i]);
	}
}