// JavaScript Document

window.onload = init;

// init() - Initialize the functionality on the page.
function init()
{
	if (!initFAQ())
	{
		alert('There are no FAQs on this page. Exiting.');
	}
	
	var expandButton = document.getElementById("expandAll");
	expandButton.onclick = expandAll;
	
	var collapseButton = document.getElementById("collapseAll");
	collapseButton.onclick = collapseAll;
}

// initFAQ() - function to find all the DL tags with the class 'faq' and set up the DT and DD objects.
function initFAQ()
{
	var dlLists = document.getElementsByTagName("dl"); //Get all the DLs on this page.
	for (var count=0; count<dlLists.length; count++)
	{
		if (dlLists[count].className.indexOf("faq") > -1) // Does this DL have class of 'faq'?
		{
			makeFAQList(dlLists[count]); //take that DL list and turn it into a clickable FAQ list.
		}
	}
	return true;
}

// makeFAQList(dlList) - Take the DL list object and make the items clickable.
function makeFAQList(dlList)
{
	//first check to see if the list actually exists.
	if (!dlList || !dlList.hasChildNodes())
	{
		alert("Can't make the FAQ list. The DL list is missing or does not contain any terms and definitions."); // Notify the user of the problem.
		return false;	// We don't want to continue.
	}
	
	// If we're here, then we have a list. Cycle through the DL list and append the proper methods and properties to the objects.
	var listLinks = dlList.getElementsByTagName("dt");
	for (var count = 0; count < listLinks.length; count++)
	{
		listLinks[count].expanded = false; // initally, this is set to false to indicate the question is collapsed.
		listLinks[count].answer = getAnswer(listLinks[count]); // next sibling should point to the DD tag associated to this DT tag.
		listLinks[count].answer.className += " collapsed";
		
		// Whenever someone clicks on the DT, swap out the class for the DD tag.
		listLinks[count].onclick = function() {
			toggleAnswer(this);
		}
	}
}

function toggleAnswer(question)
{
	if (question.expanded){
		question.answer.className = question.answer.className.replace(/expanded/gi, "collapsed");
		question.expanded = false;
	}
	else
	{
		question.answer.className = question.answer.className.replace(/collapsed/gi, "expanded");
		question.expanded = true;
	}
	
}

// getAnswer(questionNode) - returns the DD object which is directly after the questionNode (the DT tag).
function getAnswer(questionNode)
{
	var answerNode = questionNode;
	
	// cycle through the nodes one at a time until we find the next DD tag. If we don't find one, then simply drop through.
	do
	{
		if (answerNode.tagName == 'DD'){
			return answerNode;
		}
		else
		{
			answerNode = answerNode.nextSibling;
		}
	} while(answerNode);
	
	return false;
}

function expandAll()
{
	var theDTs = document.getElementsByTagName("dt");
	
	for (var count=0; count<theDTs.length; count++)
	{
		if(theDTs[count].expanded == false)
		{
			toggleAnswer(theDTs[count]);
		}
	}
}

function collapseAll()
{
	var theDTs = document.getElementsByTagName("dt");
	
	for (var count=0; count<theDTs.length; count++)
	{
		if(theDTs[count].expanded == true)
		{
			toggleAnswer(theDTs[count]);
		}
	}
}
