var html = '';
var questionNumber = 0;
var questionCount = 2;
var answers = Array();
var ie6 = false;
if ( ($.browser.msie && $.browser.version.substring(0,1)=='6') ){
 ie6=true;
}


$(document).ready(function(){
	//Show the buttons (this way only javascript enabled browsers see the button)
	$(".bgTestButton").show();

	//Check querystring to see if we should open the test right away
	test = getQuery();
	if ( test == 'mbitest' ){
		//Load the test
		loadTest("MBI", function(){
			//Give the bg the correct height
			$("#slider_bg").height($(document).height());
			//center the popup
			windowHeight = $(window).height();
			scrollTop=$(window).scrollTop();
			if (ie6){
				$("#slider").css('position', 'absolute');
				$("#slider").css('top', scrollTop);
			}
			$("#slider_bg, #slider").show();
			
			questionNumber = 0;
			initTest();
		});
	}
	else if ( test == 'verkooptest' ){
		//Load the test
		//Load the test
		loadTest("verkoopGereed", function(){
			//Give the bg the correct height
			$("#slider_bg").height($(document).height());
			//center the popup
			windowHeight = $(window).height();
			scrollTop=$(window).scrollTop();
			if (ie6){
				$("#slider").css('position', 'absolute');
				$("#slider").css('top', scrollTop);
			}
			$("#slider_bg, #slider").show();
			
			questionNumber = 0;
			initTest();
		});
	}
	
	$("a.btnTestMBI").click(function(){
		//Load the test
		loadTest("MBI", function(){
			//Give the bg the correct height
			$("#slider_bg").height($(document).height());
			//center the popup
			windowHeight = $(window).height();
			scrollTop=$(window).scrollTop();
			if (ie6){
				$("#slider").css('position', 'absolute');
				$("#slider").css('top', scrollTop);
			}
			$("#slider_bg, #slider").show();
			
			questionNumber = 0;
			initTest();
		});
		return false;
	});

	$("a.btnTestVerkoopGereed").click(function(){
		//Load the test
		loadTest("verkoopGereed", function(){
			//Give the bg the correct height
			$("#slider_bg").height($(document).height());
			//center the popup
			windowHeight = $(window).height();
			scrollTop=$(window).scrollTop();
			if (ie6){
				$("#slider").css('position', 'absolute');
				$("#slider").css('top', scrollTop);
			}
			$("#slider_bg, #slider").show();
			
			questionNumber = 0;
			initTest();
		});
	});
});

/**
 * A function that reads the querystring
 * @return
 */
function getQuery(){
	hu = window.location.search.substring(1);
	gy = hu.split("&");
	for ( i=0; i<gy.length; i++) {
		ft = gy[i].split("=");
		if ( (ft[0].toLowerCase() == 'mbitest') || (ft[0].toLowerCase() == 'verkooptest')) {
			return ft[0];
		}
	}
}

//A function that uses AFC Ajax to load a test
function loadTest(name, callbackFunc){
	$.post(
		'/php/AjaxController.php', 
		{
			action: 'loadTestForm',
			name: name
		},
		function(data){
			//Show the result
			$("#slider").html(data);
			callbackFunc();
			Cufon.replace('#welcome .btnOrange .text', { fontFamily: 'aller' });
//			Cufon.replace('.question h2', { fontFamily: 'aller' });

		},
		"html"
	);
}

function closeTest(){
	$("#slider_bg, #slider").hide();
}

function initTest(){
	questionCount = $("#questionholder .questionContent").length;

	initBoxes();

	$("#slider #test").css("left",0);
	$("#slider #test").css("top",0);
	if ( questionNumber>0 ){
		showQuestion(questionNumber, 0);
		$("#slider #test").css("top",-537);
	}

	initContactForm();

	if ( $("a.closePopup").length ){
		$("a.closePopup").click(function(){
			//Close the popup
			closeTest();
			return false;
		});
	}
}

function initContactForm(){
	$("#test .contactForm form").submit(function(){
		//Do some AFC Ajax magic to send the form
		//Put the answers in a nice array
		var answers = Array(questionCount); 
		for ( var i=1; i<=questionCount; i++ ){
			answers[i-1] = $("#test #q"+i+" .answers input[name='a"+i+"']:checked").val();
		}
		$.post(
			'/php/AjaxController.php', 
			{
				action: 'submitTestForm',
				answers: answers,
				testName: $("#slider form").attr("name"),
				testContact_name: $("#testContact_name").val(),
				testContact_email: $("#testContact_email").val(),
				testContact_phone: $("#testContact_phone").val(),
				testContact_sendResult: $("#testContact_sendResult:checked").length,
				testContact_submit: 1
			},
			function(data){
				$("#result .contactForm .content").fadeOut(200, function(){
					$("#result .contactForm .content").html(data);
					$("#result .contactForm .content").fadeIn(200);
					initContactForm();
				});
			},
			"html"
		);
		//Show the result
		
		return false;
	});
}

function initBoxes(){
	unbindEvents();

	if ( $(".testBox#welcome").length ){
		$(".testBox#welcome a.btnOrange").click(function(){
			questionNumber++;
			showQuestion(questionNumber, 0);
			$("#slider #test").animate({left: 0, marginTop: -537},400);
			return false;
		});
	}

	if ( $(".testBox .close").length ){
		$(".testBox .close").click(function(){
			//Close the popup
			closeTest();
			return false;
		});
	}
	
	if ( $(".testBox .btnPrev").length ){
		$(".testBox .btnPrev").click(function(){
			if ( questionNumber > 1 ){
				//Load the next question
				questionNumber--;
				showQuestion(questionNumber, 400);
				return false;
			}
		});
	}
	
	if ( $(".testBox .btnNext").length ){
		$(".testBox .btnNext").click(function(){
			if ( questionNumber < questionCount && $("#test input[name='a"+questionNumber+"']:checked").length>0 ){
				//Load the next question
				questionNumber++;
				showQuestion(questionNumber, 400);
			}else if ( questionNumber == questionCount && $("#test input[name='a"+questionNumber+"']:checked").length>0 ){
				//Load the results
				//Do some AFC Ajax magic to calculate the result
				//Put the answers in a nice array
				var answers = Array(questionCount); 
				for ( var i=1; i<=questionCount; i++ ){
					answers[i-1] = $("#test #q"+i+" .answers input[name='a"+i+"']:checked").val();
				}
				$.post(
					'/php/AjaxController.php', 
					{
						action: 'getTestResult',
						name: $("#slider form").attr("name"),
						answers: answers
					},
					function(data){
						$("#result .question h2").html(data['title']);
//						Cufon.replace('#result .question h2', { fontFamily: 'aller' });
						if ( $("#result .question h2").height() > 60 ){
							$("#result .question").css("paddingTop", "7px");
						}else{
							$("#result .question").css("paddingTop", "27px");
						}
							
						$("#result .body").html(data['body']);
					},
					"json"
				);
				//Show the result
				$("#slider #test").animate({left: 0, marginTop: -1074},400);
			}
			return false;
		});
	}

	$("#slider .testBox #questionholder .answers li").mouseover(
		function(){
			if ( !$(this).hasClass('active') ) $(this).addClass('hover');
		}
	);
	$("#slider .testBox #questionholder .answers li").mouseout(
		function(){
			$(this).removeClass('hover');
		}
	);

	$(".testBox .answers li").click(function(){
		$("input", this).attr("checked", "checked");
		$(this).parent("ul").children("li").removeClass('active');
		$(this).removeClass('hover').addClass('active');
		$("#test .navBar .btnNext").addClass("active");
	});
	
	$("#test .answers li input:checked").parent().parent("li").addClass('active');
}

function unbindEvents(){
	$(".testBox#welcome a.btnOrange").unbind();
	$(".testBox .close").unbind();
	$(".testBox .btnPrev").unbind();
	$(".testBox .btnNext").unbind();
	$(".testBox .answers li").unbind();
}

function showQuestion(nr, time){
	if ( nr==0 ) return;
	
	var newLeft = (nr-1) * -865;

	if ( nr == questionCount ){
		//Change nextButton
		$("#test .navBar .btnNext .text").html("Uitslag");
//		Cufon.replace('#test .navBar .btnNext .text', { fontFamily: 'aller' });
	}else{
		$("#test .navBar .btnNext .text").html("Volgende vraag");
//		Cufon.replace('#test .navBar .btnNext .text', { fontFamily: 'aller' });
	}

	$("#questionSlider #questionholder").animate({left: newLeft},time);

	for (i=1; i<=$("#test .navBar .numbers .number").length; i++){
		var el = $("#test .navBar .numbers .number").eq(i-1);
		if ( $("#q"+i+" .answers input[name='a"+i+"']:checked").length>0 ){
			el.removeClass('todo').removeClass('active').addClass('done');
		}else{
			el.removeClass('done').removeClass('active').addClass('todo');
		}
	}
	$("#test .navBar .numbers .number").eq(nr-1).removeClass('todo').removeClass('done').addClass('active');
	
	//Navigation buttons
	if ( nr==1 ){
		//Deactivate the previous button
		$("#test .navBar .btnPrev").removeClass("active");
	}else{
		$("#test .navBar .btnPrev").addClass("active");
	}
	
	if ( $("#q"+nr+" .answers input[name='a"+nr+"']:checked").length>0 ){
		$("#test .navBar .btnNext").addClass("active");
	}else{
		$("#test .navBar .btnNext").removeClass("active");
	}		
}


