function FeelWellModule() {
}

FeelWellModule.prototype.notificationsList = [Notifications.FEELWELL_XML_LOADED, Notifications.FEELWELL_RESULT];
FeelWellModule.prototype.name = "FeelWellModule";

FeelWellModule.prototype.release = function() {
	$("#overlay2").html("");
}

FeelWellModule.prototype.init = function() {

	this.questionId = -1;

	Phire.placeTemplate("#content", "FeelWellIntro.html #feelwell", $.callback(this, this.onInit));
	Phire.sendNotification(Notifications.NAV_STATE, "feelwell");

}

FeelWellModule.prototype.handleNotification = function(notification, data) {

	switch (notification) {
		case Notifications.FEELWELL_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", "FeelWellContent.html #feelwell", $.callback(this, this.onInitTest));

			break;

	}
}
/************************************
 *  Start
 *************************************/

FeelWellModule.prototype.onInit = function() {
	$("#feelwell_back").click($.callback(this, this.onBack));
	$("#feelwell_start").click(function() {
		Phire.getModel("WebProxy").getFeelWellXML();
	});
	UserInfo.inTest = false;
	reloadImages();
}
FeelWellModule.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;
	}

	$("#feelwell_progress" + this.questionId).hide();
	this.questionId--;
	this.nextQuestion();
}
/************************************
 *  Test
 *************************************/

FeelWellModule.prototype.onInitTest = function() {
	$("#feelwell_next").click($.callback(this, this.onNext));
	$("#feelwell_back").click($.callback(this, this.onBack));
	$("#feelwell_div_answer1").mousedown($.callback(this, this.onAnswerClicked, 1));
	$("#feelwell_div_answer2").mousedown($.callback(this, this.onAnswerClicked, 2));
	$("#feelwell_div_answer3").mousedown($.callback(this, this.onAnswerClicked, 3));
	
	UserInfo.inTest = true;
	
	$(".radio").dgStyle();

	this.initProgressBar();

	this.nextQuestion();
	reloadImages();
}

FeelWellModule.prototype.initProgressBar = function() {
	var x = 432.0;
	var y = 421;

	for(var i = 1; i < this.exerciseCount; i++) {

		var symbol = $("<div id='feelwell_progress" + i + "' style='position:absolute; left:" + x + "px; top:" + y + "px; width:38px; height:10px; background-image:url(img/feelwell/progress.jpg);'/> ");
		$("#feelwell").append(symbol);
		$(symbol).hide();
		x += 38;
	};
}

FeelWellModule.prototype.nextQuestion = function() {

	this.currentExercise = this.exercises[this.questionId];
	this.currentAnswers = $("antwort", this.currentExercise);

	$("#feelwell_image").css("background-image", "url(" + $("image", this.currentExercise).text() + ")");
	$("#feelwell_question").text($("frage", this.currentExercise).text());

	$("#feelwell_answer1").text($("text", this.currentAnswers[0]).text());
	$("#feelwell_answer2").text($("text", this.currentAnswers[1]).text());

	$("#feelwell_radio_answer1").dgUncheck(-1);
	$("#feelwell_radio_answer2").dgUncheck(-1);
	$("#feelwell_radio_answer3").dgUncheck(-1);

	var answer3 = $("text", this.currentAnswers[2]).text();
	
	if(answer3 != "") {
		$("#feelwell_answer3").show();
		$("#feelwell_div_answer3").show();
		$("#feelwell_answer3").text(answer3);
	} else {

		$("#feelwell_answer3").hide();
		$("#feelwell_div_answer3").hide();
	}
	
	if(this.answers[this.questionId] != null) {
		$("#feelwell_radio_answer" + this.answers[this.questionId]).dgCheck(-1);
		$("#feelwell_next").fadeTo('fast', 1);
		this.answerSelected = this.answers[this.questionId];

	} else {
		$("#feelwell_next").fadeTo(0, 0.3);
		this.answerSelected = 0;
	}	

}

FeelWellModule.prototype.onAnswerClicked = function(value) {

	this.answers[this.questionId] = this.answerSelected = value;
	$("#feelwell_next").fadeTo('fast', 1);

}

FeelWellModule.prototype.onNext = function() {

	if(this.answerSelected == 0)
		return;

	log(this.questionId, this.exerciseCount);

	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++;
	$("#feelwell_progress" + this.questionId).show();
	this.nextQuestion();

}

FeelWellModule.prototype.onDecision = function(value) {

	if(!value) {
		Phire.placeTemplate("#content", "FeelWellResult.html #feelwell", $.callback(this, this.onInitResult));
	}
}
/************************************
 *  Result
 *************************************/

FeelWellModule.prototype.onInitResult = function() {

	Phire.placeTemplate("#overlay2", "FeelWellDetail.html #feelwell_detail", $.callback(this, this.onResultComplete));

}

FeelWellModule.prototype.onResultComplete = function() {

	Phire.sendNotification(Notifications.SHOW_PRELOADER, false);
	$("#feelwell_next").click($.callback(this, this.onNextResult));
	$("#feelwell_back").click($.callback(this, this.onPrevResult));
	$("#feelwell_infos").click(function() {
		$("#feelwell_detail").show();
	});
	$("#feelwell_back").hide();
	$("#feelwell_detail").hide();

	$("#feelwell_detail_close").click(function() {
		$("#feelwell_detail").hide();
	});
	this.questionId = 0;
	this.initProgressBar();
	this.nextResult();
	reloadImages();

}

FeelWellModule.prototype.onNextResult = function() {

	$("#feelwell_back").show();
	if(this.questionId == this.exerciseCount - 1) {

		Phire.sendNotification(Notifications.SHOW_ALERT_PANEL, {
			text : "Die Auswertung ist abgeschlossen.",
			ok : "Zurück",
			cancel : "Ok",
			callback : function(value) {
				if(!value)
					Phire.sendNotification(Notifications.SHOW_MODULE, {
						module : "MainModule"
					})
			}
		});
		return;
	}
	this.questionId++;
	$("#feelwell_progress" + this.questionId).show();
	this.nextResult();

}

FeelWellModule.prototype.onPrevResult = function() {

	if(this.questionId == 1) {

		$("#feelwell_back").hide();
	}
	$("#feelwell_next").show();
	$("#feelwell_progress" + this.questionId).hide();
	this.questionId--;
	this.nextResult();

}

FeelWellModule.prototype.nextResult = function() {
	var excercise = this.exercises[this.questionId];
	var answerList = $("antwort", excercise);
	var matchingAnswer = answerList[this.answers[this.questionId] - 1];
	var answer = $("text", matchingAnswer).text();
	var comment = $("kommentar", matchingAnswer).text();
	var info = $("allg_text:first", excercise).text();

	$("#feelwell_image").css("background-image", "url(" + $("image", excercise).text() + ")");
	$("#feelwell_answer").text(answer);
	$("#feelwell_comment").text(comment);
	$("#feelwell_details").html(info);
}

FeelWellModule.prototype.onShowDetail = function(job, type) {

};


