/*
 * TZ 2009-02-18
 * Hilfsfunktionen für GoogleMaps. 
 */

var GEOCODER = new GClientGeocoder();
var compatibleBrowser = false;
var ZOOM_LEVEL = 14;

GEOCODER.setCache(null);

/**
 * Fragt die Koordinaten für address bei GoogleMaps an und initialisiert eine
 * GoogleMap im Element map.
 * 
 * @param address Adresse als String.
 * @param map DOMElement, in dem die GoogleMap dargestellt wird. 
 */
function showAddress(address, map) {
	// console.info("Suche Koordinaten fuer: " + address + "...");
	//console.time("showAddress");
	
	GEOCODER.getLocations(address, function(response) {
		if (!response || response.Status.code != G_GEO_SUCCESS) {
			var message = '';

            switch (response.Status.code) {
                case G_GEO_BAD_REQUEST:
                    message = 'Die Anfrage konnte nicht erfolgreich analysiert werden.';
                    break;

                case G_GEO_SERVER_ERROR:
                    message = 'Es ist ein unbekannter, serverseitiger Fehler aufgetreten.';
                    break;

                case G_GEO_UNKNOWN_ADDRESS:
                    message = 'Unbekannte Adresse';
                    break;

                case G_GEO_BAD_KEY:
                    message = 'Der angegebene Schluessel ist ungueltig.';
                    break;

                case G_GEO_TOO_MANY_QUERIES:
                    message = 'Es wurden zuviele Serveranfragen gestellt.';
                    break;

                default:
                    message = 'Es ist ein unbekannter Fehler aufgetreten.';
                    break;
            } // switch

            //console.error("FEHLER:" + message);
            
        } else {
            var location = response.Placemark[0];
            //console.debug(response);
            //console.info('...gefunden: ' + location.Point.coordinates[1] + "|" + location.Point.coordinates[0]);
            
            if (location) {
                var point = new GLatLng(location.Point.coordinates[1],
                		location.Point.coordinates[0]);
                
                map.addOverlay(new GMarker(point));
            	map.setCenter(point, ZOOM_LEVEL);
            } // if
            //console.timeEnd("showAddress");
        } // else
    } // getLocations Callback
	) // getLocations
} // showAddress