var map = null;
var defaultZoom = 14;
var nearZoom = 18;

 /**
 * Initializes the map and the meta markers
 */
 function loadMap(mapElementId) {
     if (GBrowserIsCompatible()) {
         // create a GMap2 
         map = new GMap2(document.getElementById( mapElementId ? mapElementId : "map") );
         // add direction and zoom controls
         map.addControl(new GLargeMapControl());
         // add switches for map type
         map.addControl(new GMapTypeControl());
         map.addControl(new GScaleControl());
         // center first marker
         if ( window.centerPoint == null ) {
            map.setCenter(new GLatLng(47.49974961759944, 8.724431991577148), defaultZoom);
         } else {
            map.setCenter(new GLatLng(window.centerPoint.lat, window.centerPoint.lng ), defaultZoom);
         }
         // init meta markers to display points of interest
         initMetaDataMarkers();
     }
}
 
 /**
  * centers the map to lat/lon. If no zoom is provided the map pans to the center. If the zoom
  * is set map.setCenter is used.
  *
  * @param lat the latitude of the center
  * @param lon the longitude of the center
  * @param zoom? optional zoom argument
  */
 function centerMap( lat, lon , zoom) {
    if ( !zoom ) {
        map.panTo( new GLatLng(lat, lon) );
    } else {
        map.setMapType( G_SATELLITE_MAP );
        map.setCenter( new GLatLng(lat, lon), zoom );
    }
 }
 
 /**
  * sets the zoom level
  *
  * @param zoomLevel the level to set
  */
 function setZoom( zoomLevel ) {
    map.setZoom( zoomLevel );
 }
 
 if ( window.addEventListener ) {
  window.addEventListener( "load", function() { loadMap(); } , false );
 } else {
   window.onload = function() { loadMap(); };
 }
 
function addMarker( lat, lon , title, categoryTitle, text , link ) {
    var marker_tip = "<div>"; 
        marker_tip += '<p class="map-window-title"><b>' + title + '</b></p>'; 
        if ( categoryTitle != null ) {
            marker_tip += '<p class="map-window-infoline">'; 
                marker_tip += '<i>Kategorie:</i> <a href="pub/fw.action/search?q=&ce_name=Building">' + categoryTitle; 
            marker_tip += '</a></p>'; 
        }
        marker_tip += '<p class="map-window-param">';
        marker_tip += text + '</p>';
        marker_tip += '<p class="map-window-link">';
        if ( link ) {
            marker_tip += '[<a href="' + link + '">Zum Artikel</a>]';
        }
        marker_tip += ' [<a href="javascript:centerMap( ' + lat + ',' + lon + ')">Zentrieren</a>]';
        marker_tip += ' [<a href="javascript:centerMap( ' + lat + ',' + lon + ' , ' + nearZoom + ')">Nahansicht</a>]';
        marker_tip += '</p>';
    marker_tip += "</div>"; 
    
    var marker = createMarker( lat, lon, marker_tip, title );
    map.addOverlay( marker );  
}
 
  /**
  * creates a marker conveniently
  * 
  * @param lat the latitude of the marker
  * @param lon the longitude of the marker
  * @param windowHtmlString the html to be displayed in the info window
  * @param color? the color of the marker as a string (red, green, blue)
  * @return the marker created
  */
  function createMarker(lat, lon, windowHtmlString, title) {
      var point = new GLatLng( lat, lon );
      var marker = null;
      if ( title ) {
        var opts = new Object();
        opts.title = title;
        marker = new GMarker(point, opts);
      } else {
        marker = new GMarker(point);
      }
      GEvent.addListener(marker, "click", function() {
      marker.openInfoWindowHtml( windowHtmlString );
      });    
      return marker;
  }
