var objMap = null;
var geo = null;
var bounds = null;

function startMap(mapId, width, height, arInfo) {
  if (GBrowserIsCompatible()) {
    geo = new GClientGeocoder();
    var m = document.getElementById(mapId);
    m.style.width = width;
    m.style.height = height;
    objMap = new GMap2(m);		
		
    bounds = new GLatLngBounds();

    objMap.addControl(new GMapTypeControl(1));
    objMap.addControl(new GLargeMapControl());
    objMap.setCenter(bounds.getCenter(), objMap.getBoundsZoomLevel(bounds), G_HYBRID_MAP);		
//    objMap.setMapType(G_HYBRID_MAP);		
    objMap.clearOverlays();
   
    if ((arInfo.point.lat() != 0) || (arInfo.point.lng() != 0)) {
	  // vast punt opgegeven:
      addPoint(arInfo.point, arInfo);
	}
	else if (geo) {
      getPoint(arInfo);
    }
  }
}

function getPoint(arInfo) {
  geo.getLatLng(arInfo.addr, function(point) {
    if (!point) { // address not found
      alert(arInfo.addr + " Niet gevonden"); // alert

      return null;
    }
    else {
      arInfo.point = point;
      addPoint(point, arInfo);
      return point;
    }
  })
}

function addPoint (point, arInfo) {
  bounds.extend(point);
  var marker = createInfoMarker(point, arInfo.text);
  objMap.addOverlay(marker);

  marker.openInfoWindowHtml(arInfo.text);
  objMap.setCenter(bounds.getCenter(), objMap.getBoundsZoomLevel(bounds)-1);
}

function createInfoMarker(point, text) {
   var marker = new GMarker(point);

   GEvent.addListener(marker, "click",
      function() {
         marker.openInfoWindowHtml(text);
      }
   );

  return marker;
}


