// Config options.
var showJustOne = true; // Should more than one section be shown at a time.
var showAllText = "show all answers on this page";
var hideAllText = "Hide all answers on this page";

var showhideMainContainerId = "showhides";	// ID of main container that contains all other showhides	
var showhideClassName = "showhide";			// Class for blocks that have show/hide/switch elements in them.
var showhideallClassName = "showhideall";	// Class for block to replace with show/hide all link.
var switchClassName = "switch";				// Class for element to replace with show/hide link.
var showClassName = "show";					// Class for block to show, but can change to hide.
var hideClassName = "hide";					// Class for block to hide, but can change to show.
var jScriptSuffix = "js";					// Suffix to add when JavaScript is working.
var htmlShowHideBlockTag = "div";				// Html tag type that holds all showhide blocks

var showHideBlocks;	// holds references to all things to hide and show.
var showHideAllLink;	// holds reference to showhideall link

// Should be called on page load to do setup stuff.
function showhideSetup()
{
	// Add show all message
	showHideAllLink = document.getElementById(showhideallClassName);
	if(showHideAllLink != null)
	{
		showHideAllLink.innerHTML = '<strong>Please select a question </strong>or <a href="javascript:showAll()">' + showAllText + '</a>';
	}

	// Find all showhide blocks
	var blockArea = document.getElementById(showhideMainContainerId);
	var blocks = blockArea.getElementsByTagName(htmlShowHideBlockTag);
	
	// Change class name for main container 
	//blockArea.className = showhideMainContainerId + jScriptSuffix;
	
	// Add links and collect all show/hide blocks in showHideBlocks array
	showHideBlocks = new Array();
	var showhideIndex = 0; // Index in showHideBlocks Array.
	for(var i=0; i<blocks.length; i++)
	{
		if(blocks[i].className==showhideClassName)
		{
			// Loop through block's children
			for(var j=0; j<blocks[i].childNodes.length; j++)
			{
				// Find switch class element
				if(blocks[i].childNodes[j].className==switchClassName)
				{
					blocks[i].childNodes[j].innerHTML = '<a href="javascript:showHide(' + showhideIndex + ')">' + blocks[i].childNodes[j].innerHTML + '</a>';
				}
				// Find first show/hide classed element
				else if(blocks[i].childNodes[j].className==showClassName  || blocks[i].childNodes[j].className==hideClassName)
				{
					// Add them to showhideblocks array
					showHideBlocks.push(blocks[i].childNodes[j]);
					//break;
				}
			}
			showhideIndex++;
		}
		
	}//alert("show hide divs: " +  showhideIndex);

}

// Flip hide/show of passed html element
function showHide(theId)
{
	if(!showHideBlocks[theId]){/*alert('bad');*/}
	else if(showHideBlocks[theId].className == hideClassName)
	{
		if(showJustOne)
		{
			changeAll(hideClassName);
			showItAndParents(showHideBlocks[theId]);
		}
		else
		{
			showHideBlocks[theId].className = showClassName;
		}
	}
	else
	{
		showHideBlocks[theId].className = hideClassName;
	}
}

// Recurse through parent nodes and show all elements.
function showItAndParents(theElement)
{
	if(theElement.className==hideClassName)theElement.className = showClassName;
	if(theElement.parentNode != null)
	{
		showItAndParents(theElement.parentNode);
	}
}

// Change class of all blocks in showHideBlocks
function changeAll(cName)
{
	for(var i=0; i<showHideBlocks.length; i++)
	{
		showHideBlocks[i].className=cName;
	}
}

// Show all blocks and change link to 'hide all'
function showAll()
{
	if(showHideAllLink != null)
	{
		showHideAllLink.innerHTML = '<a href="javascript:hideAll()">' + hideAllText + '</a>';
	}
	changeAll(showClassName);
}

// Hide all blocks and change link to 'show all'
function hideAll()
{
	if(showHideAllLink != null)
	{
		showHideAllLink.innerHTML = '<strong>Please select a question </strong>or <a href="javascript:showAll()">' + showAllText + '</a>';
	}
	changeAll(hideClassName);
}

// Adds a css style to the styles sheet
// From http://yuiblog.com/blog/2007/06/07/style/
function addCss(cssCode)
{
	var styleElement = document.createElement("style");
	styleElement.type = "text/css";
	if (styleElement.styleSheet)
	{
		styleElement.styleSheet.cssText = cssCode;
	}
	else
	{
		styleElement.appendChild(document.createTextNode(cssCode));
	}
	document.getElementsByTagName("head")[0].appendChild(styleElement);
}
