$(function() {
	
	// state variables
	var first_time = true;
	var current_page = 1;
	var emailPat = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

/*
	begin functions
*/

	function addHover() {
		$("#next-arrow, #prev-arrow, #submit-assessment").css({ cursor : "pointer", opacity : 0.5 }).hover(function () {
			$(this).stop().fadeTo(500, 1);
		}, function () {
			$(this).stop().fadeTo(500, 0.5);
		});
	}
	
	function scrollQuizTop() {
		var y = $(".quiz").offset().top;
		var target = y - 10;
		// $("html, body").animate({ scrollTop : target}, (Math.round(target*1.2)) );
		$("html, body").animate({ scrollTop : target}, (Math.round(target*1.2)) );
	}

					
	function changeToPage1 () {
		$("#quiz-page-2, #quiz-page-3, #quiz-page-4").css({ display : "none" });
		$("#quiz-page-1").css({ display : "block" });
		$("#page2, #page3, #page4").removeClass("active-page");
		$("#page1").addClass("active-page");
		$("#submit-assessment").css({ display : "none" });
		$("#prev-arrow").css({ display : "none" });
		$("#next-arrow").css({ display : "block" });
		addHover();
		current_page = 1;

	}
	
	function changeToPage2 () {
		$("#quiz-page-1, #quiz-page-3, #quiz-page-4").css({ display : "none" });
		$("#quiz-page-2").css({ display : "block" });
		$("#page1, #page3, page4").removeClass("active-page");
		$("#page2").addClass("active-page");
		$("#submit-assessment").css({ display : "none" });
		$("#prev-arrow").css({ display : "block" });
		$("#next-arrow").css({ display : "block" });
		addHover();
		current_page = 2;

	}
	
	function changeToPage3 () {
		$("#quiz-page-1, #quiz-page-2, #quiz-page-4").css({ display : "none" });
		$("#quiz-page-3").css({ display : "block" });
		$("#page1, #page2, #page4").removeClass("active-page");
		$("#page3").addClass("active-page");
		$("#submit-assessment").css({ display : "none" });
		$("#prev-arrow").css({ display : "block" });
		$("#next-arrow").css({ display : "block" });
		addHover();
		current_page = 3;

	}
	
	function changeToPage4 () {
		$("#quiz-page-1, #quiz-page-2, #quiz-page-3").css({ display : "none" });
		$("#quiz-page-4").css({ display : "block" });
		$("#page1, #page2, #page3").removeClass("active-page");
		$("#page4").addClass("active-page");
		$("#submit-assessment").css({ display : "block" });
		$("#prev-arrow").css({ display : "block" });
		$("#next-arrow").css({ display : "none" });
		addHover();
		current_page = 4;

	}
	
/*
	shuffle function
*/

	// shuffle array function
	shuffle = function(o){
		for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
		return o;
	};
	
	var i = 0;
	// create and fill array with integers 0 to 27
	var arr = new Array();
	for (i=0; i<28; i++) { arr[i] = i; }
	// shuffle array
	var question_order = shuffle(arr);
	
	// create array of question objects
	var questions = new Array();
	var questions = $(".question");	
	
	// empy all question pages
	$("#quiz-page-1, #quiz-page-2, #quiz-page-3, #quiz-page-4").empty();
	
	// populate each question using the random order
	for (i=0; i<7; i++) {
		$("#quiz-page-1").append(questions[question_order[i]]);	
	}
	for (i=7; i<14; i++) {
		$("#quiz-page-2").append(questions[question_order[i]]);	
	}
	for (i=14; i<21; i++) {
		$("#quiz-page-3").append(questions[question_order[i]]);	
	}
	for (i=21; i<28; i++) {
		$("#quiz-page-4").append(questions[question_order[i]]);	
	}
	
	// add question numbers
	var N = $(".question .two-cols .col1 p").length; // n questions

/*
	quiz setup
*/

	/* custom css */
	$("p.gradient img").css({ opacity : 0.7 });
	$("div.col1 p").css({ "-moz-border-radius" : "5px", "-webkit-border-radius" : "5px" });

	addHover();

	// hide pages
	$("#quiz-page-2, #quiz-page-3, #quiz-page-4, #submit-assessment").css({ display : "none" });
	$("#quiz-page-1").css({ display : "block" });

/*
	quiz functionality
*/

	// add page tabs functionality 		
	$("#page1").click(function (){ changeToPage1(); });
	$("#page2").click(function (){ changeToPage2(); });
	$("#page3").click(function (){ changeToPage3();	});
	$("#page4").click(function (){ changeToPage4(); });
	
	// arrow functionality
	$("#prev-arrow").click(function(){
		scrollQuizTop();
		switch (current_page) {
			case 2: changeToPage1(); break;
			case 3: changeToPage2(); break;
			case 4:	changeToPage3(); break;
		}	
	});
	$("#next-arrow").click(function(){
		scrollQuizTop();
		switch (current_page) {
			case 1: changeToPage2(); break;
			case 2: changeToPage3(); break;
			case 3:	changeToPage4(); break;
		}
	});
	
}); // end $ function
