//global variables
var global_rating = null;//1 2 or 3 , tells us how far in the quiz the person wants to go
var global_quiz_id = null;
var global_score = '0';
var global_lang = 'en';
var right = null;
var wrong = null;

var question_total = '0';//total number of questions
var question_position = '0';//question being answered
var wrong_answers = new Array();
var wrong_answer_count = '0';//index for wrong answer array


	/**
	 * Start quiz
	 * 
	 * @param {Object} rating
	 */
	function startQuiz(quiz_id, rating, lang) {
		jQuery('#quiz_container').hide("");
		
		//change class of id class quiz_container to quiz_step_questions
		jQuery('#quiz_container').removeClass('quiz_step_select_level');
		jQuery('#quiz_container').addClass('quiz_step_questions');
		
		//set the rating so it can be accessed anywhere
		global_rating = rating;

		//set the quiz id		
		global_quiz_id = quiz_id;
		
		global_lang = lang;
		
		switch (global_lang)
		{
			case 'en':
			default:
				right = 'right';
				wrong = 'wrong';
				break;
			case 'es':
				right = 'correcto';
				wrong = 'incorrecto';
				break;
		}
		

		//count the number of questions
		jQuery.get('quizzes.php/quiz/getQuestionCount/'+quiz_id+'/'+rating, 
			function(data) {
				question_total = data;
				getNextQuestion();
		});
	}
	
	/**
	 * Process answer
	 * 
	 * Check correct number of answers were selected
	 * 
	 * @param question_id id of question to be processed
	 * @param answer_ids all possible answers to the question
	 * @param correct_answer_id id of correct answer
	 */
	function processAnswer(question_id, answer_ids, correct_answer_id) {

		//disable checkboxes
		for(x in answer_ids) {
			jQuery('#answer_'+answer_ids[x]).attr("disabled","disabled");
		} 
				
		var number_answers_marked = '0';//total number the user selected
		var answers_marked = new Array();//answers selected by the user will go here
		var score = '0';
		
		//for each answer, check if it was selected, if it was mark the answer id and add 1 to the total selected answers
		for(x in answer_ids) {
			if(jQuery('#answer_'+answer_ids[x]).attr('checked') == true){
				answers_marked[number_answers_marked] = answer_ids[x];
				number_answers_marked++;
			}			
		} 
							
		//if the correct answer id can be found in the answers_marked, mark one as correct, otherwise add question id to error list
		if(answers_marked['0'] == correct_answer_id) {
			//add correct class to correct answer
			jQuery('#label_answer_'+correct_answer_id).addClass('correct').append('<span>'+right+'</span>');
			global_score++;
			jQuery('#current_score').empty().append(global_score);
		}  else {
			jQuery('#label_answer_'+answers_marked['0']).addClass('incorrect').append('<span>'+wrong+'</span>');
			jQuery('#label_answer_'+correct_answer_id).addClass('correct').append('<span>'+right+'</span>');
			wrong_answers[wrong_answer_count] = question_id;
			wrong_answer_count++;
		}
		jQuery('#answer_description').removeClass('hidden');
		//swap the buttons
		jQuery('#button_next_question').removeClass('hidden');		


	}
	
	
	/**
	 * Get next question
	 * 
	 * Gets and displays the next question
	 */
	function getNextQuestion() {

		//hide question and display a wheel
		updateQuiz("<img src='/applications/quiz_app/img/indicator_medium.gif' class='time_wheel' />");
						
		if (question_position <= (question_total - 1)) {
			//get question code
			jQuery.get('quizzes.php/quiz/getQuestion/'+global_quiz_id+'/'+global_rating+'/'+question_position+'/'+global_score, 
				function(data) {
					updateQuiz(data);
			});	

		} else {

			finishQuiz();

		}

		question_position++;//increase question position

	}
	
	/**
	 * Finish quiz
	 * 
	 * Sort out score and final page
	 */
	function finishQuiz() {
		jQuery('#quiz_container').hide();
		//remove class question_image to the id quiz_container
		jQuery('#quiz_container').removeClass('question_image');
		
		//change class of id class quiz_container to quiz_step_questions
		jQuery('#quiz_container').removeClass('quiz_step_questions');
		jQuery('#quiz_container').addClass('quiz_step_finished');		
				
		var question_ids = "-";
		
		//for each wrong answer, get the link
		for(x in wrong_answers) {
			question_ids += wrong_answers[x]+'-';
		}
		
		jQuery.get('quizzes.php/quiz/createLastPage/'+question_ids+'/'+global_quiz_id+'/'+global_rating+'/'+global_score+'/'+question_total, 			
			function(data) {
				updateQuiz(data);
			});	
	}
	
	/**
	 * Update quiz
	 * 
	 * Updates the quiz area in the page with given code (questions or final page)
	 * 
	 * @param {Object} code
	 */
	function updateQuiz(code) {
		jQuery('#quiz_container').hide("");
		jQuery('#quiz_container').empty().append(code);
		jQuery('#quiz_container').show("slow");
	}
	