var directionDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
var geocoords_lat;
var geocoords_lng;
var marker;
var infowindow;
var destination_string = 'Wiesengrund 5, 25495 Kummerfeld';

function str_replace(search_str, replace_str, string)
{
	return string.split(search_str).join(replace_str);
}

function initialize() 
{
	directionsDisplay = new google.maps.DirectionsRenderer();
	geocoder = new google.maps.Geocoder();
	
	geocoder.geocode({'address': destination_string}, function(results, status){
		console.log(results[0].geometry.location);
		geocoords_lat = results[0].geometry.location.lat();
		geocoords_lng = results[0].geometry.location.lng();
		if(status == google.maps.GeocoderStatus.OK)
		{
			map = new google.maps.Map(document.getElementById("gmap"), {
				zoom: 9,
				center: new google.maps.LatLng(geocoords_lat + 0.1,geocoords_lng),
				mapTypeId: google.maps.MapTypeId.ROADMAP
			});
			directionsDisplay.setMap(map);
			marker = new google.maps.Marker({
				map: map, 
				position: results[0].geometry.location,
				title: 'Routenberechnung'
			});
			
			infowindow = new google.maps.InfoWindow({
				content: '<div id="form_container_route"><table border="0" cellpadding="0" cellspacing="0"><tr><td colspan="2"><strong>Route</strong></td></tr><tr><td>Von:</td><td><input type="text" name="start" id="start" /></td></tr><tr><td>Nach:&nbsp;</td><td>' + destination_string + '</td></tr><tr><td colspan="2">&nbsp;</td><tr><td colspan="2"><input type="button" value="berechnen" onclick="calcRoute()" /></td></tr></table></div>'
			});
			infowindow.open(map,marker);
		}
		else
		{
			alert("Geocode was not successful for the following reason: " + status);
		}
	});
}

function calcRoute()
{	
	var start = document.getElementById("start").value;
	var end = destination_string;
	directionsService.route({
		origin: start, 
		destination: end,
		travelMode: google.maps.DirectionsTravelMode.DRIVING,
		unitSystem: google.maps.DirectionsUnitSystem.METRIC
	}, function(result, status){
		duration_seconds = result.routes[0].legs[0].duration.value;
		duration_hours_float = duration_seconds / 3600;
		duration_hours_arr = duration_hours_float.toString().split(".");
		
		distance_final = Math.round(((result.routes[0].legs[0].distance.value / 1000) * 100) / 100) + ' km';
		duration_final = duration_hours_arr[0] + ' Std. ' + Math.round((duration_seconds - (duration_hours_arr[0] * 3600)) / 60) + ' Min.';
		
		if(status == google.maps.DirectionsStatus.OK)
		{
			infowindow.close();
			infowindow = new google.maps.InfoWindow({
				content: '<div id="form_container_route"><table border="0" cellpadding="0" cellspacing="0"><tr><td colspan="2"><strong>Route</strong></td></tr><tr><td>Von:</td><td><input type="text" name="start" id="start" value="' + start + '" /></td></tr><tr><td>Nach:&nbsp;</td><td>' + destination_string + '</td></tr><tr><td>Entfernung:&nbsp;&nbsp;</td><td>' + distance_final + '</td></tr><tr><td>Dauer:</td><td>' + duration_final + '</td></tr><tr><td colspan="2">&nbsp;</td><tr><td><input type="button" value="berechnen" onclick="calcRoute()" /></td><td><input type="button" value="Routendetails" onclick="window.open(\'http://maps.google.de/maps?saddr=' + start + '&daddr=Wiesengrund+5,+25495+Kummerfeld&hl=de\'); return false;" /></td></tr></table></div>'
			});
			infowindow.open(map,marker);
			
			directionsDisplay.setDirections(result);
		}
	});
}
google.maps.event.addDomListener(window, 'load', initialize);
