// Quiz
// override gallery.js
PEOPLE.Photo.checkhash = function() {
	var hash = String(document.location).split('#')[1];
	if (hash && hash != PEOPLE.Photo.slideid) { // hash has changed
		PEOPLE.Photo.hideslide(PEOPLE.Photo.slideid);
		PEOPLE.Photo.slideid = hash;
		PEOPLE.Photo.getslide(PEOPLE.Photo.slideid);
		PEOPLE.Quiz.updateslide(PEOPLE.Photo.slideid);
	}
};

PEOPLE.Quiz = {
	tally : [],
	right : 0,
	wrong : 0,
	init : function() {
		PEOPLE.Quiz.navall = $('#button').find('a:first');
		PEOPLE.Quiz.addids(PEOPLE.Photo.slideid);
		PEOPLE.Quiz.updateslide(PEOPLE.Photo.slideid);
		PEOPLE.Quiz.clicklistener();
		for (var i = -1, len = gallery.length; ++i < len;) { PEOPLE.Quiz.tally.push(0); } // initialize tally array
	},
	addids : function(id) {
		$('#caption_'+id).find('input').each(function(){
			if (!$(this).attr('id').match(/answer_/)) {$(this).attr('id','answer_'+id+'-'+$(this).attr('id'));}
		});
		$('#caption_'+id).find('label').each(function(){
			if (!$(this).attr('for').match(/answer_/)) {$(this).attr('for','answer_'+id+'-'+$(this).attr('for'));}
		});
	},
	updateslide : function(id) {
		var c = $.inArray(PEOPLE.Photo.preid+id,gallery),
			q = Number((c/2)+1).toString().replace('.5',''),
			s = c+1;
		$('#thisslide').html('Q: <span>'+q+'</span>');
// LEAK?
		$('#tint,#finalresults,#result').remove();
		if (PEOPLE.Photo.rightcolumn.find('div.txtcont:first').css('display') == 'none') {PEOPLE.Photo.rightcolumn.find('div.txtcont:first').slideDown();}
		if (s == gallery.length) {
			PEOPLE.Quiz.navall.html('<span>Get Final Results</span></a>');
		} else {
			if (s % 2 === 0) {
				PEOPLE.Quiz.navall.html('<span>Next Question</span>');
			} else {
				PEOPLE.Quiz.navall.html('<span>See Answer</span>');
			}
		}
	},
	clicklistener : function() { // override gallery.js
		PEOPLE.Quiz.navall.click(function() {
			if (PEOPLE.Photo.fetching != '') {return false;}
			this.blur();
			var c = $.inArray(PEOPLE.Photo.preid+PEOPLE.Photo.slideid,gallery),
				s = c+1,
				len = gallery.length,
				interstitial = (PEOPLE.Photo.main.hasClass('interstitial')) ? true : false,
				url = String(window.location).split(',,')[0];
			if (PEOPLE.Photo.hardrefresh && !interstitial) { // hard refresh
				window.location = url+',,'+PEOPLE.Photo.preid+(s+1)+',00.html';
			} else if (s == len) { // on last answer slide
				PEOPLE.Quiz.getfinalresults(PEOPLE.Photo.slideid);
			} else { // in-page transition
				PEOPLE.Photo.resetslide(PEOPLE.Photo.slideid);
				PEOPLE.Photo.hideslide(PEOPLE.Photo.slideid);
				PEOPLE.Photo.slideid = PEOPLE.Photo.replacestring(PEOPLE.Photo.preid,gallery[s]);
				PEOPLE.Photo.getslide(PEOPLE.Photo.slideid);
				PEOPLE.Quiz.addids(PEOPLE.Photo.slideid);
				PEOPLE.Quiz.updateslide(PEOPLE.Photo.slideid);
				if ((s % 2 !== 0) && (s <= len)) { 
					PEOPLE.Quiz.seeanswer(PEOPLE.Photo.slideid); 
				}
				PEOPLE.Photo.updatehash(PEOPLE.Photo.slideid);
				PEOPLE.Photo.fetchfiles(PEOPLE.Photo.slideid);
			}
			return false;
		});
	},
	seeanswer : function(id) { // transition to answer content, checking if user's answer was right or wrong
		var score = this.checkscore(id),
			result = '<div id="result" class="result">'; // build additional page content
		if (score == 'right') {
			result += '<h4 class="correct">Correct!</h4>';
		} else {
			result += '<h4 class="incorrect">Incorrect!</h4>';
		}
		result += '</div>';
		$('#scrollbar').prepend(result);
	},
	checkscore : function(id) { // check user's answer
		var q = $.inArray(PEOPLE.Photo.preid+id,gallery),
			correctid = PEOPLE.Photo.slides['slide_'+id].correctid,
			result = '',
			right = 0,
			wrong = 0,
			i,
			len = this.tally.length;
		if ($('#answer_'+id.replace(/\-1/,'')+'-'+correctid).is(':checked')) { // correct answer is checked
			this.tally[q] = 1;
			result = 'right';
		} else { // user either didn't checked anything or the checked answer is incorrect
			this.tally[q] = -1;
			result = 'wrong';
		}
		for (i = 0; ++i < len;) {
			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()
	},
	getfinalresults : function(id) { // on the last answer, show user final results
		var slide = $('#slide_'+id),
			result = this.right / (this.right + this.wrong),
			finalnode = PEOPLE.Photo.slides['slide_'+id].finalresult,
			header, caption, html, slideheight, resultsheight, top;
		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;
		}
		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>';
		slide.append(html); // append results html
		slideheight = slide.height(); // determine proper position for results overlay
		resultsheight = $('#finalresults').height();
		top = (slideheight/2) - (resultsheight/2) + 'px';
		PEOPLE.Photo.rightcolumn.find('div.txtcont:first').slideUp(); // kill content in #rightColumn
		$('#finalresults').css('top',top).show(); // show it already!
	}
};
$(function(){ // let's get this party started...
	if (PEOPLE.Photo.hardrefresh === false){ PEOPLE.Quiz.init(); }
});
