$(document).ready(function(){
	setup_survey();
	
	/**
	 * Hides the survey markup when the page loads
	 * Calls show_survey when an element with a specific class is clicked
	 */
	function setup_survey()
	{
		// show survey on click
		$(".cover-art-survey").click(function() {
			show_survey();
		});

		// move dialog up and hide
		$("#survey .dialog").hide();
		$("#survey .dialog").css("top", "-100%");
		$("#survey .dialog").css("opacity", 0);
	}
	
	/**
	 * Scrolls to the top of the page and show show the survey
	 */
	function show_survey()
	{
		scroll_up();
		
		// show survey
		$("#blackout-survey").height($(document).height());
		$("#blackout-survey").fadeIn(function() {
			$("#survey").fadeIn();
			setup_answer_events();
			setup_close_events();
		});
	}
	
	function setup_close_events()
	{
		$("#close-survey a").unbind("click");
		$("#close-survey a").click(function() {
			// fade the survey out
			$("#survey").fadeOut(function() {
				$("#blackout-survey").fadeOut();
			});
			return false;
		});
	}
	
	/**
	 * Highlights answer when clicked and if necessary discards the previous answers
	 */
	function setup_answer_events()
	{
		$("div.box div.option").unbind("click");
		$("div.box div.option").click(function() {
			var this_box = $(this).parent("div.box");
			
			// remove selection from this sibling and highlight this answer
			this_box.siblings().removeClass("selected");
			this_box.addClass("selected");
			
			// highlight text field if other picked
			if($(this).text() == "Other") {
				this_box.find("input")[0].focus();
			}
			
			check_if_submit_allowed();
		});
		
		$("div.findout p.tease").unbind("click");
		$("div.findout p.tease").click(function() {
			$(this).siblings("div.info").slideToggle();
			
		});
		
		$("#size-answers input").unbind("keyup");
		$("#size-answers input").keyup(function() {
			check_if_submit_allowed();
		});
	}
	
	/*
	 * Enabled the submit button if form validates
	 * Otherwise submit button is disabled
	 */
	function check_if_submit_allowed()
	{
		if($("div.box.selected div.option").length < 3) {
			// all questions not answered
			disable_submit_button()
			return;
		}
		
		if($("#size-answers div.box.selected div.option:contains('Other')").length) {
			if($("#size-answers input").val() == "") {
				// other field is empty
				disable_submit_button()
				return;
			}
		}
		
		// ready to submit
		enable_submit_button();
	}
	
	/**
	 * Put answers into an Array and return it
	 */
	function get_answers()
	{
		$("div.box div.option").unbind("click");
		
		var answers = {};
		$("div.box.selected div.option").each(function(i) {
			var answer = $(this).text();
			if(answer == "Other") {
				var this_box = $(this).parent("div.box");
				answer = this_box.find("input").val();
			}
			
			answers[i] = answer;
		});
		
		return answers;
	}
	
	/**
	 * Disable submit button and and unbind event handlers
	 */
	function disable_submit_button()
	{
		$("div#send-survey").addClass("disabled");
		$("div#send-survey").unbind("click");
	}
	
	/**
	 * Enable submit button and bind a single event handler
	 */
	function enable_submit_button()
	{
		$("div#send-survey").removeClass("disabled");
		$("div#send-survey").unbind("click");
		$("div#send-survey").click(function() {
			show_dialog();
		});
	}
	
	/**
	 * Show the dialog and prompt for comments
	 */
	function show_dialog()
	{
		scroll_up();
	
		var answers = get_answers();
		
		// hide the form
		$("#survey .main-form").css("opacity", .33);
		
		// disable close button
		$("#close-survey").css("opacity", .33);
		$("#close-survey a").unbind("click");
		
		
		// show dialog and slide it down
		$("#survey .dialog").show();
		$("#survey .dialog").animate({
			top: "0%",
			opacity: 1
		}, "slow");
		
		
		// save survey
		$.post(URL_ROOT + "survey/save/answers/", answers);
		
		// focus text field
		$("textarea[name=comments]").focus();
		
		$("div.survey-button").unbind("click");
		$("div.survey-button").click(function() {
			done_clicked();
		});
	}
	
	/**
	 * Handle the event when the done button is clicked
	 * Resets the form so it can be filled in again... 
	 * We have no way of preventing multiple submissions
	 */
	function done_clicked()
	{
		// save comments
		var comment = $.trim($("textarea[name=comments]").val());
		if(comment != "") {
			$.post(URL_ROOT + "survey/save/comments/", {comment: comment});
		}
		
		// slide dialog up
		$("#survey .dialog").show();
		$("#survey .dialog").animate({
			top: "-100%",
			opacity: 0
		}, "slow", function() {
			// fade the survey out
			$("#survey").fadeOut(function() {
				$("#blackout-survey").fadeOut();		
				
				// clear comments
				$("textarea[name=comments]").val("")
				
				// show the form
				$("#survey .main-form").css("opacity", 1);
				
				// show the close button
				$("#close-survey").css("opacity", 1);
				
				// clear the answers
				$("div.box.selected").removeClass("selected");
				
				// disable the submit button
				disable_submit_button();
				
				// hide the info boxes
				$("#survey div.info").hide();
			});
		});
	}
	
	function scroll_up()
	{
		// scroll to the top of the page
		$(window).scrollTop(0);
	}
});
