// overwrite two gallery.js functions to prevent actions not desired in quizzes
PhotoGallery.truncatelimit = 10000;
togglepreviousnextlinks = function() {return;};
// NEW quiz JS
PEOPLE.Quiz = {
	slide : 1,
	tally : [],
	right : 0,
	wrong : 0,
	init : function() {
		var q = $('#thisslide').html().replace('Q: ',''); // add span around question #
		$('#thisslide').html('Q: <span>' + q + '</span>');
		var button = $('#caption p.button').remove(); // move button
		$('#scrollbar').append(button);
		$('#slide ul.navigation a:first,#scrollbar p.button a:first').attr('href','javascript:void();').unbind();
		for (i = 0; i < (gallery.length / 2); i++) { // initialize tally array
			PEOPLE.Quiz.tally.push(0);
		};
	},
	updatecontent : function() { // check content and clean-up if necessary (landed in mid-quiz or used Back/Forward button)
		var slide = Number(findslidekey());
		if (slide > 0) return;
		this.hijacklinks(); // add onclick to button
		this.updateslide(); // check if correct slide #
		if (slide % 2 == 0) {$('#scrollbar div.result').remove();}; // kill "correct/incorrect" if on "question" slide
		if ($('#rightColumn div.txtcont:first').css('display') == 'none'){$('#rightColumn div.txtcont:first').slideDown();}; // reveal content in #rightColumn
		$('#tint,#finalresults').remove(); // kill "final results" overlay if it exists
	},
	updateslide : function() { // updates Q: x of xx
		var q = Number(findslidekey());
			q = q / 2 + 1;
			q = q.toString().replace('.5','');
		this.slide = Number(q);
		$('#thisslide span').text(q);
	},
	hijacklinks : function() { // override in-page links with onclick events
		var slide = Number(findslidekey());
		var links = $('#slide ul.navigation a:first,#scrollbar p.button a:first');
		if (slide % 2 == 0) { // determine which button should appear
			links.unbind().click(function(){this.blur();PEOPLE.Quiz.seeanswer();return false;}).html('<span>See Answer</span>');
		} else {
			if ((slide + 1) == gallery.length) { // if the last Answer, button s/b Get Final Results
				links.unbind().click(function(){this.blur();PEOPLE.Quiz.getfinalresults();return false;}).html('<span>Get Final Results</span></a>');
			} else {
				links.unbind().click(function(){this.blur();PEOPLE.Quiz.nextquestion();return false;}).html('<span>Next Question</span>');
			};
		};
	},
	checkscore : function() { // check user's answer
		var slide = gallery[findslidekey()].split('_'); // 20294124_20294132
		var image = $('#slide_'+slide[slide.length-1].replace(/\-1/,'')+' img:first').attr('src'); // li#slide_20294132
		var correctid = slideArrays[image]['correctid']; // all of that to get the correctid value from the JSON for "this" slide
		var result = '', right = 0, wrong = 0;
		if ($('#answers input:radio').is(':checked') && $('#' +correctid).is(':checked')) { // user checked something and it is correct
			this.tally[Number(this.slide)-1] = 1;
			result = 'right';
		} else { // user either didn't checked anything or the checked answer is incorrect
			this.tally[Number(this.slide)-1] = -1;
			result = 'wrong';
		};
		for (i = 0; i < this.tally.length; i++) {
			if (this.tally[i] > 0) right++;
			if (this.tally[i] < 0) wrong++;
		};
		this.right = right;
		this.wrong = wrong;
		$('#right span:first').html(this.right); // change in-page number right
		$('#wrong span:first').html(this.wrong); // change in-page number wrong
		return result; // return result to seeanswer()
	},
	seeanswer : function() { // transition to answer content, checking if user's answer was right or wrong
		if (busy) return false;
		busy = true;
		nextslide();
		$('#scrollbar p.button').customFadeOut(100,function(){
			var score = PEOPLE.Quiz.checkscore();
			var result = '<div id="result" class="result" style="display:none;">'; // build additional page content
			if (score == 'right') {
				result += '<h4 class="correct">Correct!</h4>';
			} else {
				result += '<h4 class="incorrect">Incorrect!</h4>';
			};
				result += '</div>';
			var links = $('#slide ul.navigation a:first,#scrollbar p.button a:first'); // set-up local variables
			if ((PEOPLE.Quiz.slide) == (gallery.length / 2)) { // if the last Answer, button s/b Get Final Results, otherwise Next Question
				links.unbind().click(function(){this.blur();PEOPLE.Quiz.getfinalresults();return false;}).html('<span>Get Final Results</span></a>');
			} else {
				links.unbind().click(function(){this.blur();PEOPLE.Quiz.nextquestion();return false;}).html('<span>Next Question</span></a>');
			};
			$('#scrollbar').prepend(result); // insert html ABOVE #caption
			$('#scrollbar div.result').slideDown(700);
			$('#scrollbar p.button').customFadeIn(3000);
		});
	},
	nextquestion : function() { // transition to next question
		if (busy) return false;
		busy = true;
		nextslide();
		$('#scrollbar p.button').customFadeOut(100,function(){
			$('#thisslide span').customFadeOut(700,function(){PEOPLE.Quiz.updateslide();$('#thisslide span').customFadeIn(700);});
			$('#scrollbar div.result').slideUp(700,function(){$('#scrollbar div.result').remove()}); // remove old result, if any
			var links = $('#slide ul.navigation a:first,#scrollbar p.button a:first'); // set-up local variables
				links.unbind().click(function(){this.blur();PEOPLE.Quiz.seeanswer();return false;}).html('<span>See Answer</span></a></p>');
			$('#scrollbar p.button').customFadeIn(3000);
		});
	},
	getfinalresults : function() { // on the last answer, show user final results
		if (busy) return false;
		var header, caption, result = this.right / (this.right + this.wrong), finalnode = slideArray[0].finalresult; // set-up local variables
		if (result > .66) {
			header = finalnode[2].header;
			caption = finalnode[2].caption;
		} else if (result < .34) {
			header = finalnode[0].header;
			caption = finalnode[0].caption;
		} else {
			header = finalnode[1].header;
			caption = finalnode[1].caption;
		};
		// build html
		var html  = '<div id="tint"></div>';
			html += '<div id="finalresults" style="display:none;">';
				html += '<div id="finalresultsscore">';
					html += '<ul>';
						html += '<li id="right"><span>' +this.right+ '</span> Right</li>';
						html += '<li id="wrong"><span>' +this.wrong+ '</span> Wrong</li>';
					html += '</ul>';
				html += '</div>';
				html += '<div id="finalresultscaption">';
					html += '<h4>' +header+ '</h4>';
					html += '<p class="deck">' +caption+ '</p>';
					html += '<p class="button"><a href="/people/archives/quiz/0,,,00.html"><span>Take Another Quiz</span></a></p>';
				html += '</div>';
			html += '</div>';
		$('#rightColumn div.txtcont:first').slideUp(); // kill content in #rightColumn
		$('#slide').append(html); // append results html
		var slideheight = $('#slide').height(); // determine proper position for results overlay
		var resultsheight = $('#finalresults').height();
		var top = (slideheight/2) - (resultsheight/2) + 'px';
		$('#finalresults').css('top',top).fadeIn(700); // show it already!
	}
};
$(function(){ // let's get this party started...
	if(PhotoGallery.dynamicrefresh == true){
		PEOPLE.Quiz.init();
	}
});