function VideoModule() {
}

VideoModule.prototype.notificationsList = [Notifications.VIDEO_XML_LOADED];
VideoModule.prototype.name = "VideoModule";

VideoModule.prototype.release = function() {

}

VideoModule.prototype.insertVideo = function(name) {

	var src = "<div id='video_container'>" + "<video id='the_video_player' width='350' height='197' controls preload poster='#poster#.jpg'>" + "<source src='#name#.mp4' type='video/mp4; codecs='avc1.42E01E, mp4a.40.2'' />" + "<source src='#name#.ogv' type='video/ogg; codecs='theora, vorbis'' />" +
	/*"<object id='flash_fallback_1' class='vjs-flash-fallback' width='350' height='197' type='application/x-shockwave-flash' data='http://releases.flowplayer.org/swf/flowplayer-3.2.7.swf'>" +
	 "<param name='movie' value='http://releases.flowplayer.org/swf/flowplayer-3.2.7.swf' />" +
	 "<param name='allowfullscreen' value='true' />" + "<param name='wmode' value='Opaque' />" +
	 "<param name='flashvars' value='config={\"playlist\":[\"#poster#.jpg\", {\"url\": \"#name#.flv\",\"autoPlay\":false,\"autoBuffering\":true}]}' />" +
	 "<img src='#poster#.jpg' width='350' height='197' />" +
	 "</object>" + */
	"<div id='swfVideo'></div>" + "<script type='text/javascript'>jwplayer('swfVideo').setup({flashplayer:'jwplayer/player.swf',file:'#name#.flv',image:'#poster#.jpg',width:350,height:197,color:'#ffffff'});</script>" + "</video>" + "</div>"
	name = $.trim(name);
	var url = window.location.href.replace("#", "");
	src = src.split("#name#").join(url + name.replace(".flv", "")).split("#poster#").join(url + this.poster);

	var player = document.getElementById("the_video_player");
	if(player != null) {

		if(BrowserDetect.browser == "Chrome") {
			player.src = url + name.replace(".flv", ".ogv");
			player.poster = url + this.poster + ".jpg";
			player.load();
			return;
		}

	}

	$("#video_container").remove();

	$("#video_box").delay(300).html(src);

}

VideoModule.prototype.handleNotification = function(notification, data) {

	switch (notification) {
		case Notifications.VIDEO_XML_LOADED:
			this.rawXML = data;
			this.xml = $(data);
			this.questionId = 0;
			this.exercise = $("aufgabe", this.xml)[0];
			this.exerciseCount = 4;
			this.answers = [];
			this.answerSelected = 0;
			switch (this.videoModule) {
				case 0:
				case 1:
					this.poster = "_videos/poster1";
					break;
				case 2:
				case 3:
					this.poster = "_videos/poster2";
					break;
			}

			Phire.placeTemplate("#content", "VideoContent.html #video", $.callback(this, this.onInitTest));

			break;

	}
}

VideoModule.prototype.init = function() {

	Phire.placeTemplate("#content", "VideoIntro.html #video", $.callback(this, this.onInit));
}
/************************************
 *  Start
 *************************************/

VideoModule.prototype.onInit = function() {

	$("#video_back").click(function() {
		Phire.sendNotification(Notifications.SHOW_MODULE, {
			module : "FitModule"
		});
	});
	$("#video_start").click($.callback(this, this.loadMenu));
	UserInfo.inTest = false;
	reloadImages();
}
VideoModule.prototype.loadMenu = function() {
	Phire.placeTemplate("#content", "VideoMenu.html #video", $.callback(this, this.onInitMenu));
}
/************************************
 *  Menu
 *************************************/

VideoModule.prototype.onInitMenu = function() {
	$("#video_menu0").click($.callback(this, function() {
		this.videoModule = 0;
		Phire.getModel("WebProxy").getVideoXML(0);
	}));
	$("#video_menu1").click($.callback(this, function() {
		this.videoModule = 1;
		Phire.getModel("WebProxy").getVideoXML(1);
	}));
	$("#video_menu2").click($.callback(this, function() {
		this.videoModule = 2;
		Phire.getModel("WebProxy").getVideoXML(2);
	}));
	$("#video_menu3").click($.callback(this, function() {
		this.videoModule = 3;
		Phire.getModel("WebProxy").getVideoXML(3);
	}));
	$("#video_back").click(function() {
		Phire.sendNotification(Notifications.EXIT_TEST, "MainModule");
	});
	reloadImages();
}
/************************************
 *  Test
 *************************************/
VideoModule.prototype.onInitTest = function() {

	$("#video_next").click($.callback(this, this.onNext));
	$("#video_back").click($.callback(this, this.onBack));
	$("#video_div_answer1").mousedown($.callback(this, this.onAnswerClicked, 1));
	$("#video_div_answer2").mousedown($.callback(this, this.onAnswerClicked, 2));
	$("#video_div_answer3").mousedown($.callback(this, this.onAnswerClicked, 3));

	this.showIntro();
	this.initProgressBar(238, 163, "progress");
	$(".radio").dgStyle();
	UserInfo.inTest = true;
	reloadImages();
}

VideoModule.prototype.showIntro = function() {
	$("#video_question").html($("intro_text", this.exercise).text());
	$("#video_answers").hide();
	this.insertVideo($("video_url", this.exercise).text());
	$("#video_next").fadeTo('fast', 1);

	this.answerSelected = 9;
}

VideoModule.prototype.initProgressBar = function(x, dx, image) {

	var y = 420;

	for(var i = 1; i < 4; i++) {

		var symbol = $("<div id='video_progress" + i + "' style='position:absolute; left:" + x + "px; top:" + y + "px; width:" + dx + "px; height:10px; background-image:url(img/video/" + image + ".jpg);'/> ");
		$("#video_content").append(symbol);
		$(symbol).hide();
		x += dx;
	};
}

VideoModule.prototype.nextQuestion = function() {

	var id = this.questionId;

	$("#video_answers").show();
	$("#video_question").html($("frage_" + id, this.exercise).text());

	$("#video_answer1").text($("frage_" + id + "_antwort_1", this.exercise).text());
	$("#video_answer2").text($("frage_" + id + "_antwort_2", this.exercise).text());
	$("#video_answer3").text($("frage_" + id + "_antwort_3", this.exercise).text());

	$("#video_radio_answer1").dgUncheck(-1);
	$("#video_radio_answer2").dgUncheck(-1);
	$("#video_radio_answer3").dgUncheck(-1);

	if(this.answers[this.questionId] != null) {
		$("#video_radio_answer" + this.answers[this.questionId]).dgCheck(-1);
		$("#video_next").fadeTo('fast', 1);
		this.answerSelected = this.answers[this.questionId];

	} else {
		$("#video_next").fadeTo(0, 0.3);
		this.answerSelected = 0;
	}

	this.insertVideo($("video_url", this.exercise).text());

}

VideoModule.prototype.onBack = function() {

	if(this.questionId == 0) {
		this.loadMenu();
		return;
	}

	$("#video_progress" + this.questionId).hide();
	this.questionId--; this.questionId == 0 ? this.showIntro() : this.nextQuestion();
}

VideoModule.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++;
	$("#video_progress" + this.questionId).show();
	this.nextQuestion();

}

VideoModule.prototype.onAnswerClicked = function(value) {

	this.answers[this.questionId] = this.answerSelected = value;
	$("#video_next").fadeTo('fast', 1);

}

VideoModule.prototype.onDecision = function(value) {

	if(!value) {
		Phire.placeTemplate("#content", "VideoResult.html #video", $.callback(this, this.onInitResult));
	}
}
/************************************
 *  Result
 *************************************/
VideoModule.prototype.onInitResult = function(value) {
	$("#video_next").click($.callback(this, this.onNextResult));
	$("#video_back").click($.callback(this, this.onPrevResult));
	$("video_back").hide();
	this.questionId = 1;
	this.initProgressBar(237, 244, "progress_result");
	this.nextResult();
	reloadImages();
}

VideoModule.prototype.nextResult = function() {
	var id = this.questionId;
	var myAnswer = this.answers[id];

	this.insertVideo($("video_auswertung_url_" + id, this.exercise).text());

	$("#video_question").text($("frage_" + id, this.exercise).text());
	var selectedAnswer = "\"" + $("frage_" + id + "_antwort_" + myAnswer, this.exercise).text() + "\"";
	$("#video_selected_answer").text(selectedAnswer);

	var result;
	if($("frage_" + id + "_antwort_" + myAnswer + "_richtig", this.exercise).text() != "0") {
		result = "Richtig!<br>";
		$("#video_result").css("color", "#02A53C");
		$("#video_label").hide();
	} else {
		result = "Falsch! Die richtige Antwort wäre gewesen:<br>";
		$("#video_result").css("color", "#C00");
		$("#video_label").show();
	}
	result += $("frage_" + id + "_erklaerung", this.exercise).text()
	$("#video_result").html(result);
	if(this.questionId > 1)
		$("#video_back").show();
	else
		$("#video_back").hide();

}

VideoModule.prototype.onNextResult = function() {

	if(this.questionId == 3) {

		Phire.sendNotification(Notifications.SHOW_ALERT_PANEL, {
			text : "Die Auswertung ist abgeschlossen.",
			ok : "Zurück",
			cancel : "OK",
			callback : $.callback(this, this.onDecisionResult)
		});
		return;
	}

	this.questionId++;

	$("#video_progress" + (this.questionId - 1)).show();
	this.nextResult();

}

VideoModule.prototype.onPrevResult = function() {

	$("#video_progress" + (this.questionId - 1)).hide();
	this.questionId--;
	this.nextResult();
}

VideoModule.prototype.onDecisionResult = function(value) {

	if(!value) {
		this.loadMenu();
	}
};

