jQuery(document).ready(function($){  
	$('input:radio').checkbox();
	
	var q = 0;
	var questions;
	var answers;
	var user_answers = [];
	var correct_answers = [];
	var hash = getUrlVars();
	
	var section = hash['section'];
	var level = hash['level'];
	if ( hash['section'] ){
		$('.trivia-menu').show();
		if ( ($.browser.msie && $.browser.version.substr(0,1)<7) ) {
			// avoid IE bug
		}else{
			$('.trivia-menu .'+hash['section']).addClass('selected');
		}
	}
	if (typeof section == 'undefined' && typeof level == 'undefined') {
		$('#trivia-section').show();
		
	} else if (typeof section != 'undefined' && typeof level == 'undefined') {
		$('#trivia-level ul li#level-beginner a').attr('href', '?section='+section+'&level=beginner');
		$('#trivia-level ul li#level-expert a').attr('href', '?section='+section+'&level=expert');
		$('#trivia-level ul li#level-advanced a').attr('href', '?section='+section+'&level=advanced');
		$('#trivia-level').show();
	}
	
	$.post(s7uijk5,
		{
			get: 'questions',
			section: section,
			level: level
		},
		function(data) {
			if (data) {
				questions = data.questions;
				answers = data.answers;
				setQuestion();
				$('div#trivia').show();
				$('#trivia-next').show();
				$('#trivia-status').show();
			} else {
				alert('Error getting data');
			}
		}, "json"
	);
	$('input:radio').bind('check', function() {
		var selected_answer = $(this).val();
		user_answers[q] = selected_answer;
	});
	$('#trivia-next').click(function() { // Next
		if ((q+1) < questions.length && typeof user_answers[q] != 'undefined') {
			q++;
			setQuestion();
		} else {
			if (typeof user_answers[q] == 'undefined') {
				alert('Select an answer.');
			} else {
				setAnswers();
			}
			
		}
		return false;
	});
	
	function setAnswers() {
		$('#trivia-results').append('<ul style="margin-bottom:15px"></ul>');
		var wrong_answers = 0;
		for (var i in correct_answers) {
			if (user_answers[i] != correct_answers[i]) {
				wrong_answers++;
				$('#trivia-results ul').append('<li id="q-'+i+'"></li>');
				$('#trivia-results ul li#q-'+i).append('<div class="wrong-question"><img src="/trivia/wrong_answer.gif" alt="" /> Question '+(parseInt(i)+1)+':</div>');
				$('#trivia-results ul li#q-'+i).append('<div class="question">'+questions[i]['question']+'</div>');
				$('#trivia-results ul li#q-'+i).append('<div class="correct-answer">Correct Answer: <strong>'+answers[questions[i]['id']][correct_answers[i]]+'</strong></div>');
				$('#trivia-results ul li#q-'+i).append('<div class="complete-answer">'+questions[i]['complete_answer']+'</div>');
			}
		}
		$('#trivia-results h4').text('You got '+(correct_answers.length-wrong_answers)+' out of '+(correct_answers.length)+' questions correct!');
		$('#trivia-results').append('<a class="addthis_button" href="http://addthis.com/bookmark.php?v=250&amp;username=lhotp"><img src="http://s7.addthis.com/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a>');
		$('#trivia').hide();
		$('#trivia-next').hide();
		$('#trivia-status').hide();
		$('#trivia-results').show();
	}
	
	function setQuestion() {
		$.each($('input:radio'), function() { // Clear previous selected answer
			$('input:radio').removeAttr('checked');
		});
		var question_id = questions[q]['id'];
		var question_text = questions[q]['question'];
		correct_answers[q] = questions[q]['answer'];
		
		$('h2#trivia-question').text(question_text);
		
		if ((q+1) > 1) $('#trivia-status').text((q+1)+' Questions Of '+questions.length); // Singular
		else $('#trivia-status').text((q+1)+' Question Of '+questions.length); // Plural
		
		var j = 0;
		for (var i in answers[question_id]) {
			j++;
			$('ul li#answer-'+j+' input').val(i);
			$('ul li#answer-'+j+' cite').text(answers[question_id][i]);
		}
	}
});
//var _0xffd5 = ["\x74\x72\x69\x76\x69\x61\x2E\x70\x68\x70"];
var _0xffd5 = ["\x2F\x74\x72\x69\x76\x69\x61\x2F\x74\x72\x69\x76\x69\x61\x2E\x70\x68\x70"];
function getUrlVars() {
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for (var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}
var s7uijk5 = _0xffd5[0x0];