/*****************************************************************************/
/*                                2010 Shio2e                                */
/*                      JAVASCRIPT MAPA GOOGLE MAPS API V3                   */
/*****************************************************************************/

// AMPLICACIONES API GOOGLE
google.maps.Map.prototype.clearMarkers = function() {
    for(var i=0; i < this.markers.length; i++)
        this.markers[i].setMap(null);

    this.markers = new Array();
};

// MAPA
// Class: mapaClass
var mapaClass = Class.create();

mapaClass.prototype = {
	// VARIABLES CONFIGURACION
	zoom_espanya : 5,
	zoom_x_defecto : 14,

	// VARIABLES INTERNAS
	obj : null,
	latitud : 0,
	longitud : 0,
	contenedor : '',
	marcadores : new Array(),
	marker_icono : new google.maps.MarkerImage(
		'/img/markers/image.png',
		new google.maps.Size(32,35),
		new google.maps.Point(0,0),
		new google.maps.Point(16,35)
	),
	marker_sombra : new google.maps.MarkerImage(
		'/img/markers/shadow.png',
		new google.maps.Size(50,35),
		new google.maps.Point(0,0),
		new google.maps.Point(17,35)
	),
	marker_shape : {
		coord: [25,1,27,2,27,3,28,4,28,5,28,6,28,7,28,8,28,9,28,10,28,11,28,12,28,13,28,14,28,15,27,16,26,17,24,18,21,19,19,20,19,21,18,22,18,23,18,24,18,25,18,26,17,27,17,28,17,29,17,30,17,31,16,32,16,33,16,34,14,34,14,33,14,32,13,31,13,30,13,29,13,28,13,27,12,26,12,25,12,24,12,23,12,22,11,21,11,20,9,19,6,18,5,17,4,16,3,15,2,14,2,13,2,12,2,11,2,10,2,9,2,8,2,7,2,6,2,5,3,4,3,3,4,2,5,1],
    	type: 'poly'
	},

	// FUNCIONES
	initialize : function () {
		this.obj 		= null;
		this.latitud 	= 0;
		this.longitud 	= 0;
		this.contenedor = '';
		this.marcadores = new Array();
	},

	nuevo : function (contenedor, latitud, longitud){
		this.contenedor = contenedor;
		latitud = typeof latitud == 'undefined' ? 0 : latitud;
		longitud = typeof longitud == 'undefined' ? 0 : longitud;

		this.mostrar(latitud, longitud);
	},

	mostrar : function (latitud, longitud) {
		this.latitud = latitud || this.latitud;
		this.longitud = longitud || this.longitud;

		var settings = {
			zoom: this.zoom_x_defecto,
			center: new google.maps.LatLng(this.latitud,this.longitud),
			mapTypeControl: true,
			mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
			navigationControl: true,
			navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		this.obj = new google.maps.Map($(this.contenedor), settings);
		this.marcadoresRecargar();
		this.marcadoresFiltrar();
	},

	centrar : function (latitud, longitud) {
		//this.obj.setCenter(new google.maps.LatLng(latitud, longitud));
		this.mostrar(latitud, longitud);
	},

	zoom : function (nuevo_zoom) {
		this.obj.setZoom(nuevo_zoom);
	},

	offsetRespectoContenedor : function (marcador) {
		var coord_mapa_geo = this.obj.getBounds();
		var coord_mapa_geo_ne = coord_mapa_geo.getNorthEast();
		var coord_mapa_geo_sw = coord_mapa_geo.getSouthWest();
		var _coord_mapa = {
			min_x : coord_mapa_geo_sw.lng(),
			max_x : coord_mapa_geo_ne.lng(),
			min_y : coord_mapa_geo_sw.lat(),
			max_y : coord_mapa_geo_ne.lat()
		}
		var coord_mapa = {
			nw_x : _coord_mapa.min_x,
			nw_y : _coord_mapa.max_y,
			long_x : Math.abs(_coord_mapa.max_x-_coord_mapa.min_x),
			long_y : Math.abs(_coord_mapa.max_y-_coord_mapa.min_y)
		}
		var coord_marker_geo = marcador.getPosition();
		var coord_marker = {
			x : coord_marker_geo.lng(),
			y : coord_marker_geo.lat()
		}

		var compensacion_marker = {
			x : 12,
			y : 35
		}

		return {
			top : Math.round(Math.abs(coord_mapa.nw_y-coord_marker.y)/coord_mapa.long_y*$(this.contenedor).getHeight())-compensacion_marker.y,
			left : Math.round(Math.abs(coord_mapa.nw_x-coord_marker.x)/coord_mapa.long_x*$(this.contenedor).getWidth())-compensacion_marker.x
		}
	},

	marcadorNuevo : function (id, latitud, longitud, nombre, extra) {
		var pos_array = this.marcadores.length;

		this.marcadores[pos_array] = {
			marcador		: this.marcadorGoogle(latitud, longitud, nombre),
			latitud 		: latitud,
			longitud 		: longitud,
			nombre 			: nombre,
			id_comercio 	: id,
			extra 			: extra,
			event_click 	: null,
			event_mouseover : null,
			event_mouseout 	: null
		};

		this.marcadores[pos_array].event_click = google.maps.event.addListener(this.marcadores[pos_array].marcador, 'click', function() {
			buscador.abrir('ficha', {id_comercio: id});
		});

		this.marcadores[pos_array].event_mouseover = google.maps.event.addListener(this.marcadores[pos_array].marcador, 'mouseover', function() {
			buscador.fichaTecnica.mostrarInfoGoogle(this, id, nombre, extra.categorias);
		});

		this.marcadores[pos_array].event_mouseout = google.maps.event.addListener(this.marcadores[pos_array].marcador, 'mouseout', function() {
			buscador.fichaTecnica.ocultarInfo();
		});
	},

	marcadorGoogle : function (latitud, longitud, nombre) {
		return new google.maps.Marker({
			position: new google.maps.LatLng(latitud, longitud),
			map: this.obj,
			icon: this.marker_icono,
			shadow: this.marker_sombra,
			shape: this.marker_shape
		});
	},

	marcadorRecargar : function (pos_array, id, latitud, longitud, nombre, extra) {
		// DESCONFIGURAR MARCADOR DE LA MEMORIA
        this.marcadores[pos_array].marcador.setMap(null);
        this.marcadores[pos_array].marcador = null;
		google.maps.event.addListener(this.marcadores[pos_array].event_click);
		google.maps.event.addListener(this.marcadores[pos_array].event_mouseover);
		google.maps.event.addListener(this.marcadores[pos_array].event_mouseout);

        // PONER NUEVA CONFIGURACION
		id 			= this.marcadores[pos_array].id_comercio;
		nombre 		= this.marcadores[pos_array].nombre;
		categorias 	= this.marcadores[pos_array].extra.categorias;

        this.marcadores[pos_array].marcador = this.marcadorGoogle(latitud, longitud, nombre);
		this.marcadores[pos_array].event_click = google.maps.event.addListener(this.marcadores[pos_array].marcador, 'click', function() {
			buscador.abrir('ficha', {id_comercio: id});
		});

		this.marcadores[pos_array].event_mouseover = google.maps.event.addListener(this.marcadores[pos_array].marcador, 'mouseover', function() {
			buscador.fichaTecnica.mostrarInfoGoogle(this, id, nombre, extra.categorias);
		});

		this.marcadores[pos_array].event_mouseout = google.maps.event.addListener(this.marcadores[pos_array].marcador, 'mouseout', function() {
			buscador.fichaTecnica.ocultarInfo();
		});
	},

	marcadoresEliminar : function () {
	    for(var i=0; i < this.marcadores.length; i++)
	        this.marcadores[i].marcador.setMap(null);
		this.marcadores = new Array();
	},

	marcadoresRecargar : function () {
	    for(var i=0; i < this.marcadores.length; i++)
			this.marcadorRecargar(i, this.marcadores[i].id, this.marcadores[i].latitud, this.marcadores[i].longitud, this.marcadores[i].nombre, this.marcadores[i].extra);
	},

	marcadoresFiltrar : function () {
		var provincia 	= buscador.filtroProvincia;
		var poblacion 	= buscador.filtroPoblacion;
		var barrio 		= buscador.filtroBarrio;
		var categorias 	= buscador.filtroCategorias;

		var mostrar, ok_filtro_cat;

		this.marcadores.each(function(obj, i) {
			mostrar = false;
			ok_filtro_cat = false;

			if (categorias.length > 0) {
				/*categorias.each(function(categoria, j) {
					if (obj.extra.id_categoria == categoria)
						ok_filtro_cat = true;
				}.bind(this));*/
				categorias.each(function(categoria, j) {
					obj.extra.categorias.each(function(id_categoria, k) {
						if (id_categoria == categoria)
							ok_filtro_cat = true;
					}.bind(this));
				}.bind(this));
			} else {
				ok_filtro_cat = true;
			}

			if (ok_filtro_cat)
				if (barrio != 0) {
					if (obj.extra.id_provincia == provincia && obj.extra.id_poblacion == poblacion && obj.extra.id_barrio == barrio) mostrar = true;
				} else if (poblacion != 0) {
					if (obj.extra.id_provincia == provincia && obj.extra.id_poblacion == poblacion) mostrar = true;
				} else if (provincia != 0) {
					if (obj.extra.id_provincia == provincia) mostrar = true;
				} else {
					mostrar = true;
				}

			obj.marcador.setVisible(mostrar);
		}.bind(this));
	}
};
