function JobTestModule() {
}

JobTestModule.prototype.notificationsList = [Notifications.JOBTEST_XML_LOADED, Notifications.JOBTEST_RESULT];
JobTestModule.prototype.name = "JobTestModule";

JobTestModule.prototype.release = function() {
	$("#overlay2").html("");
}

JobTestModule.prototype.init = function() {

	this.questionId = -1;

	Phire.placeTemplate("#content", "JobTestIntro.html #jobtest", $.callback(this, this.onInit));
	Phire.sendNotification(Notifications.NAV_STATE, "jobtest");

}

JobTestModule.prototype.handleNotification = function(notification, data) {

	switch (notification) {
		case Notifications.JOBTEST_XML_LOADED:

			this.rawXML = data;
			this.xml = $(data);
			this.questionId = 0;
			this.exercises = $("aufgabe", this.xml);
			this.exerciseCount = this.exercises.length;
			this.answers = [];
			Phire.placeTemplate("#content", "JobTestContent.html #jobtest", $.callback(this, this.onInitTest));
			break;

		case Notifications.JOBTEST_RESULT:
			UserInfo.jobtestType = this.sek;
			
			this.resultXML = $(data);
			this.resultJobs = $("job", this.resultXML);

			Phire.placeTemplate("#content", "JobTestResult.html #jobtest", $.callback(this, this.onInitResult));
			Phire.sendNotification(Notifications.NAV_STATE, "jobtest");
			break;
	}
}
/************************************
 *  Start
 *************************************/

JobTestModule.prototype.onInit = function() {
	$("#jobtest_hauptschule").click($.callback(this, this.onHauptschule));
	$("#jobtest_gymnasium").click($.callback(this, this.onGymnasium));
	$("#jobtest_back").click($.callback(this, this.onBack));
	UserInfo.inTest = false;
	reloadImages();
}
JobTestModule.prototype.onBack = function() {

	if(this.questionId == -1) {
		Phire.sendNotification(Notifications.SHOW_MODULE, {
			module : "MainModule"
		});
		return;
	}
	if(this.questionId == 0) {
		Phire.sendNotification(Notifications.EXIT_TEST, "MainModule");
		return;
	}

	$("#jobtest_progress" + this.questionId).hide();
	this.questionId--;
	this.nextQuestion();
}

JobTestModule.prototype.onHauptschule = function() {

	this.sek = "sek1";
	Phire.getModel("WebProxy").getJobTestXML("sek1");

}
JobTestModule.prototype.onGymnasium = function() {

	this.sek = "sek2";
	Phire.getModel("WebProxy").getJobTestXML("sek2");

}
/************************************
 *  Test
 *************************************/

JobTestModule.prototype.onInitTest = function() {
	$("#jobtest_next").click($.callback(this, this.onNext));
	$("#jobtest_back").click($.callback(this, this.onBack));
	$("#jobtest_div_answer1").mousedown($.callback(this, this.onAnswerClicked, 1));
	$("#jobtest_div_answer2").mousedown($.callback(this, this.onAnswerClicked, 2));
	$("#jobtest_div_answer3").mousedown($.callback(this, this.onAnswerClicked, 3));
	$("#jobtest_div_answer4").mousedown($.callback(this, this.onAnswerClicked, 4));

	var x = 186.0;
	var y = 421;

	for(var i = 1; i < this.exerciseCount; i++) {

		var symbol = $("<div id='jobtest_progress2_" + i + "' style='position:absolute; left:" + x + "px; top:" + y + "px; width:16px; height:10px; background-image:url(img/jobtest/progress2.jpg);'/> ");
		$("#jobtest_content").append(symbol);
		x += 15;
	}
	x = 186.0
	for( i = 1; i < this.exerciseCount; i++) {
		symbol = $("<div id='jobtest_progress" + i + "' style='position:absolute; left:" + x + "px; top:" + y + "px; width:16px; height:10px; background-image:url(img/jobtest/progress.jpg);'/> ");
		$("#jobtest_content").append(symbol);
		$(symbol).hide();
		x += 15;
	};
	$(".radio").dgStyle();
	this.nextQuestion();
	UserInfo.inTest = true;
	reloadImages();

}

JobTestModule.prototype.nextQuestion = function() {

	this.currentExercise = this.exercises[this.questionId];
	this.currentAnswers = $("antwort", this.currentExercise);

	$("#jobtest_image").css("background-image", "url(" + $("image", this.currentExercise).text() + ")");
	$("#jobtest_question").text($("frage", this.currentExercise).text());

	$("#jobtest_answer1").text($("text", this.currentAnswers[0]).text());
	$("#jobtest_answer2").text($("text", this.currentAnswers[1]).text());
	$("#jobtest_answer3").text($("text", this.currentAnswers[2]).text());
	$("#jobtest_answer4").text($("text", this.currentAnswers[3]).text());

	$("#jobtest_radio_answer1").dgUncheck(-1);
	$("#jobtest_radio_answer2").dgUncheck(-1);
	$("#jobtest_radio_answer3").dgUncheck(-1);
	$("#jobtest_radio_answer4").dgUncheck(-1);

	if(this.answers[this.questionId] != null) {
		$("#jobtest_radio_answer" + this.answers[this.questionId]).dgCheck(-1);
		$("#jobtest_next").fadeTo('fast', 1);
		this.answerSelected = this.answers[this.questionId];

	} else {
		$("#jobtest_next").fadeTo(0, 0.3);
		this.answerSelected = 0;
	}

	if(this.questionId >= 20) {
		$("#jobtest_headline").text("Was ich kann. Selbsteinschätzung meiner Fähigkeiten.");
	} else {
		$("#jobtest_headline").text("Was ich will. Meine beruflichen Interessen.");
	}

}

JobTestModule.prototype.onAnswerClicked = function(value) {

	this.answers[this.questionId] = this.answerSelected = value;
	$("#jobtest_next").fadeTo('fast', 1);

}

JobTestModule.prototype.onNext = function() {

	if(this.answerSelected == 0)
		return;

	if(this.questionId == this.exerciseCount - 1) {

		Phire.sendNotification(Notifications.SHOW_ALERT_PANEL, {
			text : "Sie haben alle Fragen beantwortet.",
			ok : "Zurück",
			cancel : "Auswertung",
			callback : $.callback(this, this.onDecision)
		});
		return;
	}

	this.questionId++;
	$("#jobtest_progress" + this.questionId).show();
	this.nextQuestion();

}

JobTestModule.prototype.onDecision = function(value) {

	if(!value) {
		Phire.getModel("WebProxy").sendJobTestResult(this.answers, this.sek);
	}
}
/************************************
 *  Result
 *************************************/

JobTestModule.prototype.onInitResult = function() {

	Phire.placeTemplate("#overlay2", "JobTestJobDetail.html #jobtest_job_detail", $.callback(this, this.onResultComplete));
}

JobTestModule.prototype.onResultComplete = function() {

	Phire.sendNotification(Notifications.SHOW_PRELOADER, false);
	UserInfo.inTest = false;
	for(var i = 0; i < this.resultJobs.length; i++) {
		var row = $("#jobtest_result_row_template").clone();
		var job = this.resultJobs[i];
		$(row).attr("id", "detail_row");
		$(row).mouseover(function() {
			$(this).css("background-color", "#ddd");
		})
		$(row).mouseout(function() {
			$(this).css("background-color", "transparent");
		})

		$("#jobtest_job", row).text($("beruf", job).text());
		$("#jobtest_job", row).click($.callback(this, this.onShowJobDetail, job));

		$("#jobtest_button_job", row).click($.callback(this, this.onShowJobDetail, job, "job"));
		$("#jobtest_button_fitness", row).click($.callback(this, this.onShowJobDetail, job, "fitness"));
		$("#jobtest_button_money", row).click($.callback(this, this.onShowJobDetail, job, "money"));

		if($("verguetung1", job).text() == "") {
			$("#jobtest_button_money", row).attr("src", "img/jobtest/icon_spacer.gif");
			$("#jobtest_button_money", row).attr("title", "");
			$("#jobtest_button_money", row).attr("cursor", "none");
		}

		$("#jobtest_joblist").append(row);
	};
	$("#jobtest_result_row_template").hide();
	$("#jobtest_head_print").hide();

	$("#jobtest_job_detail").hide();
	$("#jobtest_detail_close").click(function() {
		$("#jobtest_job_detail").hide()
	});

	$("#jobtest_print").click(function() {
		$("div[id*='detail_row']").css("border-bottom-width", "0px");
		$("#jobtest_head_print").show();
		$("#jobtest_joblist").jqprint({
			operaSupport : true,
			printContainer : false
		});
		$("#jobtest_head_print").hide();
		$("div[id*='detail_row']").css("border-bottom-width", "1px");
	});

	$("#jobtest_jobkompass").click(function() {
		Phire.sendNotification(Notifications.SHOW_MODULE, {
			module : "KompassModule"
		});
	});

	$("#jobtest_detail_print").click(function() {
		$("#jobtest_detail_job_headline_print").show();

		$("#jobtest_details").jqprint({
			operaSupport : true,
			printContainer : false
		});

		$("#jobtest_detail_job_headline_print").hide();

	});
	reloadImages();
}

JobTestModule.prototype.onShowJobDetail = function(job, type) {

	$("#jobtest_job_detail").show();
	$("#jobtest_detail_job_headline").text($("beruf", job).text());
	$("#jobtest_detail_job_headline_print").text($("beruf", job).text());
	$("#jobtest_detail_job_headline_print").hide();

	$("#jobtest_details").scrollTop(0);

	var txt = $("jobinfo1", job).text() + "<br>" + $("jobinfo2", job).text() + "<br>" + $("jobinfo3", job).text();
	txt = txt.split("\n").join("<br>").split(".").join(". ").split(".  ").join(". ");
	$("#jobtest_detail_job_text").html(txt);
	txt = $("jobfitness1", job).text() + "<br>" + $("jobfitness2", job).text();
	txt = txt.split("\n").join("<br>").split(".").join(". ").split(".  ").join(". ");
	$("#jobtest_detail_fitness_text").html(txt);

	if($("verguetung1", job).text() == "") {
		$("#jobtest_detail_money").hide();
	} else {
		$("#jobtest_detail_money").show();
		var txt = "Ausbildungsjahr 1: " + $("verguetung1", job).text();
		if($("verguetung2", job).text() != "")
			txt += "<br>" + "Ausbildungsjahr 2: " + $("verguetung2", job).text();
		if($("verguetung3", job).text() != "")
			txt += "<br>" + "Ausbildungsjahr 3: " + $("verguetung3", job).text();

		$("#jobtest_detail_money_text").html(txt.split(".").join(". ").split(".  ").join(". "));
	}
	if(type != null) {
		$("#jobtest_details").scrollTop(0);
		$("#jobtest_details").scrollTop(Math.abs(parseInt($("#jobtest_detail_" + type).position().top)));
	}

};

