var xmlUrl = 'questions.xml';
var url;

function SetQuizQuestions(filename) {
  xmlUrl = filename;
  loadXML();
}

/* Je laadde telkens opnieuw de xml, bij elke nieuwe vraag.
*  Dat is niet nodig, dus alleen de eerste keer (zie onderaan dit bestand)
*/
function loadXML() {
  $.ajax({
    type: "GET",
    url: xmlUrl,
    dataType: "xml",
    success: function (data) {
      xml = data;
    }
  });
}

$(document).ready(function() {
	var number = 1;
	var total = 0;
	var result = 0;
	var start = 'Start de quiz';
	var check = 'Controleer antwoord';
	var next = 'Volgende vraag';
	var reload = 'Opnieuw spelen';

	function loadQuestion(number) {										
		if (total == 0) {
			questions = $(xml).find('questions');
			// Je had de "$('span.quizTotal').html(total);" IN de loop staan, 
			// waardoor elke keer (10 keer in dit geval) de html in span.quiztotal 
			// gewijzigd wordt, wat niet nodig is want dat hoeft natuurlijk alleen 
			// maar op het einde!
			$(questions).find('question').each(function(){
				total = total + 1;
			});
			
			$('span.quizTotal').html(total);
			// Cufon hier, nadat het cijfer is gezet
			Cufon.replace('span#result', { fontFamily: 'RotisSemiSans' });
			Cufon.replace('span.quizTotal', { fontFamily: 'RotisSemiSans' });
		}
						
		//reset
		$('div#quiz').removeClass();
		$('input#quizButton').removeClass();	
		$('ul#quizAnswers li').remove();
		$('input#quizButton').val(check);
		$('p#quizExplanationRight').hide();
		$('p#quizExplanationWrong').hide();
		$('p#response').show();
		$('p#responseRight').hide();
		$('p#responseWrong').hide();
				
		//set content				
		$('span#quizCurrent').html(number);

		// Cufon hier, nadat het cijfer is gezet
		Cufon.replace('span#quizCurrent', { fontFamily: 'RotisSemiSans' });
		question = $(xml).find('question[id='+number+']');
		$('h2#quizQuestion').html($(question).find('content').text());
		$('p#quizExplanationRight').html($(question).find('right').text());
		$('p#quizExplanationWrong').html($(question).find('wrong').text());
		$(question).find('answer').each(function(){
			value = '';					
			if($(this).attr('value')=='right') {
				value = 'right';
			}
			var answer = '<label><input value="'+value+'" name="answer" type="radio" />' + $(this).text() + '</label>';
			$('<li class="'+value+'"></li>').html(answer).appendTo('ul#quizAnswers');				
		});
	}		
	
	$('input#quizStoppen').click(function () {
		$.fancybox.close();
	});

  $('input#quizButton').click(function () {
  		
		questionState = $(this).val();	

		if (questionState == start) {
			$('div#quiz div.intro').hide();
			$('div#quiz div.content').show();
			loadQuestion(number);
		}
		if (questionState == check) {
			$('input#quizButton').addClass('arrow');	
			$('p#response').hide();
			if ($("input[type='radio']:checked").val() == 'right') {			
				$('div#quiz').addClass('right');
				$('p#quizExplanationRight').show();
				$('p#responseRight').show();
				result = result + 1;
			} else {
				$('div#quiz').addClass('wrong');
				$("input[type='radio']:checked").parent().parent().addClass('wrong');
				$('p#quizExplanationWrong').show();
				$('p#responseWrong').show();
			}			
			$('p#quizExplanation').show();
			
			$(this).val(next);
			
			if (number == (total)) {
				$('input#quizButton').removeClass();
				$('input#quizButton').val('Bekijk uw score');
			}
				
		}
		if ((questionState == next) | (questionState == 'Bekijk uw score')) {	
			if (number < total) {
				number = number + 1;
				loadQuestion(number);
			} else {	
				$('span#result').html(result);
				$('div#quiz div.content').hide();
				$('div#quiz div.results').show();
				$('input#quizStoppen').show();
				Cufon.replace('div.results h3', { fontFamily: 'RotisSemiSans' });
				$(this).val(reload);
			}
		}
		if (questionState == reload) {
			$('div#quiz div.content').show();
			$('div#quiz div.results').hide();
			$('input#quizStoppen').hide();
			number = 1;			
			result = 0;
			loadQuestion(number);
		}
		
	});
	
	loadXML();
});
