// Funcionalidad Principal
var mapaGoogle = {
	datos: {
		mapa: null,
		gdir: null,
		icono: null,
		anchoMiniDefecto: 130,
		altoMiniDefecto: 130,
		direcciones: new Array(),
		controles: new Array(),
		zoomDefecto: 15,
		urlIcono: "",
		urlSombraIcono: "",
		idMapa: "mapaGoogle",
		idDirecciones: "rutaANotaria"
	},
	
	funciones: {
		mapaCargado: function(){
			var auxi = document.getElementById(mapaGoogle.datos.idMapa);
			
			if(!auxi)
				return false;
				
			mapaGoogle.datos.mapa = new GMap2(auxi);
			mapaGoogle.datos.mapa.setCenter(new GLatLng(0, 0), mapaGoogle.datos.zoomDefecto);
			
			var controles = mapaGoogle.datos.controles;
			for(var i=0; i<controles.length; i++){
				mapaGoogle.datos.mapa.addControl(controles[i]); 
			}
			
			if(mapaGoogle.datos.urlIcono != ""){
				mapaGoogle.datos.icono = new GIcon();
				mapaGoogle.datos.icono.image = mapaGoogle.datos.urlIcono;
				mapaGoogle.datos.icono.shadow = mapaGoogle.datos.urlSombraIcono;
				mapaGoogle.datos.icono.iconSize = new GSize(12, 20);
				mapaGoogle.datos.icono.shadowSize = new GSize(22, 20);
				mapaGoogle.datos.icono.iconAnchor = new GPoint(6, 20);
				mapaGoogle.datos.icono.infoWindowAnchor = new GPoint(5, 1);
			}

			if(mapaGoogle.datos.idDirecciones != ""){
				mapaGoogle.datos.gdir = new GDirections(mapaGoogle.datos.mapa, document.getElementById(mapaGoogle.datos.idDirecciones));
				GEvent.addListener(mapaGoogle.datos.gdir, "load", mapaGoogle.funciones.direccionCargada);
				GEvent.addListener(mapaGoogle.datos.gdir, "error", mapaGoogle.funciones.manejadorErrores);
			}

			var direcciones = mapaGoogle.datos.direcciones;
			for(var i=0; i<direcciones.length; i++){
				var dir = direcciones[i];
				var point = new GLatLng(dir.lon, dir.lat);
				var marcador = mapaGoogle.funciones.nuevoMarcador(point, mapaGoogle.datos.icono, dir.texto, mapaGoogle.datos.markerDraggable);
				mapaGoogle.datos.mapa.addOverlay(marcador);
				if(i==0){
					mapaGoogle.datos.mapa.setCenter(point, (dir.zoom==null?mapaGoogle.datos.zoomDefecto:this.zoom)); 
					marcador.openInfoWindowHtml(dir.texto);
				}
			}

			return true;
		},
		
		calculaRuta: function(fromAddress, toAddress, locale){
			mapaGoogle.datos.gdir.load("from: " + fromAddress + " to: " + toAddress,{"locale": (locale==null?"es":locale)});
		},
		
		manejadorErrores: function(){
			if (mapaGoogle.datos.gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
				alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + mapaGoogle.datos.gdir.getStatus().code);
			else if (mapaGoogle.datos.gdir.getStatus().code == G_GEO_SERVER_ERROR)
				alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + mapaGoogle.datos.gdir.getStatus().code);
			else if (mapaGoogle.datos.gdir.getStatus().code == G_GEO_MISSING_QUERY)
				alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + mapaGoogle.datos.gdir.getStatus().code);
			//else if (mapaGoogle.datos.gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
			//	alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);
			else if (mapaGoogle.datos.gdir.getStatus().code == G_GEO_BAD_KEY)
				alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + mapaGoogle.datos.gdir.getStatus().code);
			else if (mapaGoogle.datos.gdir.getStatus().code == G_GEO_BAD_REQUEST)
				alert("A directions request could not be successfully parsed.\n Error code: " + mapaGoogle.datos.gdir.getStatus().code);
			else 
				alert("An unknown error occurred.");
		},
		
		direccionCargada: function(){
			window.location.href += '#direccion';
		},
		
		nuevoMarcador: function(point, icono, texto){
			var marker;
	
			if(icono==null)
				marker = new GMarker(point);
			else
				marker = new GMarker(point,icono);
				
			GEvent.addListener(marker, "click", function() {
				marker.openInfoWindowHtml(texto);
			});
			
			return marker;
		}		
	},
	
	nuevaDireccion: function(longitud, latitud, t, z){
		mapaGoogle.datos.direcciones.push({lon:longitud, lat:latitud, texto:t, zoom:z});
	},
		
	vistaMiniatura: function(an, al){
		var Tsize = new GSize((an==null?mapaGoogle.datos.anchoMiniDefecto:an), (al==null?mapaGoogle.datos.altoMiniDefecto:al));
		mapaGoogle.datos.controles.push(new GOverviewMapControl(Tsize));
	},
	
	controlMapaPequenio: function(){
		mapaGoogle.datos.controles.push(new GSmallMapControl());
	}, 
	
	controlMapaGrande: function(){
		mapaGoogle.datos.controles.push(new GLargeMapControl());
	}, 
	
	controlTipoMapa: function(){
		mapaGoogle.datos.controles.push(new GMapTypeControl());
	},
	
	setMapaSatelite: function(){
		mapaGoogle.datos.mapa.setMapType(G_SATELLITE_MAP);
	},
	
	setMapaHibrido: function(){
		mapaGoogle.datos.mapa.setMapType(G_HYBRID_MAP);
	},
	
	cambiaDireccion: function(indice, event){
		if(indice<0 || indice>=mapaGoogle.datos.direcciones.length)
			return false;
	
		var dir = mapaGoogle.datos.direcciones[indice];
		mapaGoogle.datos.mapa.setCenter(new GLatLng(dir.lon, dir.lat), (dir.zoom==null?mapaGoogle.datos.zoomDefecto:dir.zoom)); 
		if(event!=null)
			event.preventDefault();
	}
};


/* Datos Propios */
mapaGoogle.controlMapaPequenio();
mapaGoogle.nuevaDireccion(36.510688, -4.889324, "Notaría Motos<br/>Avda. Ricardo Soriano 19<br/>(29600) Marbella<br/>Málaga");


/* Eventos */
window.onload = mapaGoogle.funciones.mapaCargado;
window.onunload = GUnload;