var currentNode = null;
var collapsedImage = null;
var expandedImage = null;
var NORMAL = "transparent";
var HILIGHT = "#FFFAA0";
var SUBHILIGHT = "#FFFDDF";

function postInit() {
	var divList = document.getElementsByTagName("DIV");
	var div;
	for (var i = 0; i < divList.length; i++) {
		div = divList.item(i);
		cName = div.getAttribute("className");
		if (cName == undefined || cName == null) cName = div.getAttribute("class");
		
		if (cName == "q") {
			div.onclick = onClickQuestion;
			div.style.cursor = "pointer";
			div.style.backgroundColor = NORMAL;
			div.onmouseover = onMouseOver;
			div.onmouseout = onMouseOut;
		}
	}
	
	collapsedImage = new Image();
	collapsedImage.src = "/images/blank.gif";
	expandedImage = new Image();
	expandedImage.src = "/images/spacer.gif";
	
		
}

function onClickQuestion() {
	var ans;
	var img;
	
	if (currentNode != null) {
		ans = getAnswerNode(currentNode);
		if (ans != null) {
			ans.style.display = "none";
		}
		currentNode.style.backgroundImage = "url(" + collapsedImage.src + ")";
		currentNode.style.backgroundColor = NORMAL;
	}
	
	if (currentNode == this) {
		currentNode = null;
		return;
		
	}
	
	ans = getAnswerNode(this);
	if (ans == null) {
		alert("Error, no answer for this question.");
		return;
	}
	
	currentNode = this;
	ans.style.display = "block";
	this.style.backgroundImage = "url(" + expandedImage.src + ")";
	this.style.backgroundColor = HILIGHT;
	ans.style.borderTop = "1px solid #CCCCCC";
	ans.style.backgroundColor = SUBHILIGHT;
}

function onMouseOver() {
	this.style.backgroundColor = HILIGHT;
}

function onMouseOut() {
	this.style.backgroundColor = NORMAL;
}

function getAnswerNode(node) {	
	var ans = getNextSibling(node);
	var cName = ans.getAttribute("className");
	if (cName == undefined || cName == null) cName = ans.getAttribute("class");
	
	while (ans != null && cName != "a") {
		ans = getNextSibling(ans);
		cName = ans.getAttribute("className");
		if (cName == undefined || cName == null) cName = ans.getAttribute("class");
	}
	
	return ans;
}

/**
* Returns the next sibling with the same tag name.
*/
function getNextSibling(node) {
	var next = node.nextSibling;
	while (next.tagName != node.tagName) {
		next = next.nextSibling;
	}
	return next;
}
