/**
 * geoloc.js
 *
 * gestion de la carte google
 * @author collomb
 * @version 1.0
 */

if (typeof(GMap2) == "undefined" && $("#GMAP_KEY").length == 0) {
	throw 'GMAP_SCRIPT missing';
}

var mapTypes = new Array();
var mapStaticTypes = new Array;
mapStaticTypes[0] = 'roadmap';mapStaticTypes[1] = 'satellite';mapStaticTypes[2] = 'hybrid';mapStaticTypes[3] = 'terrain';
var DEFAULT_MAP_CENTER;
var DEFAULT_MAP_ZOOM = 5;
var MAP_ACCURACY_STREET = 6;
var MAP_ACCURACY_TOWN = 5;
var MAP_ACCURACY_ADDRESS = 8;

var map = null;
var divGmap = document.getElementById("divGmap");
var marker = null;
var showMarker = true;
var readOnly = $("#mapReadOnly").length > 0;

if (readOnly && parseInt('0' + $("#mapAccuracy").val(), 10) < MAP_ACCURACY_STREET) {
	var pos = $(divGmap).offset();
	$(document.body).append($('<p style="cursor:default; color: #0052F8; padding:2px; font-size:10px; margin:0; position:absolute; top:' + (pos.top + $(divGmap).height() - 52) + 'px; left:' + (pos.left) +
		'px; background:white; border:1px solid #0052F8; z-index:9">Niveau de précision de la localisation : <strong>approximatif</strong></p>').click(function(){$(this).remove();}));
}

if (typeof(GMap2) == "undefined") {
	initStaticGmap();
} else {
	if (GBrowserIsCompatible() && divGmap) {
		initGmap();
	}
}

if ($("#city_id").val() == "")
{
	$("#divGmapOverlay, #divGmapOverlayTooltip").show();
}

$("form").submit(saveGeolocInfos);

var gmapLoaded = false;
function loadGmap() {
	if (! gmapLoaded) {
		gmapLoaded = true;
		google.load("maps", "2", {callback: initGmap});
	}
}

function initGmap() {
	mapTypes[0] = G_NORMAL_MAP;mapTypes[1] = G_SATELLITE_MAP;mapTypes[2] = G_HYBRID_MAP;mapTypes[3] = G_PHYSICAL_MAP;
	DEFAULT_MAP_CENTER = new GLatLng(46.227638,2.213749);
	divGmap.innerHTML = "";
	map = new GMap2(divGmap);
	map.addMapType(G_PHYSICAL_MAP);
	map.addControl(new GLargeMapControl3D(), new GControlPosition(G_ANCHOR_TOP_LEFT,new GSize(0,0)));
	//map.addControl(new GScaleControl(), new GControlPosition(G_ANCHOR_BOTTOM_RIGHT,new GSize(5,20)));
	map.addControl(new GMapTypeControl(), new GControlPosition(G_ANCHOR_TOP_RIGHT,new GSize(0,0)));
	if ($("#mapLat").val() != "" && $("#mapLng").val() != "")
	{
		var t = $("#mapType").val();
		map.setMapType(mapTypes[(t == '') ? 0 : parseInt(t)]);

		var z = $("#mapZoom").val();
		centerMapAndPlaceMarker(new GLatLng(parseFloat($("#mapLat").val()), parseFloat($("#mapLng").val())), (z == '') ? 10 : parseInt(z));
	}
	else
	{
		map.setMapType(mapTypes[0]);
		map.setCenter(DEFAULT_MAP_CENTER, DEFAULT_MAP_ZOOM);
	}
}

function initStaticGmap() {
	var $divGmap = $(divGmap);
	var h = $divGmap.height();
	var w = $divGmap.width();
	var lat = $("#mapLat").val();
	var lng = $("#mapLng").val();
	var zoom = $("#mapZoom").val();
	var type = $("#mapType").val();
	$divGmap.css('background', 'url(http://maps.google.com/staticmap?center=' + lat + ',' + lng + '&maptype=' + mapStaticTypes[type] +
		'&markers=' + lat + ',' + lng + '&zoom=' + zoom + '&size=' + w + 'x' + h + '&key=' + $("#GMAP_KEY").val() + '&sensor=false)');
	$divGmap.mouseover(loadGmap);
}

function centerMapOnCity(lat, lng, city)
{
	centerMapAndPlaceMarker(new GLatLng(lat, lng), 12);
	var geocoder = new GClientGeocoder();
	geocoder.setBaseCountryCode('.fr');
	geocoder.setViewport(map.getBounds());
	city = city || $("#city").val();
	geocoder.getLocations($("#address1").val() + ' ' + $("#address2").val() + ' ' + city, geocoderCallback);
	$("#divGmapOverlay, #divGmapOverlayTooltip").slideUp();
}

function geocoderCallback(response){
	if (response && (response.Status.code == 200)) {
		place = response.Placemark[0];
		if (place.AddressDetails.Accuracy > MAP_ACCURACY_TOWN)
			centerMapAndPlaceMarker(new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]), 12, place.AddressDetails.Accuracy);
	}
}

function centerMapAndPlaceMarker(latLng, zoom, accuracy)
{
	map.setCenter(latLng, zoom);
	setMarkerLatLng(latLng);
	$("#mapAccuracy").val(accuracy || MAP_ACCURACY_TOWN)
}

function saveGeolocInfos()
{
	if (marker)
	{
		$("#mapLat").val(marker.getLatLng().lat());
		$("#mapLng").val(marker.getLatLng().lng());
		$("#mapZoom").val(map.getZoom());
		$("#mapType").val($.inArray(map.getCurrentMapType(), mapTypes));
	}
	return true;
}

function setMarkerLatLng(latLng)
{
	if (marker)
		marker.setLatLng(latLng);
	else {
		/*var myIcon = new GIcon(G_DEFAULT_ICON);
		myIcon.image="/pictures/common/gmap-iconMarker-blue.png";
		myIcon.iconSize = new GSize(17, 30);
		myIcon.iconAnchor = new GPoint(5, 32);*/
		marker = new GMarker(latLng/*, myIcon*/, {draggable:! readOnly});
		GEvent.addListener(marker, 'dragend', function(){$("#mapAccuracy").val(MAP_ACCURACY_ADDRESS);})
		map.addOverlay(marker);
	}
}

function hideMarker()
{
	if (entMarker)
		map.removeOverlay(entMarker);
}

