var icmsMap;
var gdir;
var geocoder = null;

function load( latitude, longitude, zoom, mapcontrol, typcontrol, showMapControl, showRouterControl ) {
	if( GBrowserIsCompatible() ) {
		function createMarker( point, html ) {
			var marker = new GMarker( point );
			return marker;
		}

        var icmsMap = new GMap2( document.getElementById( "icmsMap" ) );
		if( mapcontrol == 'small' ) icmsMap.addControl( new GSmallMapControl() );
		if( mapcontrol == 'large' ) icmsMap.addControl( new GLargeMapControl() );
		if( showMapControl == 'y' ) icmsMap.addControl( new GMapTypeControl() );
		icmsMap.setMapType( typcontrol );
        icmsMap.setCenter( new GLatLng( latitude, longitude ), zoom );

		var icon = new GIcon();
		icon.image = "http://www.google.com/mapfiles/marker.png";
		icon.shadow = "http://www.google.com/mapfiles/shadow50.png";
		icon.iconSize = new GSize( 10, 24 );
		icon.shadowSize = new GSize( 17, 14 );

		var point = new GLatLng( latitude, longitude );
		var marker = createMarker( point, '' )
/*
		GEvent.addListener( marker, "click", function() {
			marker.openInfoWindowHtml( '<b>it_artwork</b><br>klingenbrunnen 23<br>73342 bad ditzenbach' );
		} );
*/
		icmsMap.addOverlay( marker );
//		GEvent.trigger( marker, "click" );

		geocoder = new GClientGeocoder();

		if( showRouterControl == 'y' ) {
			gdir = new GDirections( icmsMap, document.getElementById( "icmsMapDirections" ) );
			GEvent.addListener( gdir, "load", onGDirectionsLoad );
			GEvent.addListener( gdir, "error", handleErrors );

			//setDirections( latitudeSource + ", " + longitudeSource, latitude + ", " + longitude, "de_DE" );
		}
	}
}

function setDirections(fromAddress, toAddress, locale) {
	gdir.load("from: " + fromAddress + " to: " + toAddress, { "locale": locale });
}

function handleErrors() {
	if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
		alert("Adresse existiert nicht (evtl. mit Latitude und Longitude versuchen)!\n Fehler: " + gdir.getStatus().code);
	else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
		alert("Adresse wurde nicht gefungen (evtl. mit Latitude und Longitude versuchen)!\n Fehler: " + gdir.getStatus().code);
	else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
		alert("Adresse vollständig angeben!\n Fehler: " + gdir.getStatus().code);
	else if (gdir.getStatus().code == G_GEO_BAD_KEY)
		alert("Google Maps API Key nicht gültig! Bitte nicht kopieren! Key kann auf http://www.google.com/apis/maps/signup.html beantragt werden! \n Fehler: " + gdir.getStatus().code);
	else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
		alert("Fehler bei der Berechnung. Bitte nochmal versuchen!\n Fehler: " + gdir.getStatus().code);
	else alert("Unbekannter Fehler!\n Fehler: " + gdir.getStatus().code);
}

function onGDirectionsLoad() {
	//Um an Informationen der load()-Funktion heranzukommen.
	//Bei Nichtverwendung nicht löschen!
}

function Route( address, latitude, longitude ) {
	if( geocoder ) {
		geocoder.getLatLng (
			address,
			function( point ) {
				if( !point ) {
					alert(address + " not found");
				} else {
					setDirections( point.y + ", " + point.x, latitude + ", " + longitude, "de_DE" );
				}
			}
		);
	}
}
