

function preloadQuestionImage(q) {
	
	$("#preload IMG").unbind();
	$("#preload").html("");
	$("#preload").html("<IMG src=\"" + q.imgUrl + "\">");
	$("#preload IMG").bind("load", function() {
	
		$("#questionImage").css({
			"background": "transparent url('" + q.imgUrl + "') -40px -48px no-repeat"
		});
	
		imageBars();		
		enableDisplay();
		enableProgressbar();
		
		if(soundEnabled) {
			if(q.sndObj!=null && q.sndObj.readyState==3) {
				q.sndObj.play();
			} else {
				progressbar();
			}
		} else {
			progressbar(); 
		}
		
	});
}

function preloadExplanationImage(q) {
	$("#preload IMG").unbind();
	$("#preload").html("");
	$("#preload").html("<IMG src=\"" + q.imgUrl + "\">");
	$("#preload IMG").bind("load", function() {
	
		if(msie) {
			$("#tabs").data("disabled.tabs", []);
			$("#tabs").tabs("select", 5);
			$("#tabs").data("disabled.tabs", [0,1,2,3,4]);
		} else {
			$("#tabs").data("disabled.tabs", []);
			$("#tabs").tabs("select", 5);
			$("#tabs").data("disabled.tabs", [2]);
		}
		
		
		$("#explanationImage").css({
			"background": "transparent url('" + q.imgUrl + "') -40px -48px no-repeat"
		});
		
		if(soundEnabled) {
			if(q.explObj!=null && q.explObj.readyState==3) {
				q.explObj.play();
			}
		}
		
	});
}

function next() {

	if(soundEnabled) {
		soundManager.stopAll();
	}
	window.clearTimeout(timer);
	disableDisplay();
	
	if(data!=null && data.questions!=null && question!=null && question>0 && data.questions[question]!=null) {
		// score per vraag
		if(data.questions[question].kind=="O") {
			if(parseFloat(data.questions[question].answer)!=parseFloat(data.questions[i].given)) {
				questionResults(data.questions[question].id, 0);
			} else {
				questionResults(data.questions[question].id, 1);
			}
		} else {
			if(data.questions[i].answer!=data.questions[question].given) {
				questionResults(data.questions[question].id, 0);
			} else {
				questionResults(data.questions[question].id, 1);
			}
		}
	}
	
	if(question!=null && question>=-1 && data!=null && data.questions!=null) {
		question++;
		if(question>=data.questions.length) {
			deinitDocumentEvents();
			
			// Wachtscherm
			$("#questionImage").css({
				"background": "transparent url('../images/uitslagloader.gif') -40px -48px no-repeat"
			});
			
			timer = window.setTimeout("showResults()", 3000);
		} else if(question<data.questions.length) {
			preloadQuestionImage(data.questions[question]);
		}
	}
}

function questionResults(id, score) {
	$.get("../json/vraag.php", {"id": id, "score": score});
}

function examResults(id, score) {
	$.get("../json/uitslag.php", {"id": id, "score": score});
}

function enableProgressbar() {
	$("#progressbar").progressbar('value', 100);
	$("#progressbar").progressbar("enable");
}

function disableProgressbar() {
	$("#progressbar").progressbar("disable");
}


function progressbar() { 
	if(question!=null && question>=0 && data!=null && data.questions!=null && data.questions[question]!=null && data.questions[question].duration!=null) {
		var value = $("#progressbar").progressbar('option', 'value');
		value -= 10.0/parseInt(data.questions[question].duration);
		if(value>=0) {
			$("#progressbar").progressbar('value', value);
			window.clearTimeout(timer);
			timer = window.setTimeout('progressbar()', 100);
		} else {
			disableProgressbar();
			next();
		}
	} else {
		disableProgressbar();
		next();
	}
}

function setAnswer(val) {
	if(answerEnabled && data!=null && data.questions!=null && question!=null && question>=0 && data.questions[question]!=null) {
		if(val=="") {
			data.questions[question].given = "";
		} else if(data.questions[question].kind=="B" && data.questions[question].given=="" && val=="J") {
			data.questions[question].given = "J";
		} else if(data.questions[question].kind=="B" && data.questions[question].given=="" && val=="N") {
			data.questions[question].given = "N";
		} else if(data.questions[question].kind=="M" && data.questions[question].given=="" && val=="A") {
			data.questions[question].given = "A";
		} else if(data.questions[question].kind=="M" && data.questions[question].given=="" && val=="B") {
			data.questions[question].given = "B";
		} else if(data.questions[question].kind=="M" && data.questions[question].given=="" && val=="C") {
			data.questions[question].given = "C";
		} else if(data.questions[question].kind=="O") {
			tmp = data.questions[question].given + val;
			if(tmp==".") tmp = "0.";
			if(!isNaN(tmp) && tmp.length<=6) data.questions[question].given = tmp;
		}
		display();
	}
}

function enableDisplay() {
	display();
	$("#display").fadeTo(10, 1);
	$("#verder INPUT").removeAttr("disabled");
	answerEnabled = true;
}

function disableDisplay() {
	answerEnabled = false;
	$("#verder INPUT").attr("disabled", "disabled");
	$("#display").fadeTo(10, 0.4);
}

function display() {
	if(data!=null && data.questions!=null && question!=null && question>=0 && data.questions[question]!=null) {
		text  = "Vraag " + (question + 1) + " van " + data.questions.length + "<BR>";
		
		if(data.questions[question].kind=="B") {
			text += "[Ja/Nee]: ";
		} else if(data.questions[question].kind=="M") {
			text += "[A/B/C]: ";
		} else if(data.questions[question].kind=="O") {
			text += "[0-9]: ";
		}
		
		if(data.questions[question].given=="J") {
			text += "Ja";
		} else if(data.questions[question].given=="N") {
			text += "Nee";
		} else {
			val = data.questions[question].given.replace(".", ",");
			text += val;
		}
		text+= "<BR>";
		
		text += "&nbsp;<BR>";
		
		errors = 0;
		for(i=0; i<Math.min(question, data.questions.length); i++) {
			if(data.questions[i].kind=="O") {
				if(parseFloat(data.questions[i].answer)!=parseFloat(data.questions[i].given)) errors++;
			} else {
				if(data.questions[i].answer!=data.questions[i].given) errors++;
			}
		}
		text += "Fouten: " + errors;
		
		$("#display").html(text);
	}
}

function showResults() {
	if(data!=null && data.questions!=null) {
		showChart();
		$("#thumbs").html("<TABLE cellpadding=\"0\" cellspacing=\"0\"></TABLE>");
		for(i=0; i<data.questions.length; i++) {
			if(i%7==0) $("#thumbs TABLE").append("<TR></TR>");
			$("#thumbs TABLE TR:last-child").append("<TD><IMG id=\"thumb" + i + "\" class=\"thumbs\" src=\"" + data.questions[i].thumbUrl + "\" width=\"100\" height=\"75\" alt=\"" + i + "\" title=\"Klik op de vraag voor een gesproken motivatie\"><DIV class=\"label\">Vraag <B>" + (i+1) + "</B></DIV></TD>");
			$("#thumb" + i).click(function() {
				explanation(parseInt($(this).attr("alt")));
			}).css("cursor", "pointer");
			if(data.questions[i].kind=="O") {
				if(parseFloat(data.questions[i].answer)!=parseFloat(data.questions[i].given)) $("#thumb" + i).fadeTo(0,0.5);
			} else {
				if(data.questions[i].answer!=data.questions[i].given) $("#thumb" + i).fadeTo(0,0.5);
			}
		}
		
		$("#tabs").data("disabled.tabs", []);
		$("#tabs").tabs("select", 3);
		$("#tabs").data("disabled.tabs", [2,5]);
	}
}
	

function explanation(index) {
	if(data!=null && data.questions!=null && index!=null && index>=0 && data.questions[index]!=null) {
	
		text  = "Vraag " + (index + 1) + "<BR>";
		text += "&nbsp;<BR>";
		text += "Juist: ";
		if(data.questions[index].answer=="J") {
			text += "Ja";
		} else if(data.questions[index].answer=="N") {
			text += "Nee";
		} else {
			val = data.questions[index].answer.replace(".", ",");
			text += val;
		}
		text += "<BR>";
		text += "Gegeven: ";
		if(data.questions[index].given=="J") {
			text += "Ja";
		} else if(data.questions[index].given=="N") {
			text += "Nee";
		} else {
			val = data.questions[index].given.replace(".", ",");
			text += val;
		}
		
		$("#explanation-display").html(text);
		
	
		
		preloadExplanationImage(data.questions[index]);
	}
	return false;
}

function back() {
	if(msie) {
		$("#tabs").data("disabled.tabs", []);
		$("#tabs").tabs("select", 4);
		$("#tabs").data("disabled.tabs", [2,5]);
	} else {
		$("#tabs").tabs("select",4);
	}
	return false;
}
