
var latitude = 0.0;		// Global latitude selected value
var longitude = 0.0;	// Global longitude selected value
var mapaddress = "";	// Global address selected value
var mygeocoder = null;	// Global geocoding (because goToAddress() )
var mymap = null;		// Global map (because goToAddress() )
var mymarker = null;	// Global marker (because goToAddress() )

/**
 * Initialize Google Maps API.
 */
function initialize(gheight, gwidth, startlat, startlng, startzoom, editable) {
	if (GBrowserIsCompatible()) {
		var mapOptions = {
			googleBarOptions : {
				style : "new",
				showOnLoad : true,
				//size: new GSize(10, 200)
			},
			size: new GSize(gwidth, gheight)
		}

		mymap = new GMap2(document.getElementById("mapcanvas"), mapOptions);	// map object
		var center = new GLatLng(startlat, startlng);
		mymap.setCenter(center, startzoom);
		
		if (editable == 1) {
			mymarker = new GMarker(center, {draggable: true});	// draggle marker object
		} else {	
			mymarker = new GMarker(center, {draggable: false});	// static marker object
		}
		mymap.addOverlay(mymarker);
		
		mygeocoder = new GClientGeocoder();	// Geocoding
		
		updateVars(mymarker.getLatLng(), mygeocoder);
		
		
		if (editable == 1) {
			GEvent.addListener(mymarker, "dragstart", function() {
				mymap.closeInfoWindow();
			});
		

			GEvent.addListener(mymarker, "dragend", function() {
				updateVars(mymarker.getLatLng(), mygeocoder);
			});
			
			GEvent.addListener(mymap,"click", function(overlay, latlng) {
				if (overlay) {
            		// ignore if we click on the info window
            		return;
          		}
				
				if (latlng) {
					mymarker.setLatLng(latlng);
					updateVars(latlng, mygeocoder);
				}
			});
		}
		
		GEvent.addListener(mymarker, "mouseout", function() {
			mymap.closeInfoWindow();
		});

		GEvent.addListener(mymarker, "mouseover", function() {
			putInfoWindow(mymarker);
		});
			
		
		//mymap.removeMapType(G_HYBRID_MAP);
		//mymap.addMapType(G_PHYSICAL_MAP);
		//mymap.addControl(new GNavLabelControl());
		//mymap.addControl(new GSmallMapControl());
		//mymap.addControl(new GMapTypeControl());
		mymap.addControl(new GLargeMapControl3D());
		//mymap.addControl(new GHierarchicalMapTypeControl());
		//mymap.addControl(new GOverviewMapControl());
		
		mymap.setUIToDefault();
		//mymap.enableGoogleBar();
		
	} else {
		alert("Desculpe, mas o Google Maps não é compativel com o seu browser...");
	}
		
	
	/**
	 * Pur Info Window (popup) in the marker.
	 */
	function putInfoWindow(marker) {
		var myHtml = null;
		
		if(editable == 1) {
			myHtml = '<div align="justify" "style="height:95px; width:200px">Clique no mapa ou arraste o marcador para o local pretendido. No final de posicionar o marcador, clique em &quot;Submeter Coordenadas&quot; para subemeter as coordenadas GPS.</div>';
		} else {
			myHtml = '<div align="justify" "style="height:10px; width:200px">Eu sou as coordenadas GPS! Impossivel alterar-las neste modo... (Modo de leitura).</div>';
		}

//You can also change the map zoom and move it using the controls on the left and/or your mouse.<br />
		marker.openInfoWindowHtml(myHtml, {width:500});
	}
	
}


/**
 * Get address for a specific position and put in the html.
 */	
function showAddress(response) {
	if (!response || response.Status.code != 200) {
		document.getElementById("showaddressinfo").innerHTML = "";
	} else {
		var place = response.Placemark[0];
		var point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
		var address = 	'<b>Address:</b> ' + place.address + ' (' + place.AddressDetails.Country.CountryNameCode + ')';
		mapaddress = place.address + ' (' + place.AddressDetails.Country.CountryNameCode + ')';
		
		if (address != null) {
			document.getElementById("showaddressinfo").innerHTML = address;
		} else {
			document.getElementById("showaddressinfo").innerHTML = "";
		}
	}
}


/**
 * Update variables.
 */
function updateVars(latlng, geocoder) {
	latitude = latlng.lat();
	longitude = latlng.lng();
	document.getElementById("latdiv").innerHTML = "<b>Latitude:</b> " + latitude.toFixed(10);
	document.getElementById("lngdiv").innerHTML = "<b>Longitude:</b> " + longitude.toFixed(10);
		
	geocoder.getLocations(latlng, showAddress);
}


/**
 * Go to and specific address.
 */
function goToAddress(address) {
	if (mygeocoder && address) {
		mygeocoder.getLatLng(address,
			function(point) {
				if (!point) {
					alert(address + " não encontrado...");
				} else {
					mymap.setCenter(point);
					mymarker.setLatLng(point);
					updateVars(point, mygeocoder);
				}
			});
	}
}