var wrong = 0;

$(document).ready(function(){

	/* flash movie */
	var pluginOptions = { version: 8, update: false };
	$('#cover').flash({ src: 'swf/title.swf', width: 967, height: 343, wmode: 'transparent' }, pluginOptions);
		
	/* hide score table initially */
	$("#score").hide();
	$("#dialog").hide();
	
	/* Slideshow Animation */
	$("#start-quiz, #quiz .correct").click(function(){
		$("#score").fadeOut("slow", function(){
			$("#quiz>ol").animate({marginLeft: "-=967px"}, 2500, "easeOutBounce", function(){
				$("#score").fadeIn();
			});
		});
		return false;
	});				
	
	/* slide whole page up to a certain point */
	$("#toggle-quiz").click(function(){												
		if (this.className == '') {
			$("#container").animate({marginTop: "-=343px"}, 1500 );
			$(this).addClass("show-quiz");
		} else {
			$("#container").animate({marginTop: "+=343px"}, 1500 );
			$(this).removeClass("show-quiz");					
		}								
		return false;
	});
	
	/* Quiz correct answers
	 * = Q1A, Q2B, Q3B, Q4A, Q5B
	 */	
	$("#q1 .a").click(function(){
		$("#score .indicator li:first-child").addClass("correct");
		setDialog('correct-dialog','<strong>Correct!</strong> Although the length of survival mostly depends upon climate conditions and physical exertion, anyone going without water for even just one day will be in desperate straights.');
		return false;
	});
	$("#q2 .b").click(function(){
		$("#score .indicator li:first-child+li").addClass("correct");		
		setDialog('correct-dialog','<strong>Correct!</strong> In truth water needs depend on many factors; no single formula that fits every one. But drinking more water is advised for proper metabolism, as our body is mostly water. Some dietary recommendations advise men consume around 3 liters and women 2.2 liters daily.');
		return false;
	});
	$("#q3 .b").click(function(){
		$("#score .indicator li:first-child+li+li").addClass("correct");
		setDialog('correct-dialog','<strong>Correct!</strong> Muscle is made up of about 75% water, fat is made up of about 50% water and bones, too, are about 50% water.');
		return false;
	});
	$("#q4 .a").click(function(){
		$("#score .indicator li:first-child+li+li+li").addClass("correct");
		setDialog('correct-dialog','<strong>Correct!</strong> One person in six lives without regular access to safe drinking water; over twice that number-2.4 billion-lack access to adequate sanitation. In reality these figures should be even higher.');
		return false;
	});
	$("#q5 .b").click(function(){
		$("#score .indicator li:first-child+li+li+li+li").addClass("correct");
		setDialog('correct-dialog','<strong>Correct!</strong> This is the last and the most difficult question. You must be an American to get this right! So it means an American family of five could use up to 100 times of the amount used by an African family.');
		return false;
	});	

	/* Quiz incorrect answers
	 * = Q1B, Q2A, Q3A, Q4B, Q5A
	 */	
	$("#q1 .b").click(function(){
		$("#score .indicator li:first-child").addClass("incorrect");
		$("#score .mark").addClass("wrong-" + (++wrong));
		setDialog('incorrect-dialog','<strong>Incorrect!</strong> Water is by far the most important nutrient for the human body besides oxygen, going without it for more than 3 days could kill you already.');		
		return false;
	});
	$("#q2 .a").click(function(){
		$("#score .indicator li:first-child+li").addClass("incorrect");
		$("#score .mark").addClass("wrong-" + (++wrong));
		setDialog('incorrect-dialog','<strong>Incorrect!</strong> 1 liter is too less! 1.5 liters is the average daily urine output for adults with another liter lost from breathing, sweating and bowel movements. If less than 2 liters of water is consumed along with your normal diet, your body will not be able to properly carry out normal functions.');
		return false;
	});
	$("#q3 .a").click(function(){
		$("#score .indicator li:first-child+li+li").addClass("incorrect");
		$("#score .mark").addClass("wrong-" + (++wrong));
		setDialog('incorrect-dialog','<strong>Incorrect!</strong> The human body is anywhere from 55% to 78% water depending on body size. The precise amount depends on the level of activity, temperature, humidity and other factors.');
		return false;
	});
	$("#q4 .b").click(function(){
		$("#score .indicator li:first-child+li+li+li").addClass("incorrect");
		$("#score .mark").addClass("wrong-" + (++wrong));
		setDialog('incorrect-dialog','<strong>Incorrect!</strong> Much more than that! To more than two billion people, fresh, pure water is more valuable than gold. The problem is not confined to the developing world, already about one-third of the world\'s population lives in countries suffering from moderate-to-high water stress.');
		return false;
	});
	$("#q5 .a").click(function(){
		$("#score .indicator li:first-child+li+li+li+li").addClass("incorrect");
		$("#score .mark").addClass("wrong-" + (++wrong));
		setDialog('incorrect-dialog','<strong>Incorrect!</strong> This is the last and the most difficult question. It is beyond imagination that an individual in America could use 5 times of water used by a family in Africa!');
		return false;
	});
	
	$("#dialog>div, #dialog>div a").click(function(){		
		$("#dialog").fadeOut();
		return moveToNext();
	});

});		

function setDialog(cls, str) {
	var result = 5-parseInt(wrong);
	var percents = [0, 14, 28, 42, 56, 70]; 
	$("#test-result").text(result);
	$("#test-percent").text(percents[result]);
	$("#dialog").removeClass("incorrect-dialog");
	$("#dialog").removeClass("correct-dialog");
	$("#dialog").addClass(cls);
	$("#dialog div p").html(str);
	$("#dialog").fadeIn();
}

function moveToNext() {	
	$("#score").fadeOut(function(){
		$("#quiz>ol").animate({marginLeft: "-=967px"}, 2500, "easeOutBounce", function(){
			$("#score").fadeIn();
		});					
	});
	return false;
}			

