// JavaScript Document

var map, cluster;
var test;

	function myOnLoad() {
		if (GBrowserIsCompatible()) {
			map=new GMap2(document.getElementById('map'));
			map.setCenter(new GLatLng(51, 10), 5, G_NORMAL_MAP);
			
			map.addControl(new GMapTypeControl(), new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 7)));
			map.addControl(new GLargeMapControl(), new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 28)));
			
		}
	}
	
	function myOnLoadSingle(latLng, text) {
		if (GBrowserIsCompatible()) {
			map=new GMap2(document.getElementById('map'));
			map.setCenter(new GLatLng(51, 10), 5, G_NORMAL_MAP);
			
			map.addControl(new GMapTypeControl(), new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 7)));
			map.addControl(new GLargeMapControl(), new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 28)));
			marker = newMarker(latLng, text, text);
			map.addOverlay(marker);
			map.setCenter(marker.getLatLng());
			//map.refresh();
				//setMarkers();
                //map.setCenter(new GLatLng(51, 10), 4, G_NORMAL_MAP);
		}
	}
	
	//Termin anzeigen
	function myOnLoadTermin(addy, text) {
		
		if (GBrowserIsCompatible()) {
			map=new GMap2(document.getElementById('map'));
			map.setCenter(new GLatLng(51, 10), 5, G_NORMAL_MAP);
			
			map.addControl(new GMapTypeControl(), new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 7)));
			map.addControl(new GLargeMapControl(), new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 28)));
			
			lat = 0;
			lon = 0;
			gc = new GClientGeocoder();
			
			gc.getLatLng(addy, function(point) {
												if(!point){
													//alert("PLZ nicht gefunden");
													lat = 50.00285;
													lon = 8.21105;
													$("#map").hide();
												}
												else{
													lat = point.lat();
													lon = point.lng();
													
													marker = new GMarker(new GLatLng(lat,lon), {title:text});
													map.addOverlay(marker);
													map.setCenter(marker.getLatLng());
													
													GEvent.addListener(marker, 'click', function() {
																				routing = "<p><a target='_blank' href='http://maps.google.com/maps?daddr="+lat+","+lon+" ("+addy+")'>Route planen</a></p>"
																				marker.openInfoWindowHtml('<p>'+text+'<br />'+addy+'</p>'+routing);
																								 });
												}
										});
			
			
			
			//map.refresh();
				//setMarkers();
                //map.setCenter(new GLatLng(51, 10), 4, G_NORMAL_MAP);
		}
	}


	function setMarkers(){
		
		var marker, markersArray=[];
		
		for (var i=0; i<json.length; i++) {
			marker=newMarker(new GLatLng(json[i].lat, json[i].lng), json[i].treffname, json[i].mTitle);
			markersArray.push(marker);
			//marker = newMarker(newGLatLng(lat,lon),"Treffname");
			//alert(map.getZoom());
		}
		
		
		cluster = new ClusterMarker(map, { markers:markersArray });
		
		cluster.fitMapToMarkers();
		
		map.savePosition();	//	enables the large map control centre button to return the map to initial view
		
		
		//	add an HtmlControl to enable toggling of the ClusterMarker cluster function
		//	see http://googlemapsapi.martinpearman.co.uk/htmlcontrol for more info on HtmlControl
		
		/*var html='<div class="htmlControl" style="padding:0px 3px 3px 3px">Enable clustering: <input type="checkbox" checked="checked" onclick="toggleClustering()" /></div>';
		var control = new HtmlControl(html);
		map.addControl(control, new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(7,7)));*/
				
	}
	
	function newMarker(markerLocation, markerId, mTitle) {
		
		//alert("id: "+markerId);
		if(markerLocation.lat()==0 && markerLocation.lng()==0){
			$("#map").hide();
			return false;
		}
		var marker=new GMarker(markerLocation, {title:mTitle});
		
		GEvent.addListener(marker, 'click', function() {
													 //<a href='http://maps.google.com/maps?daddr="+markerLocation.lat()+","+markerLocation.lon()+"'>
													routing = "<p><a target='_blank' href='http://maps.google.com/maps?daddr="+markerLocation.lat()+","+markerLocation.lng()+" ("+mTitle+")'>Route planen</a></p>"
													marker.openInfoWindowHtml('<p>'+markerId+'</p>'+routing);
												});
		return marker;
	}
	
	function toggleClustering() {
		cluster.clusteringEnabled=!cluster.clusteringEnabled;
		cluster.refresh(true);	//	true required to force a full update of the markers - otherwise the update would occur next time that the map is zoomed or the active markers change
	}
	
	
	// JavaScript Document
