var map = null;
var geocoder = null;

function load() {
	if (GBrowserIsCompatible()) {
		var mapdiv = document.getElementById("map_canvas");

		map = new GMap2( mapdiv );
		map.setCenter(new GLatLng(48.8514211, 2.2920236), 13);
		map.addControl(new GMapTypeControl());
		map.addControl(new GLargeMapControl())
		geocoder = new GClientGeocoder();
		GEvent.addListener(map, "click", mapClickEvent );
		GEvent.addDomListener(mapdiv, "DOMMouseScroll", wheelZoom );
		GEvent.addDomListener(mapdiv, "mousewheel", wheelZoom );
	}

	document.getElementById('address').onkeydown = keyListenAddr;
	document.getElementById('lat').onkeydown = keyListenCoord;
	document.getElementById('lon').onkeydown = keyListenCoord;
}

function keyListenAddr ( e ) {
	if ( e == undefined )
		e = window.event;
	if (e.keyCode == 13)
		btnAddr2Gps();
}

function keyListenCoord ( e ) {
	if ( e == undefined )
		e = window.event;
	if (e.keyCode == 13)
		btnGps2Addr();
}

function wheelZoom(e) {
	var mouseX, mouseY;
	if(!e) e = window.event;
	mouseX = (e.layerX) ? e.layerX : e.offsetX;
	mouseY = (e.layerY) ? e.layerY : e.offsetY;
	var c = (e.target) ? e.target : e.srcElement;
	while(c) {
		mouseX += c.offsetLeft;
		mouseY += c.offsetTop;
		c = c.offsetParent;
		if(c == map.getContainer()) break;
	}
	// This is the appropriate point for the cursor at the moment of zooming in or out
	var point = map.fromContainerPixelToLatLng(new GPoint(mouseX, mouseY));

	// Prevent from scrolling the page when zooming the map
	if(window.event) { e.returnValue = false; } // IE
	if(e.cancelable) { e.preventDefault(); } // DOM-Standard
	// Scroll to where the pointer is
	if((e.detail || -e.wheelDelta) < 0) {
		map.zoomIn(point);
	} else {
		map.zoomOut(point);
	}
}


var marker = null;
function addrToGpsCoordinates(address) {
	if (geocoder) {
		geocoder.getLatLng(
		address,
		function(point) {
			if (!point) {
				alert(address + ' could not be found');
			} else {
				map.setCenter(point, 13);
				if ( marker != null )
					map.removeOverlay( marker );
				marker = new GMarker(point);
				map.addOverlay(marker);
				marker.openInfoWindowHtml(
					'<b>Lat x Lon : </b>' + point.lat()+', '+point.lng()+'<br />'+
					'<b>Address : </b>' + address
				);

				var lat = document.getElementById('lat');
				var lon = document.getElementById('lon');
				lat.value = point.lat();
				lon.value = point.lng();

				intGps2GpsDeg();
			}
		}
	);
	}
}




function getcoord(address) {
	if (geocoder) {
		geocoder.getLatLng(
		address,
		function(point) {
			if (!point) {
				alert(address + ' could not be found');
			} else {
				

				 //alert (point.lat()+' ,ffff, '+point.lng());
				 //this.test.value=point.lng();
					//var titi;
					//titi = point.lng();
					this.tyty.lat.value= point.lat();
					this.tyty.lng.value= point.lng();

					alert (point.lng());
	
					
			
			}
		}
	);
	}
}
function getcoord2(address,nameform) {
	if (geocoder) {
		geocoder.getLatLng(
		address,
		function(point) {
			if (!point) {
				alert(address + ' could not be found');
			} else {
				
					var lat = document.getElementById('lat'+nameform);
					lat.value = point.lat();
					var lng = document.getElementById('lng'+nameform);
					lng.value = point.lng();
	
	
					
			
			}
		}
	);
	}
}






function gpsToAddr( lat, lon ) {
	// Cette méthode en contient en fait 2 autres

	var showAddress = function (response) {
		map.clearOverlays();
		if (!response || response.Status.code != 200) {
			//alert("Status Code:" + response.Status.code);
		} else {
			place = response.Placemark[0];
			point = new GLatLng( lat, lon );
			if ( marker != null )
				map.removeOverlay( marker );
			marker = new GMarker(point);
			map.addOverlay(marker);
			marker.openInfoWindowHtml(
				'<b>Lat x Lon : </b>' + lat+", "+lon + '<br>' +
				'<b>Address : </b>' + place.address + '<br>'
			);
			var form = document.getElementById('formulaire');
			form.address.value = place.address;
		}
	}

	function getAddress( latlng ) {
		geocoder.getLocations(latlng, showAddress);
	}

	getAddress( new GLatLng( lat, lon ) );
}



function btnAddr2Gps() {
	var addr = document.getElementById('address').value;
	addrToGpsCoordinates( addr );
}

function btnGps2Addr() {
	var lat = document.getElementById('lat').value;
	var lon = document.getElementById('lon').value;
	gpsToAddr( lat, lon );
	intGps2GpsDeg();
}

function intGps2GpsDeg() {
	var lat = document.getElementById('lat').value;
	var lon = document.getElementById('lon').value;

	if (lat==null || isNaN( lat ) )
		lat=0;
	if (lon==null || isNaN( lon ) )
		lon=0;

	latPlus = (lat > 0);
	lonPlus = (lon > 0);

	document.getElementById('latOrientationN').checked = latPlus;
	document.getElementById('latOrientationS').checked = ! latPlus;

	document.getElementById('lonOrientationE').checked = lonPlus;
	document.getElementById('lonOrientationW').checked = ! lonPlus;

	// Change to absolute value
	lat = Math.abs(lat);
	lon = Math.abs(lon);

	// Convert to Degree Minutes Seconds Representation
	latDeg = Math.floor(lat);
	latMin = Math.floor((lat-latDeg)*60);
	latSec =  (Math.round((((lat - latDeg) - (latMin/60)) * 60 * 60) * 1000) / 1000 ) ;
	lonDeg = Math.floor(lon);
	lonMin = Math.floor((lon-lonDeg)*60);
	lonSec = (Math.round((((lon - lonDeg) - (lonMin / 60 )) * 60 * 60) * 1000) / 1000);

	document.getElementById('latDeg').value = latDeg;
	document.getElementById('latDegMinute').value = latMin;
	document.getElementById('latDegSeconde').value = latSec;

	document.getElementById('lonDeg').value = lonDeg;
	document.getElementById('lonDegMinute').value = lonMin;
	document.getElementById('lonDegSeconde').value = lonSec;
}

function btnGpsDeg2Addr() {
	var latDeg = parseInt( document.getElementById('latDeg').value );
	if ( isNaN( latDeg ) )
		latDeg = 0;

	var latMinute = parseInt( document.getElementById('latDegMinute').value );
	if ( isNaN( latMinute ) )
		latMinute = 0;

	var latSeconde = parseInt( document.getElementById('latDegSeconde').value );
	if ( isNaN( latSeconde ) )
		latSeconde = 0;

	var lonDeg = parseInt( document.getElementById('lonDeg').value );
	if ( isNaN( lonDeg ) )
		lonDeg = 0;

	var lonMinute = parseInt( document.getElementById('lonDegMinute').value );
	if ( isNaN( lonMinute ) )
		lonMinute = 0;

	var lonSeconde = parseInt( document.getElementById('lonDegSeconde').value );
	if ( isNaN( lonSeconde ) )
		lonSeconde = 0;

	var lat = latDeg + latMinute/60 + latSeconde/3600;
	var lon = lonDeg + lonMinute/60 + lonSeconde/3600;

	if ( document.getElementById('latOrientationS').checked )
		lat *= -1;

	if ( document.getElementById('lonOrientationW').checked )
		lon *= -1;

	document.getElementById('lat').value = Math.round(lat*1000000)/1000000;
	document.getElementById('lon').value = Math.round(lon*1000000)/1000000;

	gpsToAddr( lat, lon );
}

function mapClickEvent( overlay, pos ) {
	var lat = document.getElementById('lat');
	var lon = document.getElementById('lon');

	lat.value = arr( pos.lat() );
	lon.value = arr( pos.lng() );
	btnGps2Addr();
}

function arr( nb ) {
	return Math.round( nb*1000000 ) /1000000;
}



