/**
 * Holds the Google map. Global so that initMap() can read it.
 */
var googleMap = null;


/**
 * This function initializes the Google Maps functionality.
 * 
 * Attention: The first operation before adding overlays to the map HAS TO BE
 * centering it somewhere. If you do not center the map before adding
 * overlays, javascript errors will occur.
 */
function load(argXMLUrl) {
  
  if (GBrowserIsCompatible()) {
    googleMap = new GMap2(document.getElementById("map"));
    googleMap.addControl(new GMapTypeControl());
    googleMap.addControl(new GLargeMapControl());
    googleMap.addControl(new GOverviewMapControl());
    
    
    /**
     * This function creates a marker on the map.
     * 
     * @param point The point where to create the marker.
     * @param label The mouseover text of the marker.
     * @param descr The text for the popup.
     */
    function createMarker(point, label, descr) {
      if ((label != null) && (label != "")) {
        var marker = new GMarker(point, {title:label});
      } else {
        var marker = new GMarker(point);
      }
      GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(descr);  });
      return marker;
    }
    
    
    // get the XML data and add the markers to the map
    GDownloadUrl(argXMLUrl, function(data) {
      var xml = GXml.parse(data);
      var markers = xml.documentElement.getElementsByTagName("marker");
      var markerArray = new Array(markers.length);
      
      for (var i = 0; i < markers.length; i++) {
        var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),  parseFloat(markers[i].getAttribute("lng")));
        var descr = markers[i].getAttribute("descr");
        var label = markers[i].getAttribute("label");
        var name = markers[i].getAttribute("name");
        // get the parameter for centering the map
        var refname = location.href;
        refname = refname.replace(/^(.*)\?/, ""); // cut out the query part
        refname = refname.replace(/^(.*)&page=(.*)$/, "$2"); // cut away everything before the page parameter
        refname = refname.replace(/&(.*)$/, ""); // cut away every parameter that might come after it
        if (name == refname) {
          googleMap.setCenter(point, 10);
        }
        // add the marker
        markerArray[i] = createMarker(point, label, descr);
      } /* end for */
      
      // make sure that a center has been set
      initMap();
      
      // add the markers to the list (separate loop because the maps needs to
      // have a center first)
      for (var i = 0; i < markerArray.length; i++) {
        // add the marker
        if (markerArray[i] != null) {
          googleMap.addOverlay(markerArray[i]);
        }
      } /* end for */
    });
    
    // make sure that a center has been set (in case the XML was not found and
    // GDownloadUrl did nothing, give GDownloadUrl some time to be executed)
    window.setTimeout("initMap()", 2500);
  } /* end if compatible */
}


/**
 * Ensures that the map has been initialized with setCenter by setting it to
 * the center of Germany (default location, zoom factor 5) if necessary.
 * Global function so that it can be called from setTimeout().
 */
function initMap() {
  if ((googleMap != null) && !googleMap.isLoaded()) {
    // center of Germany
    googleMap.setCenter(new GLatLng(50.79204706440684, 9.4953125), 5);
  }
}
