var question = -1;
var timer;
var answerEnabled = false;
var preloadInProgress = false;
var sound = null;

function preloadQuestionSound(q) {
	if(soundEnabled && soundManager.canPlayURL(q.sndUrl)) {
		sound = soundManager.createSound({
			id: data.questions[question].sndUrl,
			url: data.questions[question].sndUrl,
			autoLoad: true,
			autoPlay: false,
			onfinish:function() {
				progressbar()
			},
			onload:function() {
				preloadQuestionImage(q)
			}
		});
	} else {
		preloadQuestionImage(q);
	}
}

function preloadExplanationSound(q) {
	$("#explanationImage IMG").fadeTo(0,0);
	$("#tabs").tabs("enable", 4);
	$("#tabs").tabs("select",4);
	
	if(soundEnabled && soundManager.canPlayURL(q.explUrl)) {
		sound= soundManager.createSound({
			id: q.explUrl,
			url: q.explUrl,
			autoLoad: true,
			autoPlay: false,
			onload:function() {
				preloadExplanationImage(q)
			}
		});
	} else {
		preloadExplanationImage(q);
	}
}

function preloadQuestionImage(q) {
	$("#questionImage IMG").attr("src", q.imgUrl).bind("load", function() {
		//alert("Foto geladen");
		imageBars();		
		enableDisplay();
		enableProgressbar();
		sound.play(q.sndUrl);
		
		preloadInProgress = false;
	});
}

function preloadExplanationImage(q) {
	$("#explanationImage IMG").attr("src", q.imgUrl).bind("load", function() {
		$(this).fadeTo(0,1);
		sound.play(q.explUrl);
		preloadInProgress = false;
	});
}

function next() {
	if(!preloadInProgress) {
		if(soundEnabled) {
			if(sound) sound.destruct();
			soundManager.stopAll();
		}
		window.clearTimeout(timer);
		disableDisplay();
		
		question++;
		if(question>=data.questions.length) {
			showResults();
		} else if(question<data.questions.length) {
			preloadInProgress = true;
			preloadQuestionSound(data.questions[question]);
		}
	}
}

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

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

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

function setAnswer(val) {
	if(answerEnabled) {
		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);
	answerEnabled = true;
}

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

function display() {
	text  = "Vraag " + (question + 1) + " van " + data.questions.length + "<BR>";
	//text += "<BR>";
	//text += "";
	
	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() {
	//$("#tabs-1").html("<IMG id=\"spinner\" src=\"images/loading.gif\">");
	/*$("#spinner").css({
		position: "absolute",
		zIndex: 9999,
		left: ($("#tabs").width()-300)/2,
		top: ($("#tabs").height()-300)/2
	});*/
	
	showChart();
	for(i=0; i<data.questions.length; i++) {
		$("#thumbs").append("<SPAN style=\"position: relative\"><A href=\"#\" onclick=\"return explanation(" + i + ")\"><IMG id=\"thumb" + i + "\" class=\"thumbs\" src=\"" + data.questions[i].thumbUrl + "\" width=\"100\" height=\"75\" alt=\"\" title=\"\"></A><DIV class=\"label\" style=\"position: absolute; left:10px; bottom:5px;\">Vraag " + (i+1) + "</DIV></SPAN>");
		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").data("disabled.tabs", [0,1,4]);
	$("#tabs").tabs("select",2);
	$("#tabs").tabs("option", "fx", {opacity: "toggle"});
}
	

function explanation(index) {
	
	
	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);
	

	
	preloadExplanationSound(data.questions[index]);
	return false;
}

function back() {
	$("#tabs").tabs("select",3);
	return false;
}
