

function tnzQuiz(quizId, quiz) {
	this.quizId = quizId;
	this.quiz = quiz;
	this.numOptions = 0;
	this.numQuestions = 0;
	this.sumQuiz = false;
	
	
	// Calculate Frequency Results
	this.calcFrequencyResults = function() {
		var results = new Array(this.numOptions);
		var sortedResults = new Array(this.numOptions);
		var winners = new Array();
		var winner = -1;
		
		// First, check that everything has been answered
		if (!this.checkAnswers()) return;	
	
		// Initialize results
		for (i=0; i<this.numOptions; i++) { results[i] = sortedResults[i] = 0; }
	
		// Tally results for each answer
		$(this.quiz).find("input:checked").each(function() {
			results[$(this).attr("value")] += 1;
			sortedResults[$(this).attr("value")] += 1;
		});
		
		// Figure out the winnah
		var winFreq = sortedResults.sort(this.sortNumber)[this.numOptions-1];
		for (i=0; i<this.numOptions; i++) {
			if (results[i] == winFreq) winners.push(i);
		}
		
		// If there's more than one winner, pick one
		winner = winners[Math.round(Math.random()*(winners.length-1))];

		// Display winner
		$(this.quiz).find(".results .res"+winner).addClass("winner");
		// Remove submit button
		$("#quiz"+this.quizId+"_submit").remove();
	};
	
	
	// Calculate Sum Results
	this.calcSumResults = function() {
		var sum = 0;
		var qid = this.quizId;
		
		// First, check that everything has been answered
		if (!this.checkAnswers()) return;	
		
		// Add up results
		$(this.quiz).find("input:checked").each(function() {
			sum += new Number($(this).attr("value"));
		});
		//alert(sum);
		// Display winner
		$(this.quiz).find(".results div").each(function() {
			if (sum <= new Number($(this).attr("pts"))) {
				// Display winner
				$(this).addClass("winner");
				// Remove submit button
				$("#quiz"+qid+"_submit").remove();
				return false;
			}
		});
	}
	
	
	// Check that all quesitons have been answered
	this.checkAnswers = function() {
		$(this.quiz).find(".question").removeClass("error");
		if (this.numQuestions > $(this.quiz).find("input:checked").length) {
			// TODO : Show error message?
			$(this.quiz).find(".question").each(function(i) {
				if ($(this).find("input:checked").length == 0) $(this).addClass("error");
			});
			return false;
		}
		return true;
	};
	
	
	// Number sorting function
	this.sortNumber = function(a,b) { return a-b; }
	
	
	
	// Constrcutor foo - set up the functionality, here
	// Add quiz id
	$(quiz).attr("id", "quiz"+quizId);
	
	// Set the quiz type
	this.sumQuiz = sq = ($(this.quiz).hasClass("sumQ"));
	
	// Set up questions and answers
	this.numQuestions = $(quiz).find(".question").length;
	$(quiz).find(".question").each(function(i) {
		// Set the class: q<question #>
		$question = $(this);
		$question.addClass("q"+i);
		$question.find("ul li").each(function(j) {
			// Set the class: a<answer #>
			$(this).addClass("a"+j);
			// Add the radio button
			radio = "<input type='radio' name='question"+i+"' value='";
			radio += (sq?$(this).attr("value"):j);
			radio += "' />";
			$(this).prepend(radio);
		});
	});
	
	// Set up results
	$(quiz).find(".results div").each(function(i) {
		$(this).addClass("res"+i);
	});
	
	// Set up submit button
	$question.append("<div class='submit'><input type='submit' value='Show Results!' id='quiz"+quizId+"_submit' /></div>");
	$button = $("#quiz"+this.quizId+"_submit");
	if (sq)
		$button.click(function() { 
			quizzes[quizId].calcSumResults(); 
		});
	else
		$button.click(function() { 
			quizzes[quizId].calcFrequencyResults(); 
		});
	
	// Figure out how many options there are per question
	this.numOptions = $(this.quiz).find(".q0 ul li").length;
}



// Set up the quizzes on this page

var quizzes = new Array();

$(function() {
	// Go through all quizzes on the page and add functionality
	$(".tnzQuiz").each(function (i) {
		// Add new quiz
		quizzes[i] = new tnzQuiz(i, this);
	});
});
