// this file contains all of the functions and information we need in order to
// run our trivia page/plugin

// load a trivia question from XML at the server
function question()
{
	dojo.xhrGet({
		url:		"trivia_questions.php",
		handleAs:	"json",
		load:		question_loader
	});
}
function question_loader()
{
	var data = (question_loader.arguments[0]) ? question_loader.arguments[0] : false;
	var args = (question_loader.arguments[1]) ? question_loader.arguments[1] : false;
	
	// the question...
	dojo.byId("tquestion").innerHTML = "Q: "+data.question;
	// answers
	var answerhtml = "";
	var i;
	for( i=0; i<data.answers.length; i++ )
	{
		var answer = data.answers[i];
		answerhtml += "<div class=\"tanswer\"> &nbsp; ";
		answerhtml += "<input type=\"radio\" name=\"answerid\" id=\"answerid_"+answer.id+"\" value="+answer.id;
		answerhtml += " onclick=\"select_answer(this.value)\"> ";
		answerhtml += unescape(answer.text);
		answerhtml += "</div>";
	}
	dojo.byId("options").innerHTML = answerhtml;
	
	// the correct answer
	answerid   = data.answerid;
	answertext = data.answertext;
	answerlink = data.answerlink;
	answerlinkdisplay = data.answerlinkdisplay;
}

var errortext    = "<span style=\"font-weight:bold;font-size:20pt;\">Sorry!</span> &nbsp; &nbsp; That's incorrect, please try again.";
var successtext  = "<span style=\"font-weight:bold;font-size:20pt;\">Correct!</span><br><br> [answer] <br><br>";
	successtext += "<span style=\"float:left;\"><a href=\"trivia.php\" class=\"correctlink\">Try another question</a></span>";
	successtext += "<span style=\"float:right;\"><a href=\"[answerlink]\" class=\"correctlink\">[answerlinkdisplay]</a></span>";
var answertext;
var answerid;
var choiceid = 0;
// set the selected answer
function select_answer() { choiceid = select_answer.arguments[0]; }
// check the answer given by the user
function answer()
{
	// hide the answer text again
	dojo.fadeOut({node:dojo.byId("answertext"),duration:500}).play();
	// sanity
	if( !choiceid ) { alert("Please select an answer."); return; }
	// check the answer
	setTimeout( check_answer, 1000 );
}
function check_answer()
{
	// check the choiceid against the correct answerid
	var stylus = (choiceid != answerid) ? "wronganswer" : "correctanswer";
	var html   = (choiceid != answerid) ? errortext : successtext.replace( /\[answer\]/, unescape(answertext) );
		html   = (choiceid != answerid) ? errortext : html.replace( /\[answerlink\]/, unescape(answerlink) );
		html   = (choiceid != answerid) ? errortext : html.replace( /\[answerlinkdisplay\]/, unescape(answerlinkdisplay) );
	// add it to the page
	dojo.byId("answertext").className = stylus;
	dojo.byId("answertext").innerHTML = html;
	dojo.fadeIn({node:dojo.byId("answertext"),duration:500}).play();
//	dojo.byId("answertext").style.visibility = "visible";
}

var hilited = "#FF00FF";
var normal = "#9900FF";
// hilite the submit button
function hilite() { dojo.byId("submit").style.backgroundColor = hilited; }
function unhilite() { dojo.byId("submit").style.backgroundColor = normal; }
