var questions = new Array;
var answers = new Array;

var nowopen = '';
var nowclosed = '';

function hide_text(object) {
	if (!object) return false;
	// Re-display any old ones now closed
	if (nowclosed) nowclosed.style.display = '';
	sleep(50);

	object.style.display = 'none';
	nowclosed = object;
	return true;
}

function show_text(object) {
	if (!object) return false;
	// Close any old ones still open
	if (nowopen) nowopen.style.display = '';
	sleep(50);

	object.style.display = 'block';
	nowopen = object;
	return true;
}

function sleep(msec) {
	date = new Date();
	var curDate = null;
	do {
		var curDate = new Date();
	} while(curDate-date < msec);
} 

function answer_this(question) {
    for ( var i = 0; i < questions.length; i++ ) {
        if (question == questions[i]) {
            return answers[i];
        }
    }
    return false;
}

function prepare_FAQ(open_event, add_clicks) {
    if (!open_event) open_event = "onmouseover";

	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
	var paras = document.getElementsByTagName("p");
	for ( var i = 0; i < paras.length; i++ ) {
		if (paras[i].className.match(/\bquestion\b/)) {
          if (add_clicks) {
            paras[i].appendChild(document.createElement('br'));
            var click = document.createElement('span');
            click.appendChild(document.createTextNode(add_clicks));
            paras[i].appendChild(click);
            questions.push(click);
          }
          else {
            questions.push(paras[i]);
          }
        }
        else if (paras[i].className.match(/\banswer\b/)) {
            answers.push(paras[i]);
        }
    }

    if (questions.length != answers.length) {
        alert("WARNING: " + questions.length + " questions and " + answers.length + " answers");
    }
    
    for ( var i = 0; i < questions.length; i++ ) {
        var do_this = "questions[i]." + open_event +
            " = function(){ if (this.tagName == 'SPAN') hide_text(this); show_text(answer_this(this)) }";
        eval(do_this);
        if (open_event == "onmouseover") questions[i].onmouseout = questions[i].onmouseover;
	}
	return true;
}

