var quiz = {
	// array to save types (typed quiz)
	types:[],
	questions:[],
	message: '',
	// set result message
	set_hi_result_msg: function(txt) {
		this.hi_result_message = txt;
	}, 
	set_lo_result_msg: function(txt) {
		this.lo_result_message = txt;
	},
   
	// add question, set correct answer index one number bellow to match array index
	add_question:function(obj){
		obj.correct_answer--;
		this.questions.push(obj);
	},
	// get question html
	load_question:function(obj,n){
		// set even/odd class to add blue/white color to background
		var cl = 'odd';
		if(n%2 == 0) cl = 'even';
		var explanation = '';
		if(obj.explanation_copy) explanation = '<p class="explanation">'+obj.explanation_copy+'</p>';
		// set question html
		var htm = '<fieldset class="'+cl+'"> \
					<legend>Question ' + (n+1) + '</legend> \
					<p class="question clearfix"><span>' + (n+1) + '</span> <strong>' +obj.question_copy+ '</strong></p> \
					<div class="answers">';
		// loop into answers
		for(var i=0,len=obj.answers.length;i<len;i++) {
			cl = '';
			if(i == 0) cl = ' first';
			var answer = obj.answers[i];
			// if typed quiz, use answer obj copy
			if(this.types.length > 0) answer = obj.answers[i].copy;
			htm += '<div class="answer'+cl+'"> \
					<input type="radio" name="question-' + n + '" id="answer-' + n + '-' + i + '" /> \
					<label for="answer-' + n + '-' + i + '">' + answer + '</label> \
					</div>';
		}
		htm += '</div>'+ explanation +'\
				</fieldset>';
		return htm;
	},
	// load score page
	score:function(n){
		var container = $('#quiz_content');
		var max = this.questions.length;
		var low = false;
		// high score, >= 50%
		// low score, < 50%
		if(n<(max/2)) low = true;
		if(low) {
			// set low score classes and txt result
			$('#main').addClass('low_score');
			$('h2#title').show().html('Oops, You Got A Low Score').removeClass().addClass('title_low_score');
			$('p#result').html('You got '+n+' out of '+max+' correct!' );
			container.html('<div class="message">\
				<p> '+ this.lo_result_message + ' </p>\
						<a class="button_retake" href="javascript:location.reload();">Re-take the quiz</a></div>');
		} else {
			// set high score classes and txt result
			$('#main').addClass('high_score');
			$('h2#title').show().html('Good Job! You Got A High Score!').removeClass().addClass('title_high_score');
			$('p#result').html('<strong>You got '+n+' out of '+max+' correct!<br /></strong>' + this.hi_result_message);
			$("div.quiz a.send_friend").show();
		}
		// change hash to scroll up page
		//location.hash = '#main';   
		 window.scrollTo(0,0);
	},
	
	// load typed quiz score
	score_type: function(winner) {
		$('#main').addClass('low_score');
		$('h2#title').html(winner.title).removeClass().addClass('title_high_score');
		$('p#result').html(winner.title);
		$('#quiz_content').html('<div class="message">'+winner.result+'</div>');
		//location.hash = '#main';
		window.scrollTo(0,0); 
	},
	
	// add quiz type
	add_type:function(type) {
		// add score to each obj
		type.score = 0;
		this.types.push(type);
	},
	
	// bind form submit event
	set_submit:function() {
		$('#quiz input#submit').click(function() {
			// check how many answered questions
			var answered = $('#quiz div.answer_on').length;
			if(answered < quiz.questions.length){
				$('#alert_box').show();
				setTimeout(function() {location.hash = '#alert_box';},10);
				return false;
			}
			$('#alert_box').hide();
			// unbind answers click event
			$('#quiz div.answer').unbind('click');
			// if typed search
			if(quiz.types.length > 0) {
					
					// for each selected answer
					$('#quiz div.answer_on').each(function(){
						// get id
						var id = $('input',this).attr('id');
						if(id) {
							id = id.split('answer-').join('');
							// get question number
							var question_number = id.split('-')[0];
							// get answer given
							var answer_given = id.split('-')[1];
							// get type of given answer
							var type = quiz.questions[question_number].answers[answer_given].type;
							type--;
							// add point to type score
							quiz.types[type].score++;
						}
					});
					
					// get winner type
					var winner = quiz.types[0];
					for(var i=0;i<quiz.types.length;i++) {
						if(quiz.types[i].score > winner.score) winner = quiz.types[i];
					}
				
					quiz.score_type(winner);
					return false;
				
				} else {
			
					var correct = 0;
					// show all correct answers
					for(var i=0,len=quiz.questions.length;i<len;i++) {
						var answer_correct = quiz.questions[i].correct_answer;
						$('#answer-'+i+'-'+answer_correct).parent().addClass('answer_correct');
					}
					// for each selected answer
					$('#quiz div.answer_on').each(function(){
						// get id
						var id = $('input',this).attr('id');
						if(id) {
							id = id.split('answer-').join('');
							// get question number
							var question_number = id.split('-')[0];
							// get answer given
							var answer_given = id.split('-')[1];
							var answer_correct = quiz.questions[question_number].correct_answer;
							// compare with correct answer
							if(answer_correct != answer_given) {
								// set wrong class
							    // line = 154
								$(this).addClass('answer_wrong');
							} else {
								// else, count as correct answer
								correct++;
							}
							// remove select answer class
							$(this).removeClass('answer_on');
						}
					});
					// set all answers to off
					$('#quiz div.answer').addClass('answer_off');
					// show quiz score
					quiz.score(correct);
					return false;
			
			}
		});
	},
	// load quiz
	load:function(){
		var container = $('#quiz_content');
		var htm = '';
		// get each question html
		for(var i=0,len=this.questions.length;i<len;i++) {
			var question = this.questions[i];
			htm+= this.load_question(question,i);
		}
		// append questions html
		container.html(htm);
		// set rollover event
		$('#quiz div.answer').hover(
		function(){
			$('label',this).addClass('on');
		},
		function(){
			$('label',this).removeClass('on');
		}
		);
		// bind answers click event
		$('#quiz div.answer').click(function(){
			var obj = $(this);
			var parent = obj.parent();
			// remove all selected answers from parent fieldset
			$('div.answer',parent).removeClass('answer_on');
			// select answer
			obj.addClass('answer_on');
			$('input[type="radio"]',obj).attr("checked","checked");
		});
		this.set_submit();
	}
}

$(document).ready(function(){
	$('body').addClass('has-js');
	quiz.load();
});