// start by getting all the questions and answers
// these will be put into arrays

var questions = document.getElementsByTagName('dt');
var answers = document.getElementsByTagName('dd');
var headers = document.getElementsByTagName('h3');
var sections = document.getElementsByTagName('dl');

// function for the link that turns them all off
function toggleAllOff(){
	for (var i = 0; i < answers.length; i++) {  // turns off all the dd's
		answers[i].className = 'hide';
	}

	//Commented out so that page loads with questions displayed
	/*for (var i = 0; i < sections.length; i++) { // turns off all the dl's
		sections[i].className = 'hide';
	}*/

	
}

// function for the link that turns them all on
function toggleAllOn(){
	for (var i = 0; i < answers.length; i++) {  // turn on all the dd's
		answers[i].className = 'show';
	}
	
	for (var i = 0; i < sections.length; i++) {  // turn on all the dl's
		sections[i].className = 'show';
	}

}


function toggleNext(element) {
	var next = element.nextSibling;
	while(next.nodeType != 1) next=next.nextSibling; // if it gets to a non-element node, go to the next one
	next.className=((next.className=="hide") ? "show" : "hide");

}


//makes the definition lists click-able
function displayToggle(){
	
	toggleAllOff(); // calls the toggle all off function to turn all the answers off when the page is loaded	
	 
	 for (i=0; i<questions.length; i++) { // loops through the questions 
		 questions[i].onclick=function() { // shows the answers onclick
		 	toggleNext(this);
		}
	 }
	 
	 for (i=0; i<headers.length; i++) { // loops through the headers a
		 headers[i].onclick=function() { // shows the dl sections on click
		 	toggleNext(this);
		}
	 }
	 	 
	 
}

function toggleLinked(){
	hashString = self.document.location.hash.substring(1);
	if(hashString.length > 0){
		for (i=0; i<questions.length; i++) {
			temp_links = questions[i].getElementsByTagName('a');
	
			if(temp_links[0].name == hashString){
				break;
			}
		}
		toggleNext(questions[i]);
		var parent = questions[i].parentNode;
		
		var section_change = document.getElementById(parent.id);
		section_change.className = 'show';
		window.location.hash = hashString;
	}
}

function toggleLinked_gloss(value){
	hashString = value.hash.substring(1);
	if(hashString.length > 0){
		for (i=0; i<questions.length; i++) {
			temp_links = questions[i].getElementsByTagName('a');
	
			if(temp_links[0].name == hashString){
				break;
			}
		}
		toggleNext(questions[i]);
		var parent = questions[i].parentNode;
		
		var section_change = document.getElementById(parent.id);
		section_change.className = 'show';
		window.location.hash = hashString;
	}
}

// initiates the click-able dt's when the page loads
window.onload=function() {
	displayToggle();
	toggleLinked();
}
