﻿$(document).ready(function() { 
	
	// Amend updateAnswer in quiz.inc if timer is changed here
	var countDownFrom = 20;
	
	function updateProgress(info) {
		// console.log("Update Progress");

		$("#progress").load("/_public/ajax/updateProgress.htm?id=" + info);

	}


	function timerSetup() {
		// console.log("Timer Setup");

		// Setup the timer and start the countdown
		var questionTime = null;
		questionTime = new Date();
		questionTime.setSeconds(questionTime.getSeconds() + countDownFrom);
		
		$("#countdownPanel").countdown({until: questionTime, onExpiry: expireQuestion, format: "S", compact: true});

	}


	// Expire Question
	function expireQuestion() {
		// console.log("Expired Question");

		a = $("#answerID").val();
		b = $("#questionID").val();
		c = "0";
		$("#answerPanel").load("/_public/ajax/updateAnswer.htm?answerID=" + a + "&questionID=" + b + "&answer="+ c, {}, function() 
			{
			updateProgress("Expire")
			});
		$("input.answer").attr("disabled", "disabled");
		$("#questionPanel").fadeTo("slow", 0.70);
		$("#countdownPanel").countdown("destroy");

	}



	// Start Click
	$("p.start").live('click', function(){
		// console.log("Start Click");
      // Remove the 'Start' Panel from #answerPanel
		$("#answerPanel").html("");
		
		// Load a question
		$("#questionPanel").load("/_public/ajax/start.htm");
		$("#questionPanel").load("/_public/ajax/getQuestion.htm", {}, function() 
			{
			updateProgress("Start");
			timerSetup();
			});
		
	});



	// Answer Click 
	$(".answer").live('click', function(){
		// console.log("Answer Click");

		a = $("#answerID").val();
		b = $("#questionID").val();
		c = this.value;
		d = $(".countdown_amount").html();
		
		$("#answerPanel").load("/_public/ajax/updateAnswer.htm?answerID=" + a + "&questionID=" + b + "&answer="+ c + "&timer=" + d, {}, function() 
			{
			updateProgress("Answer")
			});
		
		$("input.answer").attr("disabled", "disabled");
		$("#questionPanel").fadeTo("slow", 0.70);
		$("#countdownPanel").countdown("destroy");

	});



	// Next Question Click
	$("p.next").live('click', function(){
		// console.log("Next Question Click");
		$("#questionPanel").fadeTo("slow", 0.00);
		
		// Remove the question
		$("#questionPanel").html("");
		// Remove the answer from #answerPanel
		$("#answerPanel").html("");
		
		// Load a question
		$("#questionPanel").load("/_public/ajax/getQuestion.htm", {}, function() 
			{
			updateProgress("Next");
			timerSetup();
			});
		
		$("input.answer").attr("disabled", "");
		$("#questionPanel").fadeTo("slow", 1.00);
		
		
		
	});



	// serverStats Select
	$("#gameTypeID").change(function(){
	document.allUserGameStats.submit();
	});


	// serverStats Select
	$("#period").change(function(){
	document.allUserGameStats.submit();
	});


   
}); 

