// Google maps API interface, see _inc/class/Gmaps.php for example:

var map;
var geocoder;
var marker;
var bubblehtml;

var DEFAULT_ZOOM_LEVEL = 14;

function gmap_init() {

	map = new GMap2(document.getElementById("gmap"));

	with (map) {
		// control for navigation on map:
		addControl(new GSmallMapControl());

		// control for switching map type:
		addControl(new GMenuMapTypeControl());

		// overview map in corner:
		// addControl(new GOverviewMapControl());

		// normal map default:
		setMapType(G_NORMAL_MAP);
	}

	geocoder = new GClientGeocoder();

	// Initialize geocoder:
	with (geocoder) {
		setBaseCountryCode("de");
	};
}

// callback function: set up map with resulting coordinates:
function gmap_geocoder_callback(result, bubbletext, address) {

	bubblehtml = bubbletext;

	if(!result) {
		// output error message (only for debugging):
		// alert("geocoder lookup error for address '" + address + "': result undefined!")
	}
	else if (result.Status.code != G_GEO_SUCCESS) {
		var errmsg;
		switch (result.Status.code)	{
			case G_GEO_MISSING_ADDRESS :			errmsg = "Missing Address: The address was either missing or had no value."; break;
			case G_GEO_UNKNOWN_ADDRESS :			errmsg = "Unknown Address:  No corresponding geographic location could be found for the specified address."; break;
			case G_GEO_UNAVAILABLE_ADDRESS :	errmsg = "Unavailable Address:  The geocode for the given address cannot be returned due to legal or contractual reasons."; break;
			case G_GEO_BAD_KEY :							errmsg = "Bad Key: The API key is either invalid or does not match the domain for which it was given"; break;
			case G_GEO_TOO_MANY_QUERIES :			errmsg = "Too Many Queries: The daily geocoding quota for this site has been exceeded."; break;
			case G_GEO_SERVER_ERROR :					errmsg = "Server error: The geocoding request could not be successfully processed."; break;
			case G_GEO_SUCCESS :							errmsg = "Success"; break;
			default:													errmsg = "Unknown error"; break;
		}

		// output error message (only for debugging):
		// alert("geocoder lookup error for address '" + address + "': " + errmsg);
	}
	else {
		venue = result.Placemark[0];
		gmap_show_by_coords(venue.Point.coordinates[1], venue.Point.coordinates[0], bubblehtml)
	}
}

// call Google Geocoder lookup for address to latidude/longitude values:
function gmap_show_by_address(address, bubbletext) {

	with (geocoder) {
		getLocations(
			address,
			function(result) {
				gmap_geocoder_callback(result, bubbletext, address);
			}
		);
	}
}

// show map of location with given coordinates:
function gmap_show_by_coords(latitude, longitude, bubbletext) {

	bubblehtml = bubbletext;

	var venue_coord = new GLatLng(latitude, longitude);

	// set up marker with venue information:
	marker = new GMarker(venue_coord);

	GEvent.addListener(
		marker,
		'click',
		function() {
			marker.openInfoWindowHtml(bubblehtml);
		}
	);

	map.addOverlay(marker);

	// center map to coordinates of location:
	map.setCenter(
		venue_coord,
		DEFAULT_ZOOM_LEVEL
	);

	// open info window:
	GEvent.trigger(marker, 'click');
}

function gmap_open_new_window() {
	// collect info about currently displayed map:
	var card_type = map.getCurrentMapType();	// GMapType
	// alert(card_type);
	var center = map.getCenter();							// GLatLng
	// alert(center);
	var zoom = map.getZoom();
	// alert(zoom);
	var markerbubble = bubblehtml;
	// alert(markerbubble);
	var markercoords = marker.getLatLng();
	// alert(markercoords);

	//window.open("xxx.php?center=");
}

//if (GBrowserIsCompatible()) {
// TODO: GUnload einbauen bei Page-Unload
//}

