/*
	LA CLASE ControladorMapa se encarga de importar datos en formato XML y convertirlos en puntos en el mapa.
	También se encarga de ocultar y mostrar los datos dinámicamente, maneja eventos y muestra las ventanas de
	información correspondientes para cada punto, adquiriendo previamente los datos vía AJAX
*/

function controladorMapa() {
	var mapa;
	var posts;
	var denuncias;
	var poligonos;
}

controladorMapa.iniciarMapa = function(idContenedor, initZoom, initLat, initLng) {
	initZoom = typeof(initZoom) != 'undefined' ? initZoom : 5;
	initLat = typeof(initLat) != 'undefined' ? initLat : 0;
	initLng = typeof(initLng) != 'undefined' ? initLng : 0;
	
	if (GBrowserIsCompatible()) {
		this.mapa = new GMap2(document.getElementById(idContenedor));
		this.mapa.addMapType(G_PHYSICAL_MAP);
		this.mapa.setCenter(new GLatLng(initLat,initLng), initZoom);
		this.mapa.addControl(new GLargeMapControl());
		this.mapa.addControl(new GMapTypeControl());
		//this.mapa.enableScrollWheelZoom();
	}
}
controladorMapa.prototype.iniciarMapa = controladorMapa.iniciarMapa;



controladorMapa.cargarDatos = function(ruta, callBack) {
	new Ajax.Request(ruta, {
		onComplete: callBack
	})
}
controladorMapa.prototype.cargarDatos = controladorMapa.cargarDatos;



controladorMapa.mostrarPosts = function(listado) {
	var marcadorActual;
	
	for(var i=0;i<listado.length;i++) {
		var datos_punto = listado[i];
		
		//HACER EL ÍCONO
		var icono = new GIcon();
		icono.image = '/imagenes/iconos/bitacora.png';
		icono.shadow = '/imagenes/iconos/bitacora_sombra.png';
		icono.iconSize = new GSize(12, 20);
		icono.shadowSize = new GSize(22, 20);
		icono.iconAnchor = new GPoint(6, 20);
		icono.infoWindowAnchor = new GPoint(5, 4);
		
		var coordenadas = new GLatLng(datos_punto.lat, datos_punto.lng);		
		
		//CREAR EL OBJETO DE COORDENADAS
		var opciones = {'title': datos_punto.titulo};
		marcadorActual = this.crearMarcador(coordenadas, icono, opciones);
		marcadorActual.idPost = datos_punto.id;
		GEvent.addListener(marcadorActual, 'click', function() {
			controladorMapa.mostrarDetallePost(this);
		});
	}
}
controladorMapa.prototype.mostrarPosts = controladorMapa.mostrarPosts;



controladorMapa.mostrarDenuncias = function(listado) {
	/*
	CREA LOS PUNTOS DE DENUNCIAS Y LES ASIGNA ACCIONES A SUS EVENTOS
	1. obtener los datos del servidor via JSON
	2. hacer el loop para mostrar las denuncias
	*/
	
	var marcadorActual;
	
	for(var i=0;i<listado.length;i++) {
		var datos_punto = listado[i];
		
		//HACER EL ÍCONO
		var icono = new GIcon();
		icono.image = '/imagenes/iconos/'+datos_punto.categoria+'.png';
		icono.shadow = '/imagenes/iconos/sombra.png';
		icono.iconSize = new GSize(12, 20);
		icono.shadowSize = new GSize(22, 20);
		icono.iconAnchor = new GPoint(6, 20);
		icono.infoWindowAnchor = new GPoint(5, 4);
		
		var coordenadas = new GLatLng(datos_punto.lat, datos_punto.lng);
		
		//CREAR EL OBJETO DE COORDENADAS
		var opciones = {'title': datos_punto.titulo};
		marcadorActual = this.crearMarcador(coordenadas, icono, opciones);
		marcadorActual.idDenuncia = datos_punto.id;
		GEvent.addListener(marcadorActual, 'click', function() {
			controladorMapa.mostrarDetalleDenuncia(this);
		});
	}
}
controladorMapa.prototype.mostrarDenuncias = controladorMapa.mostrarDenuncias;



controladorMapa.mostrarDetalleDenuncia = function(marcadorDenuncia) {
	//VER SI LOS DATOS DE LA DENUNCIA FUERON CARGADOS
	//SI NO HAN SIDO CARGADOS, CARGARLOS
	
	if(typeof detalles_denuncias == 'undefined' || detalles_denuncias[marcadorDenuncia.idDenuncia]==null) {
		var callBack = function() {
			if(detalles_denuncias[marcadorDenuncia.idDenuncia]!=null) {
				controladorMapa.mostrarDetalleDenuncia(marcadorDenuncia);
			}
			else {
				controladorMapa.mostrarErrorDetalle(marcadorDenuncia);
			}
			
		};
		var urlDetalle = '/fotodenuncia/datosDetalleDenuncia/'+marcadorDenuncia.idDenuncia;
		controladorMapa.cargarDatos(urlDetalle, callBack);
	}
		
	//SI YA FUERON CARGADOS, MOSTRAR EL POPUP
	else {
		//CONSTRUIR LOS HTMLS PARA IMAGEN Y TEXTO
		var idDenuncia = marcadorDenuncia.idDenuncia;
		var htmlInfo = detalles_denuncias[idDenuncia]['info'];
		var htmlImagen = detalles_denuncias[idDenuncia]['imagen'];
		
		var tabs = [
			new GInfoWindowTab('INFO', htmlInfo),
			new GInfoWindowTab('FOTO', htmlImagen)
		];
		
		marcadorDenuncia.openInfoWindowTabsHtml(tabs);		
	}
}
controladorMapa.prototype.mostrarDetalleDenuncia = controladorMapa.mostrarDetalleDenuncia;




controladorMapa.mostrarDetallePost = function(marcadorPost) {
	//VER SI LOS DATOS DE LA DENUNCIA FUERON CARGADOS
	//SI NO HAN SIDO CARGADOS, CARGARLOS
	
	if(typeof detalles_posts == 'undefined' || detalles_posts[marcadorPost.idPost]==null) {
		var callBack = function() {
			if(detalles_posts[marcadorPost.idPost]!=null) {
				controladorMapa.mostrarDetallePost(marcadorPost);
			}
			else {
				controladorMapa.mostrarErrorDetalle(marcadorPost);
			}
		};
		var urlDetalle = '/bitacora/datosDetallePost/'+marcadorPost.idPost;
		controladorMapa.cargarDatos(urlDetalle, callBack);
	}
		
	//SI YA FUERON CARGADOS, MOSTRAR EL POPUP
	else {
		//CONSTRUIR LOS HTMLS PARA IMAGEN Y TEXTO
		var idPost = marcadorPost.idPost;
		var html = detalles_posts[idPost];
		
		marcadorPost.openInfoWindowHtml(html);		
	}
}
controladorMapa.prototype.mostrarDetallePost = controladorMapa.mostrarDetallePost;




controladorMapa.mostrarErrorDetalle = function(marcadorDenuncia) {
	var html = '<div class="error">Hubo un error al cargar los datos.</div>';
	marcadorDenuncia.openInfoWindowHtml(html);
}
controladorMapa.prototype.mostrarErrorDetalle = controladorMapa.mostrarErrorDetalle;



controladorMapa.mostrarPoligonos = function() {
	/*
	CREA LOS POLÍGONOS DE ACUERDO CON LOS DATOS RECIBIDOS Y LES ASIGNA SUS ACCIONES
	*/
	
	
}
controladorMapa.prototype.mostrarPoligonos = controladorMapa.mostrarPoligonos



controladorMapa.crearMarcador = function(coordenadas, icono, opcionesAdicionales) {
	var opciones = {
		icon: icono
	}
	
	if(typeof(opcionesAdicionales)!='undefined') {
		for(opcion in opcionesAdicionales) {
			opciones[opcion] = opcionesAdicionales[opcion];
		}
	}
	
	var marcador = new GMarker(coordenadas, opciones);
	this.mapa.addOverlay(marcador);
	return marcador;
}
controladorMapa.prototype.crearMarcador = controladorMapa.crearMarcador;