google.load('maps', '2');

//var map;
var geocoder;
var doSubmit;
var showFullAddress;
var gFormat;

// Call this function when the page has been loaded
function initialize() {
	//map = new google.maps.Map2(document.getElementById('map'));
	//map.setCenter(new google.maps.LatLng(37.4419, -122.1419), 13);
	//map.addControl(new google.maps.LargeMapControl());
	//var mapControl = new google.maps.MapTypeControl();
	//map.addControl(mapControl);
	//map.enableScrollWheelZoom();
	geocoder = new google.maps.ClientGeocoder();
	geocoder.setBaseCountryCode(defaultCountry);
}
//google.setOnLoadCallback(initialize);

function getLocalityName(place) {
	var depLocName = '';

	//if(showFullAddress == 0) {
		try {
			depLocName = place.AddressDetails.Country.Locality.DependentLocality.DependentLocalityName+', ';
		}
		catch(e) {};
	
		try {
			depLocName = place.AddressDetails.Country.AdministrativeArea.Locality.DependentLocality.DependentLocalityName+', ';
		}
		catch(e) {};
	
		try {
			depLocName = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.DependentLocality.DependentLocalityName+', ';
		}
		catch(e) {};

		//

		try {
			depLocName = place.AddressDetails.Country.Thoroughfare.ThoroughfareName+', ';
		}
		catch(e) {};

		try {
			depLocName = place.AddressDetails.Country.AdministrativeArea.Thoroughfare.ThoroughfareName+', ';
		}
		catch(e) {};
	
		try {
			depLocName = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Thoroughfare.ThoroughfareName+', ';
		}
		catch(e) {};
	
		try {
			depLocName = place.AddressDetails.Country.Locality.Thoroughfare.ThoroughfareName+', ';
		}
		catch(e) {};
	
		try {
			depLocName = place.AddressDetails.Country.Locality.DependentLocality.Thoroughfare.ThoroughfareName+', ';
		}
		catch(e) {};
	
		try {
			depLocName = place.AddressDetails.Country.AdministrativeArea.Locality.Thoroughfare.ThoroughfareName+', ';
		}
		catch(e) {};
	
		try {
			depLocName = place.AddressDetails.Country.AdministrativeArea.Locality.DependentLocality.Thoroughfare.ThoroughfareName+', ';
		}
		catch(e) {};
	
		try {
			depLocName = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.Thoroughfare.ThoroughfareName+', ';
		}
		catch(e) {};
	
		try {
			depLocName = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.DependentLocality.Thoroughfare.ThoroughfareName+', ';
		}
		catch(e) {};
	
		//

		try {
			depLocName = place.AddressDetails.Country.Premise.PremiseName+', ';
		}
		catch(e) {};

		try {
			depLocName = place.AddressDetails.Country.Locality.Premise.PremiseName+', ';
		}
		catch(e) {};

		try {
			depLocName = place.AddressDetails.Country.Locality.Thoroughfare.Premise.PremiseName+', ';
		}
		catch(e) {};
	
		try {
			depLocName = place.AddressDetails.Country.Locality.DependentLocality.Premise.PremiseName+', ';
		}
		catch(e) {};
	
		try {
			depLocName = place.AddressDetails.Country.Locality.DependentLocality.Thoroughfare.Premise.PremiseName+', ';
		}
		catch(e) {};

		try {
			depLocName = place.AddressDetails.Country.AdministrativeArea.Premise.PremiseName+', ';
		}
		catch(e) {};
	
		try {
			depLocName = place.AddressDetails.Country.AdministrativeArea.Thoroughfare.Premise.PremiseName+', ';
		}
		catch(e) {};
	
		try {
			depLocName = place.AddressDetails.Country.AdministrativeArea.Locality.Premise.PremiseName+', ';
		}
		catch(e) {};
	
		try {
			depLocName = place.AddressDetails.Country.AdministrativeArea.Locality.Thoroughfare.Premise.PremiseName+', ';
		}
		catch(e) {};
	
		try {
			depLocName = place.AddressDetails.Country.AdministrativeArea.Locality.DependentLocality.Premise.PremiseName+', ';
		}
		catch(e) {};
	
		try {
			depLocName = place.AddressDetails.Country.AdministrativeArea.Locality.DependentLocality.Thoroughfare.Premise.PremiseName+', ';
		}
		catch(e) {};
	
		try {
			depLocName = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Premise.PremiseName+', ';
		}
		catch(e) {};
	
		try {
			depLocName = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Thoroughfare.Premise.PremiseName+', ';
		}
		catch(e) {};
	
		try {
			depLocName = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.Premise.PremiseName+', ';
		}
		catch(e) {};
	
		try {
			depLocName = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.Thoroughfare.Premise.PremiseName+', ';
		}
		catch(e) {};
	
		try {
			depLocName = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.DependentLocality.Premise.PremiseName+', ';
		}
		catch(e) {};

		try {
			depLocName = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.DependentLocality.Thoroughfare.Premise.PremiseName+', ';
		}
		catch(e) {};

		// try to determine the locality name
		try {
			return depLocName + place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName;
		}
		catch(e) {};

		try {
			return depLocName + place.AddressDetails.Country.AdministrativeArea.Locality.LocalityName;
		}
		catch(e) {};

		try {
			return depLocName + place.AddressDetails.Country.Locality.LocalityName;
		}
		catch(e) {};
	//}

	// finally, return the address
	/* // disabling this. The above shall return something consistent (i.e. searching for "38230" doesn't give a specific enough response)
	try {
		return place.address;
	}
	catch(e) {};
	*/

	return ''; //place.address;
}

function addAddressToMap(response) {
	//map.clearOverlays();
	//if (!response || response.Status.code != 200 || response.Placemark[0].AddressDetails.Country.CountryNameCode != defaultCountry) {
	if (!response || response.Status.code != 200) {
		//alert('Sorry, we were unable to geocode that location');
		document.getElementById('location').value = '';
		document.getElementById('currentLocationDiv').style.display = "none";
		document.getElementById('longitude').value = '';
		document.getElementById('latitude').value = '';
		document.getElementById('mapImage').innerHTML = '';
		document.getElementById('errorLocation').innerHTML = '<div class="error">Lieu non reconnu</div>';
		document.getElementById('errorLocation').style.display = "block";
		doSubmit = 0;
	} else if (response.Placemark[0].AddressDetails.Accuracy < 4 || getLocalityName(response.Placemark[0]) == '') {
		//alert('Sorry, you need to be more specific about the location');
		document.getElementById('location').value = '';
		document.getElementById('currentLocationDiv').style.display = "none";
		document.getElementById('longitude').value = '';
		document.getElementById('latitude').value = '';
		document.getElementById('mapImage').innerHTML = '';
		document.getElementById('errorLocation').innerHTML = '<div class="error">Veuillez être plus précis</div>';
		document.getElementById('errorLocation').style.display = "block";
		doSubmit = 0;
	} else {
		place = response.Placemark[0];
		//point = new google.maps.LatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
		//marker = new google.maps.Marker(point);
		//google.maps.Event.addListener(marker, 'click', function() { marker.openInfoWindowHtml(place.address); });
		//map.setCenter(point, 13);
		centerMap(place.Point.coordinates[0], place.Point.coordinates[1], gFormat);
		document.getElementById('location').value = getLocalityName(place);
		document.getElementById('currentLocation').innerHTML = '<div class="location">' + getLocalityName(place) + '</div>';
		document.getElementById('currentLocationDiv').style.display = "block";
		document.getElementById('longitude').value = place.Point.coordinates[0];
		document.getElementById('latitude').value = place.Point.coordinates[1];
		document.getElementById('errorLocation').style.display = "none";
		//map.addOverlay(marker);
		//marker.openInfoWindowHtml('<div>' + place.address + '</div>');
		//document.formLocation.location.value = getLocalityName(place);
	}
	if(doSubmit == 1) {
		document.getElementById('doSubmit').value = 1;
		document.getElementById('mainForm').submit();
	}
}

function centerMap(lng, lat, format) {
	switch(format) {
		case 1:
			document.getElementById('mapImage').innerHTML="<img class=\"map\" src='http://maps.google.com/staticmap?size=262x150&zoom=13&markers="+lat+","+lng+"&key="+googleApiKey+"' width='262' height='150' />";
			break;
		case 2:
			//document.getElementById('mapImage').innerHTML="<img class=\"map\" src='http://maps.google.com/staticmap?size=180x150&zoom=13&markers="+lat+","+lng+"&key="+googleApiKey+"' width='180' height='150' />";
			document.getElementById('mapImage').innerHTML="<img class=\"map\" src='http://maps.google.com/staticmap?size=262x150&zoom=13&markers="+lat+","+lng+"&key="+googleApiKey+"' width='262' height='150' />";
			break;
	}
}

function showLocation(format, address, in_doSubmit, in_showFullAddress) {
	if(address != '') {
		document.getElementById('doSubmit').value = '';
		if(in_doSubmit != 1) {
			doSubmit = 0;
		} else {
			doSubmit = 1;
		}
		if(in_showFullAddress != 1) {
			showFullAddress = 0;
		} else {
			showFullAddress = 1;
		}
		gFormat = format;
		//geocoder.getLocations(address + ' ' + defaultCountrySearch, addAddressToMap);
		geocoder.getLocations(address, addAddressToMap);
	} else {
		document.getElementById('location').value = '';
		document.getElementById('currentLocationDiv').style.display = "none";
		document.getElementById('currentLocation').innerHTML = '';
		document.getElementById('longitude').value = '';
		document.getElementById('latitude').value = '';
		document.getElementById('mapImage').innerHTML = '';
		document.getElementById('errorLocation').innerHTML = '';
	}
}

