$(document).ready(function(){
		window.googlemaps()?googlemaps():null;
	});
function googlemaps() {
	if(document.getElementById("map")){
		var mapcenter=document.getElementById('mapcenter').innerHTML;
		var locations=document.getElementById('locations').innerHTML;
		var xmlfile = (document.getElementById('xmlfile')?document.getElementById('xmlfile').innerHTML:'');
			

		var mapzoom=parseInt(document.getElementById('mapzoom').innerHTML!=undefined?document.getElementById('mapzoom').innerHTML:8);
		if(!(mapzoom>0)){mapzoom=8;}
		 if (GBrowserIsCompatible()) {
			var map = new GMap2(document.getElementById("map"));
			map.addControl(new GSmallZoomControl());
			map.enableDoubleClickZoom();
			var geocoder = new GClientGeocoder();
			function showAddress(address,url) {
				  geocoder.getLatLng(
					address,
					function(point) {
					  if (!point) {

					} else {
						var marker = new GMarker(point);
						  GEvent.addListener(marker, "click", function() {
						   window.location=(url);
						  });

						map.addOverlay(marker);
					  }
					}
				  );
				}
			function createMarker(point,name,html) {
				var marker = new GMarker(point);
				GEvent.addListener(marker, "click", function() {
				  marker.openInfoWindowHtml(html);
				});
				// save the info we need to use later for the side_bar

				return marker;
			  }

			function centerMap(mapcenter){
				//if($('#debug').text()=='true')
					/* check if mapcenter is a string or a bunch of coordinates of the
					 * (1.2345678,9,0123456) format */
					var coordinateFormat = /\(([0-9.]+),([0-9.]+)\)/;
					var regexmatch = coordinateFormat.exec(mapcenter);
					//console.log(regexmatch);
					if(regexmatch!=null){
						map.setCenter(new GLatLng(regexmatch[1],regexmatch[2]),mapzoom);
					}else{
						/* the geocoder is (relatively) slow, so we center the map on some arbitrary
						 * spot first because for some reason google maps can't add markers before 
						 * the map is centered someplace.
						 * */
						map.setCenter(new GLatLng(0,0),mapzoom);
						geocoder.getLatLng(
							mapcenter,function(point){ if (!point) {
								//document.getElementById("map").style.display='none'; /* address not found, hide map */
									} else {
										map.setCenter(point, mapzoom);
									  }
								}
							);
						}
					}
			centerMap(mapcenter);
			/* to do: use lat/lon instead of addresses if possible to avoid multiple geocode requests */
			locations=locations.split('|');
			for(var i=0;i<locations.length;i++){
				var locationprops=locations[i].split(';');
				var url = locationprops[1];
				/* check if locationprops[0] looks like coordinates */
				if(locationprops[0].match(/^\([0-9.-]+,[0-9.-]+\)$/)){
					/* extract the coordinates */
					var latlong=locationprops[0].substring(1,locationprops[0].length-1);
					latlong=latlong.split(',');
					var point = new GLatLng(latlong[0],latlong[1]);
					var marker = new GMarker(point);
					marker.url=url;
					  GEvent.addListener(marker, "click", function() {							
							//$('#balloon').load('/xml/test.lasso?id='+this.url);
						  });
					map.addOverlay(marker);
					
				}else{
					showAddress(locationprops[0],url);
				}
			}
			if(xmlfile!=''){
			 GDownloadUrl(xmlfile, function(doc) {
				var xmlDoc = GXml.parse(doc);
				var markers = xmlDoc.documentElement.getElementsByTagName("marker");
			  
				for (var i = 0; i < markers.length; i++) {
				  // obtain the attributes of each marker
				  var lat = parseFloat(markers[i].getAttribute("lat"));
				  var lng = parseFloat(markers[i].getAttribute("lng"));
				  var point = new GLatLng(lat,lng);

				  var url = markers[i].getAttribute("url");
				  var orgname= markers[i].getAttribute("name");
				  var location= markers[i].getAttribute("location");
				  var html = '<p style="margin-top: 5px;"><a href="'+url+'" style="font-weight: bold;">'+orgname+'</a><br />'+location+'</p>';
				  var label = markers[i].getAttribute("label");
				  // create the marker
				  var marker = createMarker(point,label,html);
				  
				  map.addOverlay(marker);
				}
				// put the assembled side_bar_html contents into the side_bar div
				//document.getElementById("side_bar").innerHTML = side_bar_html;
			  });
	  		}

			//showAddress(address);
		 }
	  }
    }

