﻿$.getSpeurtocht = function() {
$('body').append('<div id="speurtocht"><div id="speurtocht-header"><div id="speurtocht-pet"><img src="/ContentSuite/template/lgk/images/empty.gif" id="speurtocht-pet-img" /></div><div id="speurtocht-logo"></div><div id="speurtocht-sublogo"></div><div id="speurtocht-resize"></div><div id="speurtocht-next" title="Volgende vraag"></div><!-- <div id="speurtocht-loader"></div> --></div><div id="speurtocht-content"></div></div>');

	$('#speurtocht-resize, #speurtocht-logo, #speurtocht-sublogo').click(function() {
		$.toggleSpeurtochtSize();
	});

	$('#speurtocht-sublogo').click(function() {
		if (!$(this).hasClass('active')) {
			var field = document.getElementById('menu_search').getElementsByTagName('input')[0];
			top.scroll(0, 0);
			field.value = '';
			field.focus();
			playAnimations('search');
		}
	});

	$('#speurtocht-next').click(function() {
		$.getSpeurtochtContent('&next=1');
	});

	if ($.cookie('speurtocht') == 'show') {
		$('#speurtocht-content').show();
		$('#speurtocht-resize').toggleClass('active').next();
		$('#speurtocht-sublogo').toggleClass('active').next();
	}

	$.getSpeurtochtContent('');
};

$.getSpeurtochtContent = function(p_strQS) {
	//$('#speurtocht-loader').show();
	$.getJSON('/contentsuite/template/lgk/speurtocht.aspx?t=' + new Date().getTime() + p_strQS, function (data) {
		switch (data[0].Status) {
			case 'start':
				$('#speurtocht-sublogo').hide();
				$('#speurtocht-next').hide();
				$('#speurtocht-content').html('<p>Test en vergroot je kennis over huisdieren door als een echte speurder door de website te zoeken naar de juiste antwoorden.</p>');
				$('#speurtocht-content').append('<p>Je naam: <input id="speurtocht-name" maxlength="50" /></p>');
				$('#speurtocht-name').keyup(function(e) {
					if (e.keyCode == 13) {
						$.getSpeurtochtContent('&name=' + encodeURI($('#speurtocht-name').val()));
					}
				});
				$('#speurtocht-content').append('<div id="speurtocht-start"></div>');
				$('#speurtocht-start').click(function() {
					$.getSpeurtochtContent('&name=' + encodeURI($('#speurtocht-name').val()));
				});
				$('#speurtocht-pet-img').attr("src", '/ContentSuite/template/lgk/images/speurtocht_start.png');
				$('#speurtocht-name').focus();
				break;
			case 'question':
				$('#speurtocht-sublogo').show();
				$('#speurtocht-next').hide();
				$('#speurtocht-content').html('<p>(' + data[1].Number + '/15) ' + data[2].Question + '</p>');
				$('#speurtocht-content').append('<ul><li id="speurtocht-a"><div></div><span>' + data[2].Answer1 + '</span></li><li id="speurtocht-b"><div></div><span>' + data[2].Answer2 + '</span></li><li id="speurtocht-c"><div></div><span>' + data[2].Answer3 + '</span></li></ul>');
				$('#speurtocht-a').click(function() {
					$.getSpeurtochtContent('&answer=a');
				});
				$('#speurtocht-b').click(function() {
					$.getSpeurtochtContent('&answer=b');
				});
				$('#speurtocht-c').click(function() {
					$.getSpeurtochtContent('&answer=c');
				});
				$('#speurtocht-pet-img').attr("src", data[2].Image);
				//$('#speurtocht-pet-img').attr("src", 'http://cms.webbeat.net' + data[2].Image);
				break;
			case 'answer':
				$('#speurtocht-sublogo').show();
				$('#speurtocht-next').show();
				$('#speurtocht-content').html('<p>(' + data[1].Number + '/15) ' + data[2].Explanation + '</p>');
				$('#speurtocht-content').append('<ul><li id="speurtocht-a"><div></div><span>' + data[2].Answer1 + '</span></li><li id="speurtocht-b"><div></div><span>' + data[2].Answer2 + '</span></li><li id="speurtocht-c"><div></div><span>' + data[2].Answer3 + '</span></li></ul>');
				$('#speurtocht-a').addClass(data[2].CorrectAnswer == 'a' ? 'correct' : 'incorrect');
				$('#speurtocht-b').addClass(data[2].CorrectAnswer == 'b' ? 'correct' : 'incorrect');
				$('#speurtocht-c').addClass(data[2].CorrectAnswer == 'c' ? 'correct' : 'incorrect');
				$('#speurtocht-' + data[3].UserAnswer).append('<div class="speurtocht-useranswer"></div>');
				$('#speurtocht-pet-img').attr("src", data[2].Image);
				//$('#speurtocht-pet-img').attr("src", 'http://cms.webbeat.net' + data[2].Image);
				break;
			case 'end':
				$('#speurtocht-sublogo').hide();
				$('#speurtocht-next').hide();
				$('#speurtocht-content').html('<p>Dit was de laatste vraag van je speurtocht. Je hebt <strong>' + data[1].Score + '</strong> punten gehaald!</p>');
				$('#speurtocht-content').append('<p>Bekijk snel of je bij de topscorers van deze maand hoort...</p>');
				$('#speurtocht-content').append('<p><a href="/lgk-speurtopscores" id="speurtocht-topscorers"></a></p>');
				$('#speurtocht-content').append('<p><div id="speurtocht-startnew"></div></p>');
				$('#speurtocht-startnew').click(function() {
					$.getSpeurtochtContent('&reset=1');
				});
				$('#speurtocht-pet-img').attr("src", '/ContentSuite/template/lgk/images/speurtocht_end.png');
				break;
			default:
				$('#speurtocht').hide();
		}
		//$('#speurtocht-loader').hide();
	});
};

$.toggleSpeurtochtSize = function() {
	$('#speurtocht-content').slideToggle('slow');
	$('#speurtocht-resize').toggleClass('active').next();
	$('#speurtocht-sublogo').toggleClass('active').next();

	if ($.cookie('speurtocht') == 'show') {
		$.cookie('speurtocht', '');
	}
	else {
		$.cookie('speurtocht', 'show');
	}
};

$(document).ready(function() {
	$.getSpeurtocht();
});
