var map;
var tempo = 60;
var nivel = 0;
var pontos = 0;
var vidas = 2;
var acertos = 0;
var respondidos = 0;
var currentMarker;
var iconQ, iconV, iconX;
var h = [0];
var interval;
var quiz_markers = [];
var quiz_marker_i = 0;

function temporizar(){
	tempo--;
	$('#tempo').text('00:' + (tempo < 10 ? '0' + tempo : tempo));
	if(tempo == 0){
		errou();
		clearInterval(interval);
	}
}

function reiniciarTempo(){
	if(nivel <= 3) tempo = 60;
	else if(nivel <= 7) tempo = 40;
	else tempo = 20;
}

function avancarNivel(){
	nivel++;
	$('#nivel').text(nivel);
	$('#nivel_fechado').text(nivel);
	acertos = 0;
	respondidos = 0;
	vidas++;
	$('#vidas').text(vidas);
	$('#vidas_fechado').text(vidas);
	limparPontos();
	plotarPontosQuizz();
	reiniciarTempo();
	if (nivel>1)
		$('#inicioJogo2').fadeIn("slow");
	
}

function perderVida(){
	vidas--;
	$('#vidas').text(vidas);
	$('#vidas_fechado').text(vidas);
	if(vidas == 0) gameOver();
	else reiniciarTempo();
}

function gameOver(){
	$('#inicioJogo2').hide();
	limparPontos();
	showQuizz();
	$("#tela2_quizz").hide();
	$("#tela3_quizz").show();
	$("#gameOver").fadeIn();
}

function hallMostrar(){	$("#hall_quizz").fadeIn();	}
function hallFechar(){	$("#hall_quizz").fadeOut();	}

function limparPontos(){
	GEvent.clearInstanceListeners(map);
	map.clearOverlays();
}
var qPontos = new Array();
function plotarPontosQuizz(){
	qPontos = [];
	
	var pontos = new Array();
	pontos[1] = [-19.922089,-43.944971];
	pontos[2] = [-19.987251,-44.005073];
	pontos[3] = [-19.859817,-43.979539];
	pontos[4] = [-19.936268,-43.935333];
	pontos[5] = [-19.916329,-43.918041];
	pontos[6] = [-19.933796,-43.970525];
	
	var ptoAleatorio = 1 + Math.floor(Math.random() * 6);
	
	map.setCenter(new GLatLng(pontos[ptoAleatorio][0],pontos[ptoAleatorio][1]), 12);
	
	$.getJSON('jogar.php', {nivel: nivel, 'h[]': h.join(',')}, function(data){
		if(data.noMore){
			gameOver();
		}
		else {
			var bounds = map.getBounds();
			var southWest = bounds.getSouthWest();
			var northEast = bounds.getNorthEast();
			var lngSpan = northEast.lng() - southWest.lng();
			var latSpan = northEast.lat() - southWest.lat();
			quiz_markers = [];
			quiz_marker_i = 0;

			$.each(data.pontos, function(){
				h[h.length] = this.id;
				var html = this.html;
				var latR;
				if (this.latitude) latR = this.latitude;
				else latR = southWest.lat() + latSpan * (Math.random() / 1.5 + 0.1);
				var lngR;
				if (this.longitude) lngR = this.longitude;
				else lngR = southWest.lng() + lngSpan * (Math.random() / 2 + 0.1);
				var marker = new GMarker(new GLatLng(latR,lngR), {icon: iconQ});
				quiz_markers[quiz_markers.length] = [marker, this.html];

				qPontos.push({mark:marker,latitude:latR,logintude:lngR,acesso:false,html:this.html});
					

				map.addOverlay(marker);
				GEvent.addListener(marker, 'click', function() {
					currentMarker = marker;
					marker.openInfoWindowHtml(html, {maxWidth: 400});
					clearInterval(interval);
					interval = setInterval('temporizar()', 1000);
					
				});
			});
		}
	});
}
var qPontos_i=0;
function gotoNextPoint(){
	$('#inicioJogo').hide();

	var marker = quiz_markers[quiz_marker_i][0];
	var html = quiz_markers[quiz_marker_i][1];
	quiz_marker_i++;
	if(quiz_marker_i >= quiz_markers.length)
		quiz_marker_i = 0;
	currentMarker = marker;

	marker.openInfoWindowHtml(html, {maxWidth: 400});
	clearInterval(interval);
	interval = setInterval('temporizar()', 1000);
}

function acertou(){
	for(var i = 0; i < quiz_markers.length; i++)
		if(quiz_markers[i][0] == currentMarker) {
			quiz_markers.splice(i, 1);
			break;
		}

	var lat = currentMarker.getLatLng().lat();
	var lng = currentMarker.getLatLng().lng();
	map.removeOverlay(currentMarker);
	map.addOverlay(new GMarker(new GLatLng(lat, lng), {icon: iconV}));
	pontos += nivel;
	$('#pontos').text(pontos);
	$('#pontos_fechado').text(pontos);
	acertos++;
	respondidos++;
	reiniciarTempo();
	if(acertos == nivel * 2 + 1)
		avancarNivel();
	else if(respondidos == nivel * 2 + 3)
		avancarNivel();
}

function errou(html){
	for(var i = 0; i < quiz_markers.length; i++)
		if(quiz_markers[i][0] == currentMarker) {
			quiz_markers.splice(i, 1);
			break;
		}

	var lat = currentMarker.getLatLng().lat();
	var lng = currentMarker.getLatLng().lng();
	map.removeOverlay(currentMarker);
	map.addOverlay(new GMarker(new GLatLng(lat, lng), {icon: iconX}));
	if(html) map.openInfoWindowHtml(new GLatLng(lat, lng), html, {maxWidth: 400});
	perderVida();
	respondidos++;
	if(respondidos == nivel * 2 + 3)
		avancarNivel();
}

function responder(p, a){
	clearInterval(interval);
	$.getJSON('responder.php', {pergunta: p, alternativa: a}, function(data){
		if(data.certo) acertou();
		else errou(data.html);
	});
}

function iniciarPartida(){
	
	hideQuizz();
	$("#tela1_quizz").hide();
	$("#tela3_quizz").hide();
	$("#tela2_quizz").show();
	
	$('#inicioJogo').fadeIn("slow");
	
	
	avancarNivel();
	$('#nexPoint').show();
	
}

function msgBottom(){
	setTimeout("$('#info_quiz').fadeIn('slow')", 100);
	setTimeout("$('#info_quiz').fadeOut('slow')", 10000);
}

function hideQuizz(){
	$("#painel_quizz").slideUp("slow");
	$("#painel_fechado").show();
}

function showQuizz(){
	$("#painel_fechado").hide();
	$("#painel_quizz").slideDown();
}
function reiniciar(){
	clearInterval(interval);
	
	tempo = 60;
	nivel = 0;
	pontos = 0;
	vidas = 2;
	acertos = 0;
	respondidos = 0;	
	$('#pontos').text(pontos);
	$('#pontos_fechado').text(pontos);
	
	$("#gameOver").fadeOut();
	
	limparPontos();
	iniciarPartida();

}
function sairJogo(){
	clearInterval(interval);
	
	tempo = 60;
	nivel = 0;
	pontos = 0;
	vidas = 2;
	acertos = 0;
	respondidos = 0;
	
	$('#pontos').text(pontos);
	$('#pontos_fechado').text(pontos);
	
	limparPontos();
	
	$("#regras_quizz").hide();
	$("#indica_quizz").hide();
	
	$("#tela1_quizz").show();
	$("#tela2_quizz").hide();
	$("#tela3_quizz").hide();
	$('#inicioJogo').hide();
	$('#inicioJogo2').hide();
	$("#painel_fechado").hide();
		
	$("#painel_quizz").hide();
	$("#exibition").fadeIn();
	$('#info_quiz').fadeOut();
	$('#gameOver').hide();
	plotarObras2008();
	
	
}

function abrirQuizz(){
	clearInterval(interval);
	$("#exibition").hide();
	$("#references").hide();
	$("#painel_quizz").show();
}
