//Event.observe(window, 'unload', GUnload, false);

var map = null;
var geocoder = null;
var bounds = null;
var currentmarker = null;

function initGoogleMaps(type) {
	if (typeof type != 'undefined')
	{
		var thisType = google.maps.MapTypeId.HYBRID;
		switch(type)
		{
			case 'normal':
				thisType = google.maps.MapTypeId.ROADMAP; break;
			case 'satellite':
				thisType = google.maps.MapTypeId.SATELLITE ; break;
		}
	}
	else
	{
		var thisType = google.maps.MapTypeId.ROADMAP;		
	}
    var latlng = new google.maps.LatLng(48.306074,14.286293);
	var myOptions = {
		zoom: 8,
		center: latlng,
		// navigationControl: true,
		// mapTypeControl: true,
		scaleControl: false,
		mapTypeId: thisType
	};
	map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	geocoder = new google.maps.Geocoder();
	bounds = new google.maps.LatLngBounds();
	
	// var trafficLayer = new google.maps.TrafficLayer();
	// trafficLayer.setMap(map);
	
}
function initSmallGoogleMaps(type) {
	initGoogleMaps(type);
}

function loadMarkers(adr)
{
	adr.each(
		function(elem)
		{
			markAddress(elem[0],elem[1]);
		}
	);
	window.setTimeout('centerViewport()',1000);
}
function markAddress(address,txt)
{
	if (address.startsWith('('))	// bereits vorbereitete koordinaten
	{
		klammerAuf = /\(/;
		klammerZu = /\)/;
		coords = address.replace(klammerAuf,'').replace(klammerZu,'').split(', ');
		address = new google.maps.LatLng(coords[0],coords[1]);
		var marker = createMarker(address,txt);
	}
	else if (geocoder)
	{
		geocoder.geocode( { 'address': address}, function(results, status)
		{
			if (status == google.maps.GeocoderStatus.OK)
			{
				map.setCenter(results[0].geometry.location);
				var marker = createMarker(results[0].geometry.location,txt);
				new Insertion.Bottom('latLng','<div class="dbg">'+txt+'<p>'+results[0].geometry.location+'</p></div>');
			}
			else
			{
				new Insertion.Bottom('debug','<div class="dbg">'+txt+'<p>'+status+'</p></div>');
			}
		});
	}
}
function createMarker(latLng,txt) {
	var marker = new google.maps.Marker({
	    position: latLng,
	    map: map
	});
	bounds.extend(latLng);
	if (txt != undefined && txt != '')
	{
		var infowindow = new google.maps.InfoWindow({
		    content: txt
		});
		google.maps.event.addListener(marker, 'click', function() {
		  infowindow.open(map,marker);
		});
	}
	return marker;
}
function centerViewport()
{
	if (!bounds.isEmpty())
		map.fitBounds(bounds);
	var zoomLevel = map.getZoom();
	if (zoomLevel > 17) map.setZoom(17);
}

