var map, cluster, eventListeners=[], markersArray=[], icon;
var auswstatus=0;

function toggleauswmap (single, id) {
	//take care when changing! is used by List and Eventsite...
	//if single==1 --> show single regatta only
	if(auswstatus==0) {
		auswstatus=1;
		document.getElementById('contauswmap').style.display='';
		document.getElementById('toggleauswkarte').innerHTML='ausblenden';
		myOnload(single,id);
	}
	else {
		auswstatus=0;
		document.getElementById('contauswmap').style.display='none';
		document.getElementById('toggleauswkarte').innerHTML='einblenden';
		GUnload();
	}
}

function myCloseInfoWindow () {
	map.closeInfoWindow();
}

function myOnload(single, id) {
	if (GBrowserIsCompatible()) {
		map=new GMap2(document.getElementById('contauswmap'));
		map.setMapType(G_HYBRID_MAP);
		map.enableScrollWheelZoom();

		map.setCenter(new GLatLng(51.04139389812637, 10.4150390625), 5);

		map.addControl(new GMapTypeControl());
		map.addControl(new GLargeMapControl());
		map.addControl(new GOverviewMapControl());

		GEvent.addListener(map, 'zoomend', function() { map.closeInfoWindow(); });

		if(single!='1') {
			var html='<div style="border:solid black 1px; background-color:white; color:black; padding:0px 3px 3px 3px">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(73,7)));
		}

		
		function myClusterClick(args) {
			cluster.defaultClickAction=function(){
				var temp_zoom=map.getBoundsZoomLevel(args.clusterMarker.clusterGroupBounds);
				if(temp_zoom>17)
					temp_zoom=17;
				map.setCenter(args.clusterMarker.getLatLng(), temp_zoom);
				delete cluster.defaultClickAction;
			}
			var html='<div style="height:8em; overflow:auto; width:24em;" align="left"><span class="help"><b>'+args.clusteredMarkers.length+' Events:</b>';
			for (i=0; i<args.clusteredMarkers.length; i++) {
				html+='<br /><a href="javascript:cluster.triggerClick('+args.clusteredMarkers[i].index+')">Zoom in</a> | ';
				html+='<a href="http://maps.google.de/maps?t=h&q='+args.clusteredMarkers[i].getPoint().toString()+'&hl=de" target="_blank"><img src="./gifs/GMap_glogo2.png" border="0"><img src="./gifs/GMap_link.gif" border="0"></a> | '+args.clusteredMarkers[i].getTitle();
			}
			html+='<br />[<a href="javascript:void(0)" onclick="cluster.defaultClickAction()">In dieses Cluster rein zoomen</a>]</span></div>';
			//	args.clusterMarker.openInfoWindowHtml(html);
			map.openInfoWindowHtml(args.clusterMarker.getLatLng(), html);
		}
		
		//	create a ClusterMarker
		//cluster=new ClusterMarker(map, {clusterMarkerTitle:'Click to see info about %count locations' , clusterMarkerClick:myClusterClick });
		cluster=new ClusterMarker(map, {clusterMarkerTitle:'Weitere Infos zu diesem Cluster' , clusterMarkerClick:myClusterClick });
		
		icon=new GIcon();
		icon.shadow='http://www.raceoffice.org/gifs/GMap_single.png';
		icon.iconSize=new GSize(15, 15);
		icon.iconAnchor=new GPoint(9, 32);
		icon.infoWindowAnchor=new GPoint(9, 32);

		//icon.shadowSize=new GSize(35, 32);
		
		selectExample(single,id);
	}
}

function newMarker(markerLocation, title, markerIcon, address) {
	var marker=new GMarker(markerLocation, {title:title, icon:markerIcon});
	//var marker=new GMarker(markerLocation, {title:title});
	eventListeners.push(GEvent.addListener(marker, 'click', function() {
		marker.openInfoWindowHtml('<p>'+title+'</p><p style="font-size:8pt; color:grey;">'+address+'</p><p><a href="http://maps.google.de/maps?t=h&q='+marker.getPoint().toString()+'&hl=de" target="_blank"><img src="./gifs/GMap_glogo.png" border="0"></a><br><span class="help">(Routenplanung uvm.)</span></p>');
	}));
	return marker;
}

function toggleClustering() {
	cluster.clusteringEnabled=!cluster.clusteringEnabled;
	cluster.refresh(true);
}

function selectExample(single,id){
	processIt=function(file, code){
		if(code===200){
			markersArray=[];
			for(i=eventListeners.length-1; i>=0; i--){
				GEvent.removeListener(eventListeners[i]);
			}
			
			eventListeners=[];

			eval(file);
			
			var marker, newIcon, j=1, title, lat, lng;

			for (var i=0; i<json.length; i++) {
				//newIcon=new GIcon(icon, 'http://www.raceoffice.org/gifs/GMap_single.png');
				newIcon=new GIcon(icon);
				lat=Math.round(json[i].lat*100)/100;
				lng=Math.round(json[i].lng*100)/100;
				//title='id: '+i+', ('+lat+', '+lng+')';
				title='<a href="http://www.raceoffice.org/event.php?eid='+json[i].id+'" target="_blank">';
				title=title+json[i].name+'<img src="./gifs/GMap_link.gif" border="0"></a>';
				marker=newMarker(new GLatLng(json[i].lat, json[i].lng), title, newIcon, json[i].address);

				markersArray.push(marker);
				j++;
				if (j>26) {
					j=1;
				}
			}
			
			cluster.removeMarkers();
			cluster.addMarkers(markersArray);
		//	cluster.fitMapToMarkers(); //fit map to selected markers
			cluster.refresh(); // we do a refresh instead of fitmapto markers
			map.savePosition();
			json=[];
		} else {
			GLog.write('Marker data retrieval failed. Error code: '+code);
		}
	};
	if(single=='1'){
		GDownloadUrl('./map.regatten.php?single=1&id='+id+'', processIt);
	}else{
		GDownloadUrl('./map.regatten.php', processIt);
	}
	
	//var json=[]; json[0]={'id':1, 'lat':52.149549, 'lng':1.60221 }; json[1]={'id':2, 'lat':52.7403984069824, 'lng':0.514689087867737 }; json[2]={'id':3, 'lat':52.7369802416371, 'lng':0.518964529037476 }; 
}

