
var GoogleMap = {
  Keys: {
    "www.mcfa.de": "ABQIAAAAZZqalJux_HOZITuS9UGiEBRW_N6Cyx2firr5nikVGR2-TH0pqhRUVy8WDZfrowRa16j4jTYU17OCLg",
    "mcfa.de": "ABQIAAAAn9lfXlcSKT3CpSJFxNmWnhSWugyO1xGhnQXTjyvcD5z25M_NyRRONgShtfLMWpj3Yo_OjuuW3Z9GmQ",
    "www.friedrich-angels.de": "ABQIAAAAn9lfXlcSKT3CpSJFxNmWnhRi2dF4tMnvIofqrn5nwIUrIOSj7RTPbSfW9dJ1DhdgAVbrDDEfGG-gXg",
    "friedrich-angels.de": "ABQIAAAAn9lfXlcSKT3CpSJFxNmWnhSTDfhN9u175AJxgqcEjUvJuRWFdBRraSkwEbiprLXGN9FwBySdddmAXg",
    "rudedet.in-dsl.de": "ABQIAAAAZZqalJux_HOZITuS9UGiEBRaXqsZ8fLgG-RLUbYKsHduEWLI2RQl9pqAOCR8YKDmaXZ2oyM8thQzcA",
    "mcfa.nk25-berlin.de": "ABQIAAAAZZqalJux_HOZITuS9UGiEBRGN-13wLMGtzoHup7GVHc6KE53NBT330dHohPTWnQx9fj9zbraJEvwDw"
  },

  Initialized: false,

  URL: "http://maps.google.com/maps?file=api&amp;v=2&amp;key=",

  init: function(mapID, mapWidth, mapHeight, loadFunc) {
    if (!GoogleMap.Initialized) {
      GoogleMap.Initialized = true;

      if (!(location.hostname in GoogleMap.Keys)
          || !GBrowserIsCompatible()) {
        document.writeln(
            '<p class="noGoogleMap">'
	  + 'Hier könnte eine interaktive Karte unserer Fahrtziele angezeigt'
	  + ' werden, wenn der Browser von GoogleMaps unterstützt würde.'
	  + ' Dies sind u.a. Firefox, Netscape, Opera, Safari und'
	  + ' Microsoft InternetExplorer ab Version 5.5.</p>');
        return;
      }

      document.writeln(
	    '<div class="gerahmt links" style="margin-bottom:10px;">'
	  + '  <div id="'+mapID+'"'
	  + '       style="height:'+mapHeight+'px; width:'+mapWidth+'px;">'
	  + '  </div>'
	  + '</div>'
	  + '<p>Beim Klick auf einen Stern wird eine Beschreibung der'
	  + '   entsprechenden Ausfahrt in einem neuen Fenster angezeigt.'
	  + '   Ein Klick auf die Karte zentriert auf den angeklickten Punkt'
	  + '   und wechselt in die nächstgrößere Auflösung.</p>');

      if (window.addEventListener) {
	window.addEventListener("load", loadFunc, false);
      } else if (window.attachEvent) {
	window.attachEvent("onload", loadFunc);
      }
    } else {
      $(mapID).style.width = mapWidth + "px";
      $(mapID).style.height = mapHeight + "px";
    }
  },

  GetText: function(element, tagName) {
    return element.getElementsByTagName(tagName)[0].firstChild.nodeValue;
  },

  load: function(mapID, centerLat, centerLng, zoomLevel, mapType, markers) {
    var mapDiv = document.getElementById(mapID);

    var map = new GMap2(mapDiv);

    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());

    map.setCenter(new GLatLng(centerLat, centerLng), zoomLevel);
    map.setMapType(mapType);

    // Create our "tiny" marker icon
    var icon = new GIcon();
    icon.image = "/logos/mcfa_map_marker.png";
    icon.shadow = "";
    icon.iconSize = new GSize(16, 16);
    icon.iconAnchor = new GPoint(8, 8);
    icon.infoWindowAnchor = new GPoint(8, 8);
    icon.transparent = "/logos/mcfa_map_marker.png"; // Not that transparent...
    icon.imageMap = [ 0,4, 4,8, 1,12, 4,15, 8,10, 14,15,
		      14,12, 12,8, 16,4, 11,4, 9,0, 7,0, 6,4 ];

    // Create the marker icon for current location.
    var iconCurr = new GIcon(icon);
    iconCurr.image = "/logos/mcfa_map_marker_akt.png";
    iconCurr.transparent = "/logos/mcfa_map_marker_akt.png"; // once again...
    iconCurr.imageMap = [ 0,4, 0,12, 4,16, 12,16, 16,12, 16,4, 12,0, 4,0 ];

    for (var i = 0; i < markers.length; i++) {
      var mark = markers[i];
      var gMarker = new GMarker(new GLatLng(mark.lat, mark.long),
		     { title: mark.title, icon: mark.curr ? iconCurr : icon });
      map.addOverlay(gMarker);
      gMarker.href = mark.href;
    }

    GEvent.addListener(map, "click", function(marker, point) {
      if (marker) {
        window.open(marker.href);
      } else {
        map.setCenter(point);
        map.zoomIn();
      }
    });

    GEvent.addListener(map, "dblclick", function (marker, point) {
      if (GoogleMap.ondblclick) {
	GoogleMap.ondblclick(point.lat(), point.lng());
      }
    });
  }
};

if (location.hostname in GoogleMap.Keys) {
  document.write
    ('<script src="' + GoogleMap.URL + GoogleMap.Keys[location.hostname] + '"'
          + ' type="text/javascript"></script>');

  window.onunload = function() {
    GUnload();
  };
}
