// JavaScript Document

function calculardistancia(fromAddress,toAddress,locale,capa){
	var distancia = new GDirections()	
	distancia.load("from: " + fromAddress + " to: " + toAddress,
	{ "locale": locale });
	GEvent.addListener(distancia, "load", function(){  
			var distkm = distancia.getDistance().meters/1000;								   	
			document.getElementById(capa).innerHTML = "a "+Math.round(distkm)+" Km";	
	}); 
}

function cargar_mapa(lat,lng){
	if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("mapagoogle"));
		map.addControl(new GLargeMapControl());
		map.addControl(new GOverviewMapControl());
		map.addControl(new GMapTypeControl());
		var point = new GLatLng(lat,lng);
		map.setCenter(point, 9);
		var marker = new GMarker(point);
		map.addOverlay(marker);
		gdir = new GDirections(map, document.getElementById("ruta"));
		GEvent.addListener(gdir, "load", onGDirectionsLoad);
		GEvent.addListener(gdir, "error", handleErrors);
	}
}

function cargar_mapa_pueblo(lat,lng,html){
	if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("mapagoogle"));
		map.addControl(new GLargeMapControl());
		map.addControl(new GOverviewMapControl());
		map.addControl(new GMapTypeControl());
		var point = new GLatLng(lat,lng);
		map.setCenter(point, 9);
		var marker = new GMarker(point);
		map.openInfoWindowHtml(map.getCenter(),html);
	}
}


function setDirections(fromAddress, toAddress, locale) {
	gdir.load("from: " + fromAddress + " to: " + toAddress,
	{ "locale": locale });
}

function handleErrors(){
		if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
		alert("Dirección no disponible.\nError code: " + gdir.getStatus().code);
		else if (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: " + gdir.getStatus().code); 
		else if (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: " + gdir.getStatus().code); 
		else if (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: " + gdir.getStatus().code);
		else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
		alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code); 
		else alert("An unknown error occurred."); 
}
		
function onGDirectionsLoad(){ 
}


function iniciar_mapa(lat,lng,zoom){
	if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("busquedamapa"));
		map.addControl(new GLargeMapControl());
		map.addControl(new GOverviewMapControl());
		var point = new GLatLng(lat,lng);
		map.setCenter(point, zoom);
	}
}

function showPoint(lat,lng,html){
	var point = new GLatLng(lat,lng);
	var marker = new GMarker(point);
	GEvent.addListener(marker, "click", function(){marker.openInfoWindowHtml(html);}); 
    map.addOverlay(marker);
}

function showPointCasa(lat,lng,html){
	var point = new GLatLng(lat,lng);
 	var icon = new GIcon();
		icon.image = "http://media.clubrural.com/img/icon-home.gif";
	    icon.iconSize = new GSize(16, 16);
		icon.iconAnchor = new GPoint(8, 16);
		icon.infoWindowAnchor = new GPoint(10, 7);  
	var marker = new GMarker(point,icon);
	GEvent.addListener(marker, "click", function(){marker.openInfoWindowHtml(html);}); 
    map.addOverlay(marker);
}


function abrirventana(lat,lng,pos){
	var point = new GLatLng(lat,lng)
	var marker = new GMarker(point);
	map.setCenter(point);
	map.openInfoWindowHtml(map.getCenter(),arrayinfos[pos]);
}

function cargar_mapa_playa(lat,lng){
	if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("mapagoogle"));
		map.addControl(new GLargeMapControl());
		map.addControl(new GOverviewMapControl());
		map.addControl(new GMapTypeControl());
		map.setMapType(G_HYBRID_MAP);
		var point = new GLatLng(lat,lng);
		map.setCenter(point, 12);
		var marker = new GMarker(point,{draggable: true});
		GEvent.addListener(marker, "dragend", function(){  
			updateMarker(marker);  
		});
		map.addOverlay(marker);
	}
}

function cargar_mapa_playa_ok(lat,lng){
	if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("mapagooglesat"));
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.setMapType(G_HYBRID_MAP);
 		var icon = new GIcon();
		icon.image = "http://media.clubrural.com/img/parasol.gif";
	    icon.iconSize = new GSize(27, 25);
		icon.iconAnchor = new GPoint(10, 25);
		icon.infoWindowAnchor = new GPoint(10, 7);       
		var point = new GLatLng(lat,lng);
		map.setCenter(point, 15);
		var marker = new GMarker(point,icon);
		map.addOverlay(marker);
	}
}

function updateMarker(marker){
	var latlng = marker.getPoint();
	document.getElementById("latitud").value = latlng.y;
	document.getElementById("longitud").value = latlng.x;
}
