function loadMap() {
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("map"));
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(50.9428723,6.9496014), 15, G_NORMAL_MAP);
		map.setMapType(G_NORMAL_MAP);
		markerSMP = new GMarker(map.getCenter());
		GEvent.addListener(markerSMP, "click", function() {
			var html = '<b><big>Struktur Management Partner GmbH<\/big><\/b><br\/>Gereonstraße 18-30<br\/>50670 Köln';
			markerSMP.openInfoWindowHtml(html);
		});
		map.addOverlay(markerSMP);
		//GEvent.trigger(markerSMP, "click");
		
		gdir = new GDirections(map, document.getElementById("directions"));
		GEvent.addListener(gdir, "load", onGDirectionsLoad);
		GEvent.addListener(gdir, "error", handleErrors);
	}
}
function setDirections(fromAddress, toAddress, locale) {
	$('#directions').html('');
	gdir.load("from: " + fromAddress + " to: " + toAddress,
		{ "locale": locale });
}

function handleErrors(){
	if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	$('#directions').html("<p>Die angegebene Adresse wurde nicht gefunden.</p>");
	else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	$('#directions').html("<p>Bei der Bearbeitung Ihrer Anfrage ist ein Fehler aufgetreten. Bitte versuchen Sie es in Kürze noch einmal.</p>");
	else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	$('#directions').html("<p>Bei der Bearbeitung Ihrer Anfrage ist ein Fehler aufgetreten. Bitte versuchen Sie es in Kürze noch einmal.</p>");
	else if (gdir.getStatus().code == G_GEO_BAD_KEY)
	$('#directions').html("<p>Bei der Bearbeitung Ihrer Anfrage ist ein Fehler aufgetreten. Bitte versuchen Sie es in Kürze noch einmal.</p>");
	else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	$('#directions').html("<p>Bei der Bearbeitung Ihrer Anfrage ist ein Fehler aufgetreten. Bitte versuchen Sie es in Kürze noch einmal.</p>");
	else if (gdir.getStatus().code == G_GEO_UNKNOWN_DIRECTIONS)
	$('#directions').html("<p>Bei der Bearbeitung Ihrer Anfrage ist ein Fehler aufgetreten. Bitte wählen sie eine genauere Ortsangabe.</p>");
	else $('#directions').html("<p>Bei der Bearbeitung Ihrer Anfrage ist ein Fehler aufgetreten. Bitte versuchen Sie es in Kürze noch einmal.</p>");

	$('#print-directions').hide();
}

function onGDirectionsLoad(){ 
	// Use this function to access information about the latest load()
	// results.
	// e.g.
	// document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
	// and yada yada yada...
	if(gdir.getStatus().code==G_GEO_SUCCESS) {
		$('#print-directions').show();
	} else {
		$('#print-directions').hide();
	}
}

$(function() {
	loadMap();
	$('#print-directions').click(function() {
		param = $('#contact-map form').serialize();
		window.open('/'+$('#locale').val()+'/directions/?'+param, 'map', 'width=950,height=600,toolbar=0,menubar=0,scrollbars=1,location=0,status=0');
		return false;
	});
	
	$('#print-directions').hide();
	//setTimeout("$('#contact-map').css('visibility', 'visible')", 550);
	//setTimeout("$('#contact-map').hide();", 500);
});
