var pagePos = [0];
var currPage = 1;

$(function() {
	$('#questions ol li.page').each(function(e) {
		var p = $(this).position();
		var t = (p.top + 500) * -1; // + 500 = #questions ol li.page margin-top
		pagePos.push(t);
	});
	
	var h = (pagePos[1] * -1) - 500;
	$('#questions').css({height: h});
	
	$('#questions ol li input').keydown(function(e) {
		if(e.keyCode == 9) return false; // if Tab is pressed
	}).keyup(function() {
		var v = parseInt($(this).val());
		if(v > 0 && v < 5)	{
			var p = $(this).parent().next();
			if(!p.hasClass('page')) p.find('input').focus();
		}
		else $(this).val('').focus();
	});
	
	$('#next_page').click(function() {
		if(currPage < 4) {
			$('#questions ol').animate({top: pagePos[currPage]});
			var h = ((pagePos[(currPage + 1)] - pagePos[currPage]) * -1) - 500;
			$('#questions').animate({height: h});
			currPage++;
		}
		return false;
	});
	
	$('#prev_page').click(function() {
		if(currPage > 1) {
			$('#questions ol').animate({top: pagePos[(currPage - 2)]});
			var h = ((pagePos[(currPage - 1)] - pagePos[currPage - 2]) * -1) - 500;
			$('#questions').animate({height: h});
			currPage--;
		}
		return false;
	});
	
	$('#calculate').click(function() {
		var c = 0; // megvalaszolt kerdesek szama
		var v = null;
		var p = 0, points = 0;
		
		$('#questions ol li').each(function(e) {
			v = parseInt($(this).find('input').val());
			if(v && v > 0 && v < 5) { // a valasznak 1 es 4 kozotti szamnak kell lennie
				c++;
				p = v;
				if($(this).hasClass('o')) {
					if(v == 1) p = 3;
					else p = 0;
				}
				points += p;
			}
		});
		if(c == 48) {
			$('#result div').hide();
			if(points > 141) $('#result-1').show();
			else if(points > 109) { $('#result-2').show(); }
			else if(points > 82) { $('#result-3').show(); }
			else $('#result-4').show();
			
			$('#the_result').text(points);
			$('#result').show();
		}
		else alert('Kerlek minden kerdesre valaszolj 1 es 4 kozotti szammal.');
		return false;
	});
});