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

//<![CDATA[
var map;
var geocoder;
var address;
var direcciones;
var recorrido;
//ZOOM inicial
var globalZoom = 6;
var globalZoom = 15;
//DIRECCIONES
var st_direccion = "Barrio de Elexalde, 22, C.P. 48960 Galdakao, Vizcaya, Spain";
var st_direccion_html = "ANDRA MARI RESTAURANTE<br/>Barrio de Elexalde, 22, 48960<br/>Galdakao, Bizkaia<br/>Tel. (34) 94 456 00 05<br/>Fax. (34) 94 405 62 60";
var Llegardesde;

//var st_llegardesde_html = 
//		"<br/><br/>"+
//		"<form id=\"form_llegardesde\" action=\"\">"+
//		"<label for=\"llegardesde\"> Llegar Desde: </label><br/>"+
//		"<input id=\"llegardesde\" type=\"text\" name=\"llegardesde\" maxlength=\"200\" size=\"15\" "+
//		"onchange=\"Llegardesde=document.getElementById(\"llegardesde\").value;\" />&nbsp;"+
//		"<input type=\"button\" name=\"enviar\" value=\"OK\" onclick=\"map_accesos_general();\" />"+
//		"</form>";
var st_llegardesde_html = 
		'<br/><br/>'+
		'<form id=\"form_llegardesde\" action=\"\" onclick=\"return false;\">'+
		'<label for=\"llegardesde\"> Llegar Desde: </label><br/>'+
		'<input id=\"llegardesde\" type=\"text\" name=\"llegardesde\" maxlength=\"200\" size=\"25\" '+
		//'onchange=\"Llegardesde=document.getElementById(\'llegardesde\').value;\" '+
		'onchange=\"map_accesos_general();\" '+
		//'onblur=\"map_accesos_general();\" '+
		//'onfocus=\"map_accesos_general();\" '+
		'/>&nbsp;'+
		'<input type=\"button\" name=\"enviar\" value=\"OK\" onclick=\"map_accesos_general();\" />'+
		'</form>';
var Llegardesde;

//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, 15);
	}
}

//Crea marca standard
function createMarker(point, number) { 
	//var marker = new GMarker(point,G_DEFAULT_ICON);
	// Martin 10/09/08. Cambiamos el z-index del punto para que en IE7 quede por delante del punto final del recorrido 
	//cuando se hace una consulta de cómo llegar.
	var marker = new GMarker(point,{icon:G_DEFAULT_ICON, zIndexProcess:function(){return (1);} });
	GEvent.addListener(marker, "click", function() {
 		marker.openInfoWindowHtml(st_direccion_html+st_llegardesde_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, 6);
}
function map_ubicacion (){
	direcciones.clear();
	limpiaVentanas();
	resituaRecorrido(st_direccion, 16);
}
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);
	resituaRecorrido(st_direccion, 12);
}
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, 9);
}
function map_accesos_pam (){
	limpiaVentanas();
	//map.setZoom(14);
	direcciones.clear();
	direcciones.load("from:pamplona, navarra, spain to:"+st_direccion, {preserveViewport: true});
	resituaRecorrido(st_direccion, 8);
}
function map_accesos_vit (){
	limpiaVentanas();
	//map.setZoom(14);
	direcciones.clear();
	direcciones.load("from:vitoria, alava, spain to:"+st_direccion, {preserveViewport: true});
	resituaRecorrido(st_direccion, 9);
}
function map_accesos_bay (){
	limpiaVentanas();
	//map.setZoom(14);
	direcciones.clear();
	direcciones.load("from:bayonne, france to:"+st_direccion, {preserveViewport: true});
	resituaRecorrido(st_direccion, 8);
}
function map_accesos_general (){
	Llegardesde=document.getElementById('llegardesde').value;
	if (Llegardesde != '') {
		limpiaVentanas();
		direcciones.clear();
		direcciones.load("from:"+Llegardesde+" to:"+st_direccion, {preserveViewport: true});
		resituaRecorrido(st_direccion, 9);
	}
}

//function comproba_calcular_recorrido(e) {
//alert('En comproba_calcular_recorrido()');
//	keyAscii = String.charCodeAt(e.which);
//	if (e.target == Llegardesde)
//	{
//		if (keyAscii == 13) {
//			map_accesos_general();
//		}
//		else {
//			return false;
//		}
//	}
//	else
//	{
//		return true;
//	}
//}
//document.captureEvents(Event.KEYPRESS);
//document.onkeyup = comproba_calcular_recorrido();


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