//var RAIZ = "/corruptometro";
var RAIZ = "";
var numPreguntas = 10, respuestasArray = new Array(numPreguntas), preguntaActual, raton_x, raton_y, preguntasArray = new Array(numPreguntas);

function isDefined (variable) { return (typeof(window[variable]) != "undefined"); }
function resetArray (array) { for (c = 0; c < array.length; c++) { array[c] = ''; } return array; }

function selecProv (id, provincia) {
	var info = document.getElementById('infoProv');
	var y = raton_y;

	if (y > 525) { y = y - 30; }
	info.style.left = (raton_x + 10) + 'px';
	info.style.top = (y + 10) + 'px';
	info.innerHTML = provincia;	
}

function crearNodoTexto (etiqueta, texto) {
	var nodoTexto = document.createElement(etiqueta);
	nodoTexto.appendChild(document.createTextNode(texto));

	return nodoTexto;
}

function crearNodoLink (texto, src) {
	var a = document.createElement('a');
	a.appendChild(document.createTextNode(texto));
	a.setAttribute('href', src);

	return a;
}

function crearNodoLinkOnclick (texto, src) {
	var a = document.createElement('a');
	a.appendChild(document.createTextNode(texto));
	a.setAttribute('href', '#');
	a.setAttribute('onclick', src);

	return a;
}

function general_load (descargar) {
	(descargar) ? document.getElementById("cargando").style.display = "none" : document.getElementById("cargando").style.display = "";
}

function guardaRespuesta (respuesta) {
	respuestasArray[preguntaActual - 1] = respuesta;
//	muestraRespuestas();
	if (preguntaActual < numPreguntas) {
		document.getElementById('preguntaSiguiente').style.display = '';
		preguntaSiguiente();
	} else {
		//document.getElementById('terminar').style.display = '';
		enviaRespuestas();
	}
}

function infoProv_res () {
	var respuestaXML = this.req.responseXML;
	var lista = document.getElementById("selectMuni");
	var tituloProv = document.getElementById("tituloProv");
	var paso2 = document.getElementById('paso2');
	var numMunicipios = respuestaXML.firstChild.childNodes.length;
	var y = raton_y;
	tituloProv.innerHTML = this.provSelec;
	lista.options.length = 0;

	for (c = 0; c < numMunicipios; c++) {
		var id = respuestaXML.firstChild.childNodes[c].childNodes[0].firstChild.data;
		var municipio = respuestaXML.firstChild.childNodes[c].childNodes[1].firstChild.data;
		lista.options[c + 1] = new Option (decodeURIComponent (municipio), id);
	}
/*	
	if (numMunicipios > 1) {
		for (c = 0; c < numMunicipios; c++) {
			var id = respuestaXML.firstChild.childNodes[c].childNodes[0].firstChild.data;
			var municipio = respuestaXML.firstChild.childNodes[c].childNodes[1].firstChild.data;
			lista.options[c+1] = new Option(decodeURIComponent (municipio), id);
		}
	} else { 
		var id = respuestaXML.firstChild.firstChild.firstChild.firstChild.data;
		var municipio = respuestaXML.firstChild.firstChild.childNodes[1].firstChild.data;
		paso3 (id, municipio);
	}
*/	
	lista.onchange = function () { paso3 ("", ""); }
	
	if (y < 205) { y = y + 30; }
	paso2.style.display = "";
	paso2.style.left = (raton_x + 10) + 'px';
	paso2.style.top = (y - 50) + 'px';
	
	general_load(true);
}

function infoProv (IDprov, provincia) {
	obtienePreguntas();
	document.getElementById("paso2").style.display = "none";
	document.getElementById("paso3").style.display = "none";

	var url = RAIZ + '/BD/BD_municipiosProv.php?provincia=' + IDprov;
	var provincias = new net.CargadorContenidos (url, infoProv_res, general_load, '', 'GET');
	provincias.provSelec = provincia;
}

function paso3 (id, municipio) {
	var selectMuni = document.getElementById('selectMuni');
	var muniSeleccionado = selectMuni.selectedIndex;
	var selectMuniId = (id) ? id : selectMuni.options[muniSeleccionado].value;
	var selectMuniNom = (municipio) ? municipio : selectMuni.options[muniSeleccionado].text;
	var paso2 = document.getElementById('paso2');
	var nomMuni = document.getElementById('nombreMunicipio');
	var eligeMuni = document.getElementById('eligeMuni');
	var ranking = document.getElementById("rankings");

	nomMuni.innerHTML = selectMuniNom;
	eligeMuni.style.display = 'none';
	ranking.style.display = 'none';
	paso2.style.display = 'none';
	preguntas();
	nivelCorrupcion (selectMuniId);
	concejalesMunicipio (selectMuniId);
	concejalesMunicipioCorrupcion (selectMuniId);
	menuPaso3 ("");
}

function preguntas (numPreg) {
	var pregSel = 1;
	if (/^([0-9])*$/.test(numPreg)) {
		var pregSel = numPreg;
	} else {
		respuestasArray = resetArray (respuestasArray); }
	preguntaActual = parseInt (pregSel);
	var divPreguntas = document.getElementById("preguntas");
	var divNavPreg = document.getElementById("navPreguntas");
	var formResp = document.getElementById("radioRespuestas");
	var preguntaSiguiente = document.getElementById("preguntaSiguiente");
	var preguntaAnterior = document.getElementById("preguntaAnterior");
	var terminar = document.getElementById("terminar");
	var votosTerminados = document.getElementById("votosTerminados");

	document.getElementById('preguntas').style.display = '';
	document.getElementById('respuestas').style.display = '';
	divPreguntas.innerHTML = '';
	divNavPreg.style.display = '';
	votosTerminados.style.display = 'none';
	preguntaAnterior.style.display = 'none';
	preguntaSiguiente.style.display = 'none';
	terminar.style.display = 'none';
	formResp.reset();
	
	var pregunta = preguntaAleatoria (preguntaActual);
	divPreguntas.appendChild (crearNodoTexto ('div', pregunta));
	
	// compruebo si debo mostrar "pregunta anterior" siempre que la pregunta actual sea mayor que 1
	if (preguntaActual > 1) {
		preguntaAnterior.style.display = ''; }
	// siempre que 
	if (preguntaActual < numPreguntas && respuestasArray[preguntaActual - 1] != '') {
		preguntaSiguiente.style.display = ''; } 
	if (preguntaActual == numPreguntas && respuestasArray[preguntaActual - 1] != '') {
		terminar.style.display = ''; }

	switch (respuestasArray[preguntaActual - 1]) {
		case 'no' : formResp.respuesta[1].checked = true; break;
		case 'si' : formResp.respuesta[0].checked = true; break;
		case 'nose': formResp.respuesta[2].checked = true; break;
	}

	document.getElementById('preguntasQuedan').innerHTML = preguntaActual + ' / ' + numPreguntas;
	document.getElementById('paso3').style.display = '';
	general_load (true);
}

function preguntaSiguiente () {
	preguntas (preguntaActual + 1);
}

function preguntaAnterior () {
	preguntas (preguntaActual - 1);
}

function muestraRespuestas () {
	var listaRespuestas = '';
	var debugVisor = document.getElementById('debugVisor');
	
	for (c = 0; c < respuestasArray.length;	c++) {
		listaRespuestas = listaRespuestas + respuestasArray[c] + ',';
	}
	debugVisor.innerHTML = listaRespuestas;
}

function enviaRespuestas_res () {
	document.getElementById('navPreguntas').style.display = 'none';
	document.getElementById('preguntas').style.display = 'none';
	document.getElementById('respuestas').style.display = 'none';
	document.getElementById('votosTerminados').style.display = '';
	nivelCorrupcion (this.municipio);

	general_load(true);
}

function enviaRespuestas () {
	var selectMuni = document.getElementById('selectMuni');
	var muniSeleccionado = selectMuni.selectedIndex;
	var selectMuniId = selectMuni.options[muniSeleccionado].value;
	var respuestas = '';
	var debugVisor = document.getElementById('debugVisor');

	for (c = 0; c < respuestasArray.length;	c++) {
		respuestas = respuestas + respuestasArray[c] + ',';
	}

	var url = RAIZ + '/BD/BD_evaluaRespuestas.php?respuestas=' + respuestas + '&municipio=' + selectMuniId; 
	var respuestas = new net.CargadorContenidos (url, enviaRespuestas_res, general_load, '', 'GET');
	net.municipio = selectMuniId;
}

function nivelCorrupcion_res () {
	var respuestaXML = this.req.responseXML;
	var corrupcion = respuestaXML.firstChild.firstChild.firstChild.data;
	if (corrupcion < 0) { corrupcion = 0; }
	document.getElementById('barraCorrupcion').style.width = corrupcion + '%';
	document.getElementById('porcentajeCorrupcion').innerHTML = parseInt(corrupcion) + '%';

	general_load(true);
}

function nivelCorrupcion (idMuni) {
	var selectMuni = document.getElementById('selectMuni');
	var muniSeleccionado = selectMuni.selectedIndex;
	var selectMuniId = selectMuni.options[muniSeleccionado].value;
	var url = RAIZ + '/BD/BD_nivelCorrupcion.php?municipio=' + selectMuniId;
	var respuestas = new net.CargadorContenidos (url, nivelCorrupcion_res, general_load, '', 'GET');
}

function sigueRaton (e) {
	raton_x = e.clientX;
	raton_y = e.clientY;
//	document.getElementById('debugVisor').innerHTML = raton_y;
}

function obtienePreguntas_res () {
	var respuestaXML = this.req.responseXML;
	var preguntas = '';
	
	for (c = 0; c < respuestaXML.firstChild.childNodes.length; c++) {
		var id = respuestaXML.firstChild.childNodes[c].childNodes[0].firstChild.data;
		var pregunta = respuestaXML.firstChild.childNodes[c].childNodes[1].firstChild.data;
		preguntasArray[c] = decodeURIComponent (pregunta);
		preguntas = preguntas + c + ' ' + id + ' ' + preguntasArray[c] + '\n';
	}
}

function obtienePreguntas () {
	resetArray (preguntasArray);
	var url = RAIZ + '/BD/BD_listaPreguntasAleatorias.php';
	var respuestas = new net.CargadorContenidos (url, obtienePreguntas_res, general_load, '', 'GET');
}

function preguntaAleatoria (numPreg) {
	return preguntasArray[numPreg - 1];
}

function menuPaso3 (menu) {
	var corruptometro = document.getElementById("contenidoPaso");
	var concejales = document.getElementById("contenidoPasoConcejales");
	var ranking = document.getElementById("contenidoRanking");
	var pes1 = document.getElementById("pestania1");
	var pes2 = document.getElementById("pestania2");
	var pes3 = document.getElementById("pestania3");

	if (menu == 1) {
		ranking.style.display = "none";
		corruptometro.style.display = "";
		concejales.style.display = "none";
		pes3.style.backgroundPosition = "";
		pes1.style.backgroundPosition = "bottom";
		pes2.style.backgroundPosition = "";
	} else if (menu == 2) {
		ranking.style.display = "none";
		corruptometro.style.display = "none";
		concejales.style.display = "";
		pes3.style.backgroundPosition = "";
		pes1.style.backgroundPosition = "";
		pes2.style.backgroundPosition = "bottom";
	} else {
		ranking.style.display = "";
		corruptometro.style.display = "none";
		concejales.style.display = "none";
		pes3.style.backgroundPosition = "bottom";
		pes1.style.backgroundPosition = "";
		pes2.style.backgroundPosition = "";
	}
}

function concejalesMunicipio (municipio) {
	var url = RAIZ + '/BD/BD_listaConcejalesMunicipio.php?municipio=' + municipio;
	var respuestas = new net.CargadorContenidos (url, concejalesMunicipio_res, general_load, '', 'GET');
}

function concejalesMunicipio_res () {
	var respuesta = this.req.responseXML;
	var listaConcejales = document.getElementById("listadoConcejales");
	listaConcejales.innerHTML = "";
	
	for (c = 0; c < respuesta.firstChild.childNodes.length; c++) {
		var id = respuesta.firstChild.childNodes[c].childNodes[0].firstChild.data;
		var nombre = respuesta.firstChild.childNodes[c].childNodes[1].firstChild.data;
		var corruptador = document.createElement("select");
		var concejal = document.createElement ("div");
		for (o = 0; o < 11; o++) {
			corruptador.options[o + 1] = new Option (o, o);
		}
		corruptador.setAttribute ("style", "margin-right:10px");
		corruptador.setAttribute ("id", "select" + id);
		corruptador.onchange = function () { 
			var idConcejal = this.getAttribute("id").split ("select");
			puntuaConcejal (idConcejal[1]);
		}
		concejal.appendChild (corruptador);
		concejal.appendChild (document.createTextNode (decodeURIComponent (nombre)));
		listaConcejales.appendChild (concejal);
	}

	general_load(true);
}

function puntuaConcejal (id) {
	var selectConcejal = document.getElementById ("select" + id);
	var puntuacion = selectConcejal.selectedIndex - 1;
	if (puntuacion >= 0) {
		var url = RAIZ + "/BD/BD_puntuaConcejal.php?id=" + id + "&puntuacion=" + puntuacion;
		var respuestas = new net.CargadorContenidos (url, puntuaConcejal_res, general_load, "", "GET");
		selectConcejal.setAttribute ("disabled", "disabled");
	}
}

function puntuaConcejal_res () {
	var respuesta = this.req.responseXML;
	
	general_load(true);
}


function concejalesMunicipioCorrupcion (municipio) {
	var url = RAIZ + '/BD/BD_listaConcejalesMunicipioCorrupcion.php?municipio=' + municipio;
	var respuesta = new net.CargadorContenidos (url, concejalesMunicipioCorrupcion_res, general_load, '', 'GET');
}

function concejalesMunicipioCorrupcion_res () {
	var respuesta = this.req.responseXML;
	var listaConcejales = document.getElementById("rankingConcejales");
	var ulConcejales = document.createElement("ol");
	listaConcejales.innerHTML = "";
	
	for (c = 0; c < respuesta.firstChild.childNodes.length; c++) {
		var id = respuesta.firstChild.childNodes[c].childNodes[0].firstChild.data;
		var nombre = respuesta.firstChild.childNodes[c].childNodes[1].firstChild.data;
		var corrupcion = respuesta.firstChild.childNodes[c].childNodes[2].firstChild.value;
		var concejal = crearNodoTexto ("li", decodeURIComponent (nombre));
		ulConcejales.appendChild (concejal);
	}
	listaConcejales.appendChild (ulConcejales);

	general_load(true);
}

function listaConcejalesCorrupcion () {
	var url = RAIZ + "/BD/BD_listaConcejalesCorrupcion.php";
	var respuesta = new net.CargadorContenidos (url, listaConcejalesCorrupcion_res, general_load, '', 'GET');
}

function listaConcejalesCorrupcion_res () {
	var respuesta = this.req.responseXML;
	var listaConcejales = document.getElementById("rankingGenConcejales");
	var olConcejalesCorruptos = document.createElement("ol");
	var olConcejalesHonrados = document.createElement("ol");
	listaConcejales.innerHTML = "";
	
	for (c = 0; c < respuesta.firstChild.childNodes.length; c++) {
		var id = respuesta.firstChild.childNodes[c].childNodes[0].firstChild.data;
		var nombre = respuesta.firstChild.childNodes[c].childNodes[1].firstChild.data;
		var votos = respuesta.firstChild.childNodes[c].childNodes[2].firstChild.data;
		var municipio = respuesta.firstChild.childNodes[c].childNodes[3].firstChild.data;
		var concejal = crearNodoTexto ("li", decodeURIComponent (nombre));
		var nodoVotos = crearNodoTexto ("span", " " + votos);
		nodoVotos.style.fontWeight = "bold";
		concejal.appendChild (nodoVotos);
		concejal.appendChild (crearNodoTexto ("span", " (" + decodeURIComponent (municipio) + ")"));
		if (c < 10) { olConcejalesCorruptos.appendChild (concejal); }
		else { olConcejalesHonrados.appendChild (concejal); }
	}
//	listaConcejales.appendChild (crearNodoTexto ("div", "Los 10 concejales mas corruptos de Espaņa!")); 
	listaConcejales.appendChild (olConcejalesCorruptos);
//	listaConcejales.appendChild (crearNodoTexto ("div", "Los 10 concejales mas honrados de Espaņa!")); 
	listaConcejales.appendChild (olConcejalesHonrados);

	general_load(true);
}

function listaMunicipiosCorrupcion () {
	var url = RAIZ + "/BD/BD_listaMunicipiosCorrupcion.php";
	var respuesta = new net.CargadorContenidos (url, listaMunicipiosCorrupcion_res, general_load, '', 'GET');
}

function listaMunicipiosCorrupcion_res () {
	var respuesta = this.req.responseXML;
	var listaMunicipios = document.getElementById("rankingGenMunicipios");
	var olMunicipiosCorruptos = document.createElement("ol");
	var olMunicipiosHonrados = document.createElement("ol");
	listaMunicipios.innerHTML = "";
	
	for (c = 0; c < respuesta.firstChild.childNodes.length; c++) {
		var id = respuesta.firstChild.childNodes[c].childNodes[0].firstChild.data;
		var nombre = respuesta.firstChild.childNodes[c].childNodes[1].firstChild.data;
		var corrupcion = respuesta.firstChild.childNodes[c].childNodes[2].firstChild.value;
		var concejal = crearNodoTexto ("li", decodeURIComponent (nombre));
		if (c < 10) { olMunicipiosCorruptos.appendChild (concejal); }
		else { olMunicipiosHonrados.appendChild (concejal); }
	}
//	listaConcejales.appendChild (crearNodoTexto ("div", "Los 10 municipios mas corruptos de Espaņa!")); 
	listaMunicipios.appendChild (olMunicipiosCorruptos);
//	listaConcejales.appendChild (crearNodoTexto ("div", "Los 10 municipios mas honrados de Espaņa!")); 
	listaMunicipios.appendChild (olMunicipiosHonrados);

	general_load(true);
}

function menuRanking (pes) {
	var concejales = document.getElementById("rankingGenConcejales");
	var municipios = document.getElementById("rankingGenMunicipios");
	var pesConce = document.getElementById("pesRankConcejales");
	var pesMunis = document.getElementById("pesRankMunicipios");
	if (pes == "concejales") {
		concejales.style.display = "";
		pesConce.style.backgroundPosition = "bottom";
		municipios.style.display = "none";
		pesMunis.style.backgroundPosition = "";
	} else {
		concejales.style.display = "none";
		pesConce.style.backgroundPosition = "";
		municipios.style.display = "";
		pesMunis.style.backgroundPosition = "bottom";
	}
}

window.onload = function () {
	listaConcejalesCorrupcion();
	listaMunicipiosCorrupcion();
	menuRanking ('concejales');
}
