var current_question = 1;
var questions_per_page = 1;
var total_questions = 0;
var current_page = 1;

function nextQuestion(e) {
	if(questions_per_page != 0) return nextPage(e); // Multi question per page 
	var answered = false;
	
	jQuery("#question-" + current_question + " .answer").each(function(i) {
		if(this.checked) {
			answered = true;
			return true;
		}
	});
	if(!answered) {
		if(!confirm("You did not select any answer. Are you sure you want to continue?")) {
			e.preventDefault();
			e.stopPropagation();
			return false;
		}
	}
	
	jQuery("#question-" + current_question).hide();
	current_question++;
	jQuery("#question-" + current_question).show();
	
	if(total_questions <= current_question) {
		jQuery("#next-question").hide();
		jQuery("#action-button").show();
	}
}

function nextPage(e) {
	current_page++;
	showNextXQuestions();
}

function showNextXQuestions() {
	jQuery(".question").hide();
	
	var from_question = ((current_page - 1) * questions_per_page) + 1;
	var to_question = current_page * questions_per_page;
	for(var i = from_question; i <= to_question; i++) {
		jQuery("#question-" + i).show();
	}
	
	if(to_question >= total_questions) {
		jQuery("#action-button").show();
		jQuery("#next-question").hide();
	}
}

function init() {
	total_questions = jQuery(".question").length;
	if(questions_per_page > 1) {
		jQuery("#action-button").hide();
		jQuery("#next-question").show();
		showNextXQuestions();
	
	} else if(questions_per_page == 0) { //Single page mode.
		jQuery(".question").show();
		jQuery("#action-button").show();
		jQuery("#next-question").hide();
	
	} else {
		jQuery("#question-1").show();
	}
	
	jQuery("#next-question").click(nextQuestion);
}

jQuery(document).ready(init); 


function p() {
	if(arguments.length > 1) console.group();
	for(var i=0; i<arguments.length; i++) console.log(arguments[i]);
	if(arguments.length > 1) console.groupEnd();
}
