$(document).ready(function(){
	
	/**
	 * The quiz object
	 */
	window.quiz = {
		currentPart: 0,
		currentQuestion: 0,
		
		/**
		 * Questions in part
		 *
		 * @param integer
		 */
		getQuestionsInPart: function(part){
			return $("#quiz-sidebar > div[rel='" + part + "'] > ul > li").length;
		},
		
		/**
		 * Num parts
		 *
		 * @return integer
		 */
		getNumParts: function(){
			return $(".quiz-part").length;
		},
		
		/**
		 * Get last part
		 *
		 * @return integer
		 */
		getLastPart: function(){
			return ($(".quiz-part").length - 1);
		},
		
		/**
		 * Set question
		 *
		 * @param integer	part
		 * @param integer	question number
		 */
		setQuestion: function(part, i){
			$(".quiz-question.active").fadeOut(300, function(){
				$(".quiz-part, .quiz-question, #quiz-sidebar li").removeClass("active");
				$(".quiz-part[rel='" + part + "']").addClass("active");
				$("#quiz-sidebar > div[rel='" + part + "'] > ul > li[rel='" + i + "']").addClass("active");
				
				$(".quiz-part[rel='" + part + "'] .quiz-question[rel='" + i + "']").fadeIn(300, function(){
					$(this).addClass("active");
				});
				
				if($(".quiz-part[rel='" + part + "'] .quiz-question[rel='" + i + "'] input[type='text']").length !== 0){
					$(".quiz-part[rel='" + part + "'] .quiz-question[rel='" + i + "'] input").focus();
				}
				
				window.quiz.currentPart = parseFloat(part);
				window.quiz.currentQuestion = parseFloat(i);
			});
		},
		
		/**
		 * Next question
		 */
		nextQuestion: function(){
			$("input").blur();
			
			var questionsInThisPart = this.getQuestionsInPart(this.currentPart);
			var numParts = this.getNumParts();
			var lastPart = this.getLastPart();
			
			// Check if we should proceed to the next part
			if((questionsInThisPart - 1) == this.currentQuestion){
				
				// Last part? Then post it!
				if(this.currentPart == lastPart){
					$("#quiz-form").submit();
					return false;
				}
				
				// Next part
				this.setQuestion(this.currentPart + 1, 0);
			}
			else{
			
				// Next question
				this.setQuestion(this.currentPart, this.currentQuestion + 1);
			}
		}
	};
	
	/**
	 * Click in quiz sidebar
	 */
	$("#quiz-sidebar a").click(function(){
		var part = $(this).parent().parent().parent().attr("rel");
		var i = $(this).parent().attr("rel");
		
		window.quiz.setQuestion(part, i);
		
		return false;
	});
	
	/**
	 * Next button
	 */
	$(".next-button").click(function(){
		window.quiz.nextQuestion();
		
		return false;
	});
	
	/**
	 * Quiz button
	 */
	$(".quiz-button").click(function(){
		$(this).siblings("input").removeAttr("checked");
		
		if($(this).hasClass("yes")){
			$(this).siblings("input[value='1']").attr("checked", "checked");
		}
		else{
			$(this).siblings("input[value='0']").attr("checked", "checked");
		}
		
		window.quiz.nextQuestion();
		
		return false;
	});
	
	/**
	 * Text field
	 */
	$("#quiz-content input[type=text]").keyup(function(e){
		if(e.keyCode == 13){
			window.quiz.nextQuestion();
		}
	});
	
	/**
	 * Radio button
	 */
	$("#quiz-content input[type=radio]").click(function(){
		window.quiz.nextQuestion();
	});
});

function post(){
	$("form").submit();
}
