//
//  googleMaps
//
//  Created by Iban Moreno on 2007-08-31.
//  Copyright (c) 2007 __TheMovie__. All rights reserved.
//

var map;
var geocoder;
var address;
var direcciones;
var recorrido;
//ZOOM inicial
var globalZoom = 14;
//<![CDATA[

//Crea mapa
function load() {
  //si el navegador es compatible
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map"));
	recorrido = document.getElementById("recorrido");
	direcciones = new GDirections(map,recorrido);
	//controles
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	map.addControl(new GOverviewMapControl());
	resituaMapa(st_direccion, 14);
	}
}

//Crea marca standard
function createMarker(point, number) { 
	var marker = new GMarker(point,G_DEFAULT_ICON);
	GEvent.addListener(marker, "click", function() {
 		marker.openInfoWindowHtml(st_direccion_html);	
  	});
return marker;
}

//Direccion a coordenadas
function resituaMapa(direccion, zoom)
{
	globalZoom = zoom;
	geocoder = new GClientGeocoder();
	address = direccion;
	geocoder.getLocations(address, addToMap);
}
GEvent.addListener(map, 'click', function mapClick(marker,point){
	if(!marker)
	{
		oLat = document.getElementById("lat");
		oLat.value = point.y;
		oLon = document.getElementById("lon");
		oLon.value = point.x;
		oZoom = document.getElementById("zoom");
	}
});
function addToMap(response)
{
	// Retrieve the object
	place = response.Placemark[0];
	point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
	map.setCenter(point, globalZoom);
	map.addOverlay(createMarker(point, 1));
}
function addToMapR(response)
{
	// Retrieve the object
	place = response.Placemark[0];
	point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
	map.setCenter(point, globalZoom);
	map.addOverlay(createMarker(point, 1));
}
//Resitua recorrido
function resituaRecorrido (direccion, zoom){
	globalZoom = zoom;
	geocoder = new GClientGeocoder();
	address = direccion;
	geocoder.getLocations(address, addToMapR);
}
//SITUACION ACCESOS UBICACION
function map_situacion (){
	direcciones.clear();
	limpiaVentanas();
	resituaMapa(st_direccion, 14);
}
function map_ubicacion (){
	direcciones.clear();
	limpiaVentanas();
	resituaRecorrido(st_direccion, 16);
}
//Acceso por el codigo postal
function map_accesos_cp()
{
	var codigo = document.getElementById("codigo");
	limpiaVentanas();
	direcciones.clear();
	direcciones.load("from: "+codigo.value+", Spain to:"+st_direccion, {preserveViewport: true});
	
	//resituaRecorrido(st_direccion, 14);
}
function map_accesos_bil (){
	limpiaVentanas();
	//map.setZoom(14);
	direcciones.clear();
	direcciones.load("from:bilbao, vizcaya, spain to:"+st_direccion, {preserveViewport: true});
	resituaRecorrido(st_direccion, 14);
}
function map_accesos_ss (){
	limpiaVentanas();
	//map.setZoom(14);
	direcciones.clear();
	direcciones.load("from:san sebastian, guipuzcoa, spain to:"+st_direccion, {preserveViewport: true});
	resituaRecorrido(st_direccion, 14);
}
function map_accesos_pam (){
	limpiaVentanas();
	//map.setZoom(14);
	direcciones.clear();
	direcciones.load("from:pamplona, navarra, spain to:"+st_direccion, {preserveViewport: true});
	resituaMapa(st_direccion, 14);
}
function map_accesos_vit (){
	limpiaVentanas();
	//map.setZoom(14);
	direcciones.clear();
	direcciones.load("from:vitoria, alava, spain to:"+st_direccion, {preserveViewport: true});
	resituaMapa(st_direccion, 14);
}

function limpiaVentanas (){
	map.clearOverlays();
}
//
//]]>
