/*
written by Nathan Lounds - http://vudat.msu.edu 
last updated May 2nd, 2007
*/

Behaviour.addLoadEvent(function() {
	var my_path = "images/";
	/*//<![CDATA[
	// loading the stylesheet into the document header
	if(document.createStyleSheet) { // non-standard IE way of doing it
		document.createStyleSheet(my_path+'styles_qa.css');
	} else { // do it the W3C DOM way
		var newCSS=document.createElement('link');
		newCSS.rel='stylesheet';
		newCSS.href=my_path+'styles_qa.css';
		newCSS.type='text/css';
		document.getElementsByTagName("head")[0].appendChild(newCSS);
	}
	//]]>
	
	/* Set the functionality for the Questions */
	questions = document.getElementsBySelector('.qa .q');
	var doQ = true;
	if(questions) {
		for (i = 0; i < questions.length ; i++)	{
			doQ = true;
			if(questions[i].childNodes[0].childNodes[0]) {
				if(questions[i].childNodes[0].childNodes[0].nodeValue=="QUESTION") {
					doQ = false;
				}
			}
			if(doQ == true) {
				var q_div = document.createElement('div');
				q_div.className = 'question';
				var q_question = document.createTextNode('QUESTION');
				q_div.appendChild(q_question);
				questions[i].insertBefore(q_div,questions[i].childNodes[0]);
				
				var show_hide_image = document.createElement('img');
				show_hide_image.setAttribute("src",my_path+"show_answer.gif");
				show_hide_image.setAttribute("alt","Show Answer");
				show_hide_image.setAttribute("width","96");
				show_hide_image.setAttribute("height","21");
				show_hide_image.onclick = function(){
					var me = this;
					/* 
					Firefox treats whitespace as a node, so I use this function to get the next non-whitespace sibling
					I can't just use var brother = me.parentNode.nextSibling;
					*/
					var answer = getNextSibling(me.parentNode.parentNode);
					if(answer){
						if(answer.nodeType!=3){
							if(answer.style.display=='block') {
								answer.style.display = 'none';
								me.setAttribute("alt","Show Answer");
								me.src = my_path+"show_answer.gif";
							} else {
								answer.style.display = 'block';
								me.setAttribute("alt","Hide Answer");
								me.src = my_path+"hide_answer.gif";
							}
						}
					}
				}
				var show_answer = document.createElement('div');
				show_answer.className = 'show_answer';
				show_answer.appendChild(show_hide_image);
				questions[i].appendChild(show_answer);
			}
		}
	}
	
	/* Set the properties of the Answers */
	answers = document.getElementsBySelector('.qa .a');
	var doA = true;
	if(answers) {
		for (i = 0; i < answers.length; i++) {
			doA = true;
			if(answers[i].childNodes[0].childNodes[0]) {
				if(answers[i].childNodes[0].childNodes[0].nodeValue=="ANSWER") {
					doA = false;
					//console.log("already there");
				}
			}
			if(doA == true) {
				var a_div = document.createElement('div');
				a_div.className = 'answer';
				var a_answer = document.createTextNode('ANSWER');
				a_div.appendChild(a_answer);
				answers[i].insertBefore(a_div,answers[i].childNodes[0]);
			}
		}
	}
	
	/* Set up the functionality of the Show all link */
	qa_all = document.getElementsBySelector('.qa_all');
	if(qa_all) {
		for (i = 0; i < qa_all.length ; i++) {
			var all_image = document.createElement('img');
			all_image.src = my_path+"show_all.gif";
			all_image.setAttribute("alt","Show All Answers");
			show_state = 1;
			qa_all[i].appendChild(all_image);
			qa_all[i].onclick = function(){
				var the_answers = document.getElementsBySelector('.qa .a');
				var the_buttons = document.getElementsBySelector('.qa .show_answer img');
				var the_alls = document.getElementsBySelector('.qa_all');
				var first_image = the_alls[0].childNodes[0].src;
				if (show_state==1) {
					if (the_answers) {
						for (i = 0; i < the_answers.length ; i++) {
							the_answers[i].style.display = 'block';	
							the_buttons[i].src = my_path+"hide_answer.gif";
						}
					}
					if (the_alls) {
						for (i = 0; i < the_alls.length ; i++) {
							the_alls[i].childNodes[0].src = my_path+"hide_all.gif";
						}
					}
					show_state=0;
				} else {
					if (the_answers) {
						for (i = 0; i < the_answers.length ; i++) {
							the_answers[i].style.display = 'none';
							the_buttons[i].src = my_path+"show_answer.gif";
						}
					}
					if (the_alls) {
						for (i = 0; i < the_alls.length ; i++) {
							the_alls[i].childNodes[0].src = my_path+"show_all.gif";
						}
					}
					show_state=1;
				}		
			}
		}
	}
	//alert(qa_all.length+":"+questions.length+":"+answers.length);
	
	/* This function returns the next sibling node that isn't whitespace */
	function getNextSibling(startBrother) {
		//alert(startBrother.className);
  		endBrother=startBrother.nextSibling;
		while(endBrother.nodeType!=1) {
			if(endBrother.nextSibling) {
				endBrother = endBrother.nextSibling;
			} else {	
				break;
			}
		}
		return endBrother;
	}
})
