 var map = null;
 var geocoder = null;
 var markers = new Array();
 var markerCluster = null;
 var openmarker = false;

 window.onload = initialize;
 window.onunload = GUnload;

  function showAddress(addr) {

   //wait for markercluster and geocoder to initialize
   if (markerCluster != null && geocoder) {

		  geocoder.getLatLng(addr, function(point) {
				if (!point) alert(addr + ' not found');
				else map.setCenter(point, 11);
		  });
   }

 }	

function initialize() {
	new Ajax.Request('/peejax.php',
	{
		method: 'get',
		onSuccess: function(transport) {
			p_resp = transport.responseText;
			ginit();
		},
		onFailure: function() {alert('Cannot Download Points');}

	});

}
function ginit() {


	if (GBrowserIsCompatible()) { 
		map = new GMap2(document.getElementById("map_canvas")); 
		map.addControl(new GLargeMapControl()); 
		map.addControl(new GMapTypeControl()); 
		// Dieser Teil (Kartenmittelpunkt) dynamisch
		map.setCenter(new GLatLng(22.785,5.522778), 1); 
		map.enableScrollWheelZoom();
		geocoder = new GClientGeocoder();
		
		//the standard icon that should be shown as all other markers is located here http://www.gratisparken.de/images/pin.png
		var blu_icon = new GIcon();
		blu_icon.image = "http://server.zoerb.net/freepee/images/freepeemarker.png";
		blu_icon.iconSize = new GSize(35, 35);
		blu_icon.iconAnchor = new GPoint(0, 0);
		blu_icon.infoWindowAnchor = new GPoint(8, 11);
		//create the array for the other points lat, long and text

		var args = p_resp.split('\n');
		var item = null;
		var marker = null;
		var point = null;
		for (var i = 0; i < args.length; i++ )
		 {
 			item = args[i].split(',');
		
			point = new GLatLng(item[0],item[1]);
			marker = new GMarker(point, {icon: blu_icon});
			createListener(marker, "<div style='width: 300px;'><b>"+item[2]+"</b><br>"+item[3]+"<br><a href='http://www.freepee.org/toilet/detail_"+item[4]+".html'>View detail page and rate this toilet</a></div>");
			markers.push(marker); 

		} 
		markerCluster = new MarkerClusterer(map, markers);

   GEvent.addListener(map, "moveend", function() {
	   if (openmarker)
	   {
		   openmarker = false;
		   return;
	   }
          markerCluster.clearMarkers();
          markerCluster.addMarkers(markers);
     }); 

	}

}
 
   function createListener (marker, text) { 
    GEvent.addListener(marker, "click", function() { 
			 openmarker = true;
			 marker.openInfoWindowHtml(text);

       }); 
  } 