/**
 * Quiz
 * @class
 * @author grotter
 * @version 1.0
 */
Quiz = function (myObj, ajaxUrl, myContext) {
	this.scrollable_api;
	this.quiz_obj;
	var $ = jQuery;
	var context = myContext;
	
	if (typeof(ajaxUrl) == "undefined") {
		ajaxUrl = "php/";
	}
	
	/**
     * Set scroller height to the tallest item calculated
	 * @public
	 */
	this.setSize = function () {
		var heights = [];
		
		$(".item", context).each(function () {
			if ($(this).hasClass("answer")) {
				//capture heights for both possible
				$(".correct", $(this)).show();
				$(".incorrect", $(this)).hide();
				heights.push($(this).height());
				
				$(".correct", $(this)).hide();
				$(".incorrect", $(this)).show();
				heights.push($(this).height());
			} else {
				heights.push($(this).height());
			}
		});
		
		var maxHeight = Math.max.apply(Math, heights).toString() + "px";
		$(".item", context).css("height", maxHeight);
		$(".scrollable", context).css("height", maxHeight);
	}
	
	this.track = function (index) {
		if (typeof(pageTracker) != "object") return;
		
		var screen = $(".scrollable .item", context).get(index);
		var code = "/extrememammals/quiz/" + $(".uid_question", screen).text() + "/";
		
		if ($(screen).hasClass("image")) {
			code += "image";
		} else if ($(screen).hasClass("question")) {
			code += "question";
		} else {
			code += "answer";
		}
		
		pageTracker._trackEvent(code, location.href);
	}
	
	/**
     * Position buttons to float bottom
	 * @public
	 * @param {Number} index Target item index
	 */
	this.positionButtons = function (index) {
		var screen = $(".scrollable .item", context).get(index);
		
		$(".buttons", screen).each(function () {
			var padding = $(".scrollable", context).height() - $(this).height() - $(this).position().top;
			
			if ($(screen).hasClass("image")) {
				padding -= 3;
			} else {
				padding -= 5;
			}
			
			if (padding < 0) padding = 0;
			$(this).css("padding-top", padding.toString() + "px");
		});
	}
	
	/**
	 * Send scrollable to a specified item
	 * @public
	 * @param {Number} index Target item index
	 */
	this.navTo = function (index) {
		//pause the homepage Flash header
		if (typeof(pause) == "function") pause(true);
		
		this.track(index);
		this.displayStats(index);
		this.positionButtons(index);
		this.scrollable_api.seekTo(index);
	}
	
	/**
	 * If target is a results screen, append some stats
	 * @public
	 * @param {Number} index Target item index
	 */
	this.displayStats = function (index) {
		var screen = $($(".scrollable .item", context).get(index));
		
		//not a results screen
		if ($(".result", screen).length == 0) return;
		
		//stats already present, must be the last question
		if ($(".quiz-stats", screen).length > 0) return;
		
		//add the stats
		var per = Math.round((parseInt($(".total_correct", screen).html()) / parseInt($(".total_responses", screen).html())) * 100);
		if (isNaN(per)) per = 0;
		
		var stats = $("<p />");
		stats.addClass("quiz-stats");
		stats.html("<strong>" + per + "%</strong> of respondents got this one right.");
		
		$(".result", screen).after(stats);
	}
	
	/**
	 * Send response to server via AJAX
	 * @public
	 * @param {Object} radio Checked input element
	 */
	this.recordResponse = function (radios) {
		var post_data = this.quiz_obj.getPostData(radios);
		
		$.ajax({
			type: "POST",
			url: ajaxUrl,
			cache: false,
			data: post_data,
			success: this.quiz_obj.onSubmitSuccess,
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				//alert(textStatus + "/" + errorThrown);
			} 
		});
	}
	
	this.initNavigation = function () {
		var inst = this;
		
		$(".item", context).each(function () {
			var index = $(".item", context).index($(this));
			var myItem = $(this);
			var nextItem =  $(".item", context).get(index + 1);
			
			//validate form and submit
			$(".quiz-submit", myItem).click(function () {
				if (!inst.quiz_obj.isValid(myItem)) return;
				
				var callback = function () {
					//Send response to server
					inst.recordResponse($("input:checked", myItem));

					//Show / hide the result
					if ($("input:checked", myItem).hasClass("c")) {
						$(".correct", nextItem).show();
						$(".incorrect", nextItem).hide();
					} else {
						$(".correct", nextItem).hide();
						$(".incorrect", nextItem).show();
					}
					
					inst.navTo(index + 1);
				}
				
				callback();
				return false;
			});
			
			//no form submission, just progress to next item
			$(".quiz-next", myItem).click(function () {
				var callback = function () {
					inst.navTo(index + 1);
				}
				
				callback();
				return false;
			});
			
			//back
			$(".quiz-back", myItem).click(function () {
				var callback = function () {
					inst.navTo(index - 1);
				}
				
				callback();
				return false;
			});
		});
	}
	
	this.setScrollableApi = function (api) {
		this.scrollable_api = api;
	}
	
	this.initialize = function (obj) {
		this.quiz_obj = obj;
	}
	
	this.initialize(myObj);
};

