// JavaScript Document
var gdir;
var map;
var geocoder = null;
var addressMarker;
var R = 6371; // Radio de la tierra en km

var minLat = 0;
var minLng = 0;
var maxLat = 0;
var maxLng = 0;

function getIcon(image) {
var icon = new GIcon(G_DEFAULT_ICON);
	icon.image = "images/" + image + ".png";
	icon.iconSize = new GSize(32, 32);
	icon.iconAnchor = new GPoint(16, 16);
	icon.infoWindowAnchor = new GPoint(16, 16);
	icon.shadow = "images/" + image + "-shadow.png";
	icon.shadowSize = new GSize(32, 32);
return icon;
}

function setInitBound(lat, lng) {
	minLat = lat;
	maxLat = lat;
	minLng = lng;
	maxLng = lng;
}

function setMinMaxBound(lat, lng) {
	if ((lat < minLat) || (minLat == 0)) {
		minLat = lat;
	}
	if ((lat > maxLat) || (maxLat == 0)) {
		maxLat = lat;
	}
	if ((lng < minLng) || (minLng == 0)) {
		minLng = lng;
	}
	if ((lng > maxLng) || (maxLng == 0)) {
		maxLng = lng;
	}
}

function getMinMaxBound() {
	return new GLatLngBounds(new GLatLng(minLat, minLng),new GLatLng(maxLat, maxLng));
}

function myAddMark(lat, lng, nombre, html, icono) {
	var point = new GLatLng(lat, lng);
	if (icono != "") {
		markerOptions = { title:nombre, icon:getIcon(icono) };
	} else {
		markerOptions = { title:nombre };
	}
	var marker = new GMarker(point, markerOptions);
	
	map.addOverlay(marker);
	
	if (html != "") {
		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml(html, { "maxWidth": 300 });	
		});
	}
	
	setMinMaxBound(lat, lng);
	return marker;
}

function setDirections(fromAddress, toAddress, locale) {
	geocoder = new GClientGeocoder();
	geocoder.getLatLng(toAddress, function(point) { 
		if (point) {
			//alert(point); // Yipeeee!
			$("#indicaciones").css("display","block");
			map.clearOverlays();
			gdir.load("from: " + fromAddress + " to: " + point.toUrlValue(),
				{ "locale": locale });
			} else {
			  alert (toAddress + " not geocoded!! :( ");
			}
		}); 
//	gdir.load("from: " + fromAddress + " to: " + toAddress, { "locale": locale });
}

function centerMap (point) {
	map.setCenter(point, 12);
}

function showInitialMap(response) {
	map.clearOverlays();
	if (!response || response.Status.code != 200) {
		var point = new GLatLng(37.983375, -1.129897);
		map.setCenter(point, 9);
	} else {
		var place = response.Placemark[0];
		var point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
		map.setCenter(point, 9);
	}
}
// addAddressToMap() is called when the geocoder returns an
// answer.	It adds a marker to the map with an open info window
// showing the nicely formatted version of the address and the country code.
function addAddressToMap(response, zoom) {
	map.clearOverlays();
	if (!response || response.Status.code != 200) {
		$("#gMapMsg").html("No se ha encontrado en el mapa el elemento indicada.");
	} else {
		$("#gMapMsg").html("");
		place = response.Placemark[0];
		point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
		marker = new GMarker(point, {draggable: true});
		$("#latitud").val(place.Point.coordinates[1]);
		$("#longitud").val(place.Point.coordinates[0]);
		map.setCenter(point, zoom);
		map.addOverlay(marker);

		GEvent.addListener(marker, "dragend", function() {
			var punto = marker.getPoint();
			$("#gMapMsg").text("Moviendo a la posición: " + punto.toUrlValue());
			$("#latitud").val(punto.lat());
			$("#longitud").val(punto.lng());
		});
	}
}

function addAddressToMapP(response) {
	addAddressToMap(response, 9);
}

function addAddressToMapL(response) {
	addAddressToMap(response, 12);
}

function addAddressToMapB(response) {
	addAddressToMap(response, 15);
}

function getDistanceBetweenCords(lat1, lng1, lat2, lng2) {
	var dLat = (lat2-lat1).toRad();
	var dLng = (lng2-lng1).toRad();
	var a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) * Math.sin(dLng/2) * Math.sin(dLng/2);
	var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
	var d = R * c;
	return d;
}