$(document).ready(function() {
	
	$(".GoogleMaps").each(function(i) {
		googlemapgeocoder = new google.maps.Geocoder();
		
		var attrs = $(this).attr('rel').split(';');
		googlemapurl = attrs[0];
		var mapcenteraddr = attrs[1];
		var mapzoom = parseInt(attrs[2]);
		if (mapcenteraddr==undefined) {mapcenteraddr = 'Bayreuth';}
		if (isNaN(mapzoom)) {mapzoom = 9;}
		googlemaport = mapcenteraddr;
		
		if (googlemapgeocoder) {
			googlemapgeocoder.geocode( { 'address': mapcenteraddr+', Germany'}, function(results, status) {
				if (status == google.maps.GeocoderStatus.OK) {
					var myOptions = {
							zoom: mapzoom,
							center: results[0].geometry.location,
							mapTypeId: google.maps.MapTypeId.ROADMAP
							//mapTypeId: google.maps.MapTypeId.HYBRID
					};
					googlemap = new google.maps.Map($(".GoogleMaps")[i], myOptions);
				}
			});
			googlemapinfowindow = new google.maps.InfoWindow();
		}
				
	});
	
	
	$('div.GoogleMapsOverlayLine div.button input[type="checkbox"]').click(function () {
		if ($(this).is(':checked')) {
			if (!googlemapOverlays[$(this).attr('id')]) {
				googlemapOverlays[$(this).attr('id')] = [];
			}
			$.ajax({
			      type: 'GET',
			      url: '/google-maps/'+googlemapurl,
			      data: { id: $(this).attr('id'), ort: googlemaport },
			      dataType: "script"
			});
		} else {
			googlemapDeleteOverlay($(this).attr('id'));
		}
	});
	
	$('h4.GoogleMapsPullDown').click(function() {
		$('div.panelpulldown').slideToggle("slow");
	});
	$('div.panelpulldown div.button input[type="checkbox"]').click(function () {
		$('div.panelpulldown').slideToggle("slow");
	});
	
});
var googlemapgeocoder;
var googlemap;
var googlemapurl;
var googlemaport;
var googlemapOverlays = [];
var googlemapinfowindow;

function googlemapOpenInfoWindow(cont, gmap, marker) {
	googlemapinfowindow.close();
	googlemapinfowindow.setOptions({
	    content: cont
	});
	googlemapinfowindow.open(gmap,marker);
}

function googlemapActivateOverlay (ov) {
	if (!googlemapOverlays[ov]) {
		googlemapOverlays[ov] = [];
	}
	$.ajax({
		type: 'GET',
		url: '/google-maps/'+googlemapurl,
		data: { id: ov, ort: googlemaport },
		dataType: "script"
	});
	$('div.GoogleMapsOverlayLine div.button input[type="checkbox"]#'+ov).attr('checked', 'checked');
}

function googlemapAddOverlay (n, a) {
	if (googlemapOverlays[n].length > 0) {
		for (i in googlemapOverlays[n]) {
			googlemapOverlays[n][i].setMap(googlemap);
		}
	} else {
		googlemapOverlays[n].length = 0;

		if (googlemapgeocoder) {
			for (var i in a) {
				googlemapAddMarker(n, a[i]);
				
			}
		}
	}
}
function googlemapAddMarker(n, a) {
	if (a[4] > 0 && a[5] > 0) {
		var marker = new google.maps.Marker({
			position: new google.maps.LatLng(a[4], a[5]),
			icon: a[2],
			map: googlemap
		});
		if (a[3]!=undefined) {
			//var infowindow = new google.maps.InfoWindow({content: a[3]});
			google.maps.event.addListener(marker, 'click', function() {
				//infowindow.open(googlemap,marker);
				googlemapOpenInfoWindow(a[3], googlemap, marker);
			});
		}
		googlemapOverlays[n].push(marker);
	} else {
		googlemapgeocoder.geocode( { 'address': a[1]}, function(results, status) {
			if (status == google.maps.GeocoderStatus.OK) {
				var marker = new google.maps.Marker({
					position: results[0].geometry.location,
					icon: a[2],
					map: googlemap
				});
				if (a[3]!=undefined) {
					//var infowindow = new google.maps.InfoWindow({content: a[3]});
					google.maps.event.addListener(marker, 'click', function() {
						//infowindow.open(googlemap,marker);
						googlemapOpenInfoWindow(a[3], googlemap, marker);
					});
				}
				googlemapOverlays[n].push(marker);

				$.ajax({
					type: 'GET',
					url: '/google-maps/gpssave',
					data: { id: n, did: a[0], lat: results[0].geometry.location.lat(), lng: results[0].geometry.location.lng() }
				});

			} else if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
				setTimeout(function() { googlemapAddMarker(n, a); }, 100);
/*			} else if (status == google.maps.GeocoderStatus.ZERO_RESULTS) {
$(".GoogleMapsDebug").append('Zero ERROR->'+a[0]+':'+a[1]+' ('+a[4]+' | '+a[5]+')<br>');
			} else if (status == google.maps.GeocoderStatus.REQUEST_DENIED) {
$(".GoogleMapsDebug").append('Denied ERROR->'+a[0]+':'+a[1]+' ('+a[4]+' | '+a[5]+')<br>');
			} else if (status == google.maps.GeocoderStatus.INVALID_REQUEST) {
$(".GoogleMapsDebug").append('Invalid ERROR->'+a[0]+':'+a[1]+' ('+a[4]+' | '+a[5]+')<br>');
			} else {
$(".GoogleMapsDebug").append('Unknown ERROR->'+a[0]+':'+a[1]+' ('+a[4]+' | '+a[5]+')<br>');
*/
			}
		});
	}
}
function googlemapDeleteOverlay(n) {
	if (googlemapOverlays[n]) {
		for (i in googlemapOverlays[n]) {
			googlemapOverlays[n][i].setMap(null);
		}
	}
}


