
var currentQuestion = 1;
var firstAlternativeSent = false;
var mailSent = false;
arrQuestions = new Array();

$(document).ready(function() {
	leftval = 0;
	var numQuestions = 0;

	$("#questions .question").each(function (){
	    numQuestions++;
	    $("#questionNav").append("<div class='questionNavButton' questionId='"+ numQuestions +"' id='question_"+numQuestions+"'/>");
		$(this).css({"position": "absolute", "top": "0px", "left":leftval+"px"});
		leftval += $(this).parent().width();
	})

	$(".questionNavButton:not(.questionNavButtonActive)").click(function (){
		gotoQuestion($(this).attr('questionId'));
	})

	$(".extraquestion").bind("click", function (){
		if (!firstAlternativeSent) {

			$.post("/flx/dk/start/hvilken_livsform_er_du/?action=saveWantedResult", {wantedResult: $(this).html()},
				  function(data) {
		  			//console.log("debug: " + data);
				  }
			);

			//$("#livsformForm input[name=Foerste_alternativ]").val( $(this).html() );
			//$("#livsformForm").submit()
			firstAlternativeSent = true;
		}
	});

	// CLICK question
	$("#questions ul li").click(function (){
	    $(this).parent().find("li").removeClass("selected");
		$(this).addClass("selected");

		var totalx=0;
		var totaly=0;
		$("#questions ul li.selected").each(function (){
			totalx += parseFloat($(this).attr("xval"));
			totaly += parseFloat($(this).attr("yval"));
		});

		if (currentQuestion<numQuestions) {
			moveNext();
		} else {
			// Test done - results:

			// x points = max 42

			pointerLeft = 342 + (totalx*10)-30;
			if (pointerLeft<25) pointerLeft=25;
			if (pointerLeft>630) pointerLeft=630;

			pointerTop = 180 + (totaly*10) - 25;
			if (pointerTop<25) pointerTop=25;
			if (pointerTop>340) pointerTop=340;



			$(".resultoption").hide();

			if (totalx < 0 && totaly < 0) {
				//egofest
				$("#egofester").show();
				resultName = "Egofester";
			} else if (totalx < 0 && totaly >= 0) {
				// Voksencool
				$("#voksencool").show();
				resultName = "Voksencool";
			} else if (totalx >= 0 && totaly >= 0) {
				// Co-creation
				$("#cocreation").show();
				resultName = "Co-creation";

			} else {
				// Guruisme
				$("#guruisme").show();
				resultName = "Guruisme";
			}

			$.post("/flx/dk/start/hvilken_livsform_er_du/?action=saveResult", {scoreX: totalx, scoreY:totaly, result: resultName},
				  function(data) {
		  			//console.log("debug: " + data);
				  }
			);

			/*
			if (!mailSent) {
				$("#livsformForm input[name=xval]").val(totalx + '\n');
				$("#livsformForm input[name=yval]").val(totaly + '\n');
				$("#livsformForm input[name=Resultat]").val(resultName + '\n');
				$("#livsformForm").submit()

				mailSent = true;
			}*/

			$("#pointer").css("left", pointerLeft).css("bottom", pointerTop);

			$("#step1").slideUp(500, function (){

				$("#resultsContainer").slideDown(1000, function (){
					$("#pointer").effect("pulsate", 300);
				});

			});

		}
		$("#values").html("X: " + totalx + " Y: " + totaly);
	});


});

function checkForm() {
	$oF = $("#initialQuestions");
	strError = '';

	if ($("[name=Koen]", $oF).val()=='') {
		strError += '<li>Køn skal vælges';
	}
	if ($("[name=Alder]", $oF).val()=='') {
		strError += '<li>Alder skal vælges';
	}
	if ($("[name=Uddannelse]", $oF).val()=='') {
		strError += '<li>Uddannelse skal vælges';
	}

	if ($("[name=navn]", $oF).val()=='') {
		strError += '<li>Navn skal udfyldes';
	}
	if ($("[name=email]", $oF).val()=='') {
		strError += '<li>Email skal udfyldes';
	}

	if (strError!='') {
		$("#formErrors").html("Du mangler at udfylde nogle felter i formularen: <ul>" + strError + "</ul>").slideDown();
		return false;
	}
	return true;
}

function gotoTest() {

	if (checkForm()) {
		$dataForm = $("#livsformForm");
		$form = $("#initialQuestions");
		$("input[name=Nyhedsbrev-navn]", $dataForm).val($('[name=navn]', $form).val() + '\n');
		$("input[name=Nyhedsbrev-email]", $dataForm).val($('[name=email]', $form).val() + '\n');
		$("input[name=Koen]", $dataForm).val($('[name=Koen]', $form).val() + '\n');
		$("input[name=Alder]", $dataForm).val( $('[name=Alder]', $form).val() + '\n');
		$("input[name=Uddannelse]", $dataForm).val( $('[name=Uddannelse]', $form).val() + '\n');

		$.post("/flx/dk/start/hvilken_livsform_er_du/?action=saveBaseInfo", $form.serialize(),
			  function(data) {
		  		//console.log("debug: " + data);
			  }
		);


		$("#step1").hide().css("visibility", "visible");
		$("#step0").slideUp(500, function (){
			$("#step1").slideDown("slow");

		});
	}
}

function showQuestions() {
	$("#resultsContainer").slideUp(500, function (){
		$("#step1").slideDown(1000, function (){
			gotoQuestion(1);
		});
	});
}

function movePrev() {
	currentQuestion--;
	gotoQuestion(currentQuestion);

}
function moveNext() {
	currentQuestion++;
	gotoQuestion(currentQuestion);

}
function gotoQuestion(qn) {
	currentQuestion = qn;
	$("#questionNav .questionNavButton").removeClass("questionNavButtonActive");
	$("#questionNav .questionNavButton").slice(0, (currentQuestion-1)).addClass("questionNavButtonActive");
	$("#questions").animate({"margin-left": "-"+ (640*(qn-1)) +"px"}, 200);
}

