// Audiences UK
// Global Javascript functions
// Powered by Prego

$(function()
{
	// Add the "js" class to the body
	// Allows different styles to be applied for browsers with Javascript
	$("body").addClass("js");

	// Set up font size links
	var baseURL = $("base").attr("href");
	$(".text-size a").click(function()
	{		
		$.getJSON(baseURL+'accessibility/textsize/' + $(this).attr("class") + '?mode=ajax', function(response)
		{
			$("body")
				.removeClass("small")
				.removeClass("medium")
				.removeClass("large")
				.addClass(response.textSize);
		});
			
		return false;
	});

	// Set up external links
	$("a[href^=http://]")
		.not("[href^=http://www.audiencesuk.org]")
		.each(function()
		{
			this.target = "_blank";
		});

	// Set up expandable download details
	$("ol.downloads > li").each(function()
	{
		var $title = $(".download-title", this);
		var $details = $(".download-details", this);
		createExpandableSection($title, $details);
	});
	
	// Set up expandable course details
	$(".courses .course1, .courses .course2").each(function()
	{
		var $header = $(".course-header", this);
		var $detail = $(".course-detail", this);
		createExpandableSection($header, $detail);
	});
	
	// Set up expandable definition details
	$("#jargon-definition dt:not(.main)").each(function()
	{
		var $dt = $(this);
		var $dd = $("+dd", this);
		createExpandableSection($dt, $dd);
	});

	// Set up expandable team descriptions
	$("#about-team .vcard").each(function()
	{
		var $title = $(".title", this);
		var $note = $(".detail", this);
		createExpandableSection($title, $note);
	});
	
	// Set up expandable benchmarking details
	$("#benchmarking-subsections .subsection").each(function()
	{
		var $title = $(".title", this);
		var $detail = $(".detail", this);
		createExpandableSection($title, $detail);
	});

	// Set up quiz button animation
	if($("#quiz-button").length > 0)
	{
		setTimeout(animateQuizButton, 3000);
	
		// Initialise the quiz functionality
		$("#quiz-button").click(function()
		{
			quizShow();
			return false;
		});
	
		var $quizBackground = $("<div></div>")
			.attr("id", "quiz-background")
			.click(function()
			{
				quizHide();
				return false;
			});
			
		$("body").append($quizBackground);
	}
	
	// Remove previous error when re-submitting download form
	$("form#download").submit(function()
	{
		$("p.error", this).slideUp("fast", function()
		{
			$(this).remove();
		});
		return true;
	});
});

var quizQuestions = null;
var quizAnswers = null;
var quizIndex = 0;

function quizShow()
{
	scroll(0,0);
	$("#header object").hide();
	$("html").css("overflow", "hidden");
	$("#quiz-background").show();
	
	var $quiz = $("#quiz");
	if($quiz.length == 0)
	{
		$quiz = $("<div id=\"quiz\"><div id=\"quiz-top\"></div><div class=\"inner\"><h2 class=\"major\"></h2><div class=\"content\"></div></div><div id=\"quiz-bottom\"></div></div>")
			.hide();
			
		$("body").append($quiz);
	}
	
	$("#quiz h2").html("Loading...");
	$("#quiz .content").html("");
	$quiz.fadeIn();
	
	$.getJSON("benchmarking/quiz", {ajax:1}, function(data)
	{
		if(!data || typeof(data.questions) == "undefined" || !data.questions)
		{
			quizDisplayError("The quiz questions didn't load correctly.", quizShow);
			return;
		}
		
		quizQuestions = data;
		quizIndex = 0;
		quizAnswers = {};
		quizDisplayQuestion();
	});
}

function quizHide()
{
	$("html").css("overflow", "auto");
	$("#quiz-background").hide();
	$("#quiz").fadeOut("slow", function()
	{
		$("#header object").show();
	});
}

function quizDisplayError(message, retryFunction)
{
	$("#quiz h2").html("Error");
	$("#quiz .content").html("<p>"+message+"</p>");
	
	var $tryLink = $("<a>Try again</a>").click(retryFunction);
	var $closeLink = $("<a>close the quiz</a>").click(quizHide);
	
	$("#quiz .content").append(
		$("<p></p>")
			.append($tryLink)
			.append(" or ")
			.append($closeLink)
			.append(".")
	);
}

function quizDisplayQuestion()
{
	var q = quizQuestions.questions[quizIndex];
	$("#quiz h2").html(q.q);
	
	var $answers = $("<ol/>");
	for(ans in q)
	{
		if(ans == "q")
			continue;
		
		var qID = "q"+quizIndex+"a"+ans;
		$answers.append("<li><label for=\""+qID+"\"><input type=\"radio\" name=\"q"+quizIndex+"\" value=\""+ans+"\" id=\""+qID+"\" /> "+q[ans]+"</label></li>")
	}
	
	$("#quiz .content").html("").append($answers);

	// Make IE6 re-calculate the height
	$("#quiz").css("height", "10px");
	$("#quiz").css("height", "auto");
	
	$("#quiz input").click(function()
	{
		quizIndex++;
		quizAnswers["q"+quizIndex] = $(this).attr("value");
		
		if(quizIndex < quizQuestions.questions.length)
			quizDisplayQuestion();
		else
			quizDisplayResults();
	});
}

function quizDisplayResults()
{
	$("#quiz h2").html("Loading Results...");
	$("#quiz .content").html("");
	
	// Make IE6 re-calculate the height
	$("#quiz").css("height", "10px");
	$("#quiz").css("height", "auto");
	
	var getParams = quizAnswers;
	getParams.ajax = 1;
	$.getJSON("benchmarking/quiz", getParams, function(data)
	{
		if(!data || typeof(data.result) == "undefined" || !data.result)
		{
			quizDisplayError("The quiz results didn't load correctly.", quizDisplayResults);
			return;
		}
		
		$("#quiz h2").html("Results");
		$("#quiz .content").html(data.result + "<p>To find out more about how to undertake benchmarking, read our <a href=\"benchmarking/how\">step-by-step guide to benchmarking</a>.</p>");
		
		// Make IE6 recalculate the height
		$("#quiz").css("height", "10px");
		$("#quiz").css("height", "auto");
	}, "json");
}

function createExpandableSection($title, $body)
{
	if($title.length < 1 || $body.length < 1)
		return;
	
	var parentID = $body.parent().attr("id");
	var fragment = window.location.hash;
	var expand = (parentID != "" && (parentID == fragment || "#"+parentID == fragment));
	
	$moreLink = $("<a href=\"#\" class=\"more screen-only\" title=\"Show details\">Show details</a>").click(function()
	{
		var $link = $(this);
		$body.slideToggle("slow", function()
		{
			if($(this).css("display") == "block")
				$link
					.removeClass("more")
					.addClass("less")
					.attr("title", "Hide details")
					.html("Hide details");
			else
			{
				$link
					.removeClass("less")
					.addClass("more")
					.attr("title", "Show details")
					.html("Show details");
					
				$(this)
					.css("display", "")
					.addClass("print-only");
			}
		});
		
		if(typeof(this.blur) == "function")
			this.blur();
		
		return false;
	});
	
	if(expand)
	{
		$moreLink
			.removeClass("more")
			.addClass("less")
			.attr("title", "Hide details")
			.html("Hide details");
	}
	else
		$body.addClass("print-only");
	
	$title.append($moreLink);
}


function animateQuizButton()
{
	var backgroundPosition = $("#quiz-button").css("background-position");
	var backgroundPositionParts = backgroundPosition.split(" ");
	var x = parseInt(backgroundPositionParts[0]);
	
	x -= 170;
	if(x < -340)
		x = 0;
	
	$("#quiz-button").css("background-position", x+"px "+backgroundPositionParts[1]);
	
	setTimeout(animateQuizButton, 3000);
}
