/*
** 2010 IE Ingo Eberhardt ingo@tunnelen.de
** JavaScript for MikroKopter Map WorldWide
** Januar 2010 		initial release
**
*/

//<![CDATA[

/*
** SEARCH FUNCTIONS
*/

// 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) {
	// clear canvas
	//map.clearOverlays();
	
	//GLog.write( 'addAddressToMap called' );
	
	if (!response || response.Status.code != 200) {
		
		// NO SUCCESS
		alert("Sorry, we were unable to geocode that address");
		
	} else {
		//GLog.write( 'Address found. Preparing ouput: ' );
		
		// get PLACE from response
		place = response.Placemark[0];
		
		// transcode Placemark to a point
		point = new GLatLng(place.Point.coordinates[1],
							place.Point.coordinates[0]);

		//GLog.write( 'Point: ' + place.Point.coordinates[1] + ' - ' + place.Point.coordinates[0] );
		
		// build infowindow text
		myHtml = '<div class="ui-widget-black ui-widget-content-black ui-helper-clearfix">';
		myHtml += '<h2>' + place.address + '</h2>';
		myHtml += '<p>' + 'Lat: ' + point.lat() + '<br />Lon: ' + point.lng() + '</p>';
		myHtml += '</div>';
		map.openInfoWindowHtml( point, myHtml );
		
		map.panTo( point );
		map.setZoom( 12 );
	 
		// put LatLng in the right input fields, FIRST formular
		//document.getElementById("lat").value = point.lat();
		//document.getElementById("lng").value = point.lng();
	}
}

// and a copy for output with coordinates in form
function addAddressToMapWithCoordinates(response) {
	// clear canvas
	//map.clearOverlays();
	
	//GLog.write( 'addAddressToMapWithCoordinates called' );
	
	if (!response || response.Status.code != 200) {
		
		// NO SUCCESS
		alert("Sorry, we were unable to geocode that address");
		
	} else {
		//GLog.write( 'Address found. Preparing ouput: ' );
		
		// get PLACE from response
		place = response.Placemark[0];
		
		// transcode Placemark to a point
		point = new GLatLng(place.Point.coordinates[1],
							place.Point.coordinates[0]);

		//GLog.write( 'Point: ' + place.Point.coordinates[1] + ' - ' + place.Point.coordinates[0] );
		
		// build infowindow text
		myHtml = '<p id="blob_heading">' + place.address + '</p><p id="blob_content">' + 'Lat: ' + point.lat() + '<br />Lon: ' + point.lng() + '</p>';
		map.openInfoWindowHtml( point, myHtml );
		
		map.panTo( point );
		map.setZoom( 12 );
	 
		// put LatLng in the right input fields, FIRST formular
		document.getElementById("lat").value = point.lat();
		document.getElementById("lon").value = point.lng();
	}
}

// showLocation() is called when you click on the Search button
// in the form.  It geocodes the address entered into the form
// and adds a marker to the map at that location.
function showLocation() {
	var address = document.getElementById("search_location").value;
	//GLog.write( 'address' + address );
	geocoder.getLocations(address, addAddressToMap);
}

function showLocationWithCoordinates() {
	var address = document.getElementById("search_location").value;
	//GLog.write( 'address' + address );
	geocoder.getLocations(address, addAddressToMapWithCoordinates);
}

//]]>