// Load API

var key1 = '<' + 'script src="http://maps.google.com/maps?file=api&v=2&key=ABQIAAAAVKfJtzPeHKlIHBqWnk0xHxRos1FFQWLU19sFXcOShfSA4OG3exR3l-_-0BflQFBNsJjeWMWZHtzDHg" type="text/javascript">'+'<'+'/script>';
var key2 = '<' + 'script src="http://maps.google.com/maps?file=api&v=2&key=ABQIAAAAtNLev_kk4Po2U6Lx8Q9bbxQThUOmlw9Mp8TjZpobiE3hDGoy5RSavm3NX3CRgAnDIJpsfiTdNH1l-A" type="text/javascript">'+'<'+'/script>';
var key3 = '<' + 'script src="http://maps.google.com/maps?file=api&v=2&key=ABQIAAAAtNLev_kk4Po2U6Lx8Q9bbxQsOMbswokgzU0-2Giko24R7CdGTBT36hQNJIf3RjYs3T29jezzDzwciA" type="text/javascript">'+'<'+'/script>';

if (window.location.host == 'www-mgi.informatik.rwth-aachen.de') {
  document.write(key1);
}
else if (window.location.host == 'www.logic.rwth-aachen.de') {
  document.write(key2);
}
else if (window.location.host == 'logic.rwth-aachen.de') {
  document.write(key3);
}

// Load map

function gload() {
  function createMarker(point,icon,html) {
    var marker = new GMarker(point,{icon:icon});
    GEvent.addListener(marker, "click", function() {
      marker.openInfoWindowHtml(html);
    });
    return marker;
  }

  if (GBrowserIsCompatible()) {
    // Create the map
    var map = new GMap2(document.getElementById("googlemap"));
    map.enableContinuousZoom ();
    map.addControl(new GLargeMapControl ());
    map.addControl(new GMapTypeControl ());
    map.addControl(new GScaleControl());
    map.removeMapType (G_SATELLITE_MAP);
    map.setCenter(new GLatLng(0,0),0);

    // Create the icons
    var icon1 = new GIcon();
    icon1.iconSize = new GSize(32,22);
    icon1.iconAnchor = new GPoint(15,10);
    icon1.infoWindowAnchor = new GPoint(15,0);
    icon1.image = "../img/bahn.png";
    var icon2 = new GIcon();
    icon2.iconSize = new GSize(32,32);
    icon2.iconAnchor = new GPoint(15,15);
    icon2.infoWindowAnchor = new GPoint(15,0);
    icon2.image = "../img/bus.png";
    var icon3 = new GIcon();
    icon3.iconSize = new GSize(39,34);
    icon3.shadowSize = new GSize(39,34);
    icon3.iconAnchor = new GPoint(11,33);
    icon3.infoWindowAnchor = new GPoint(11,0);
    icon3.infoShadowAnchor = new GPoint(11,0);
    icon3.image = "../img/arrow.png";
    icon3.shadow = "../img/arrowshadow.png";

    // Create the markers
    var point1 = new GLatLng(50.7785, 6.06); // MGI
    var marker1 = createMarker(point1,icon3,'<p>Computer Science Bulding</p><p><strong>We are here!</strong></p>');
    map.addOverlay(marker1);
    var point2 = new GLatLng(50.7676,6.09); // Hauptbahnhof
    var marker2 = createMarker(point2,icon1,'<p>Main Station (<em>Hauptbahnhof</em>)</p><p><em>All trains stop here</em></p>');
    map.addOverlay (marker2);
    var point3 = new GLatLng(50.771,6.0915); // Wallstrasse
    var marker3 = createMarker(point3,icon2,'<p>Bus stop <em>Wallstra&szlig;e</em></p><p><em>Line 33</em></p>');
    map.addOverlay (marker3);
    var point4 = new GLatLng(50.7804,6.071); // Westbahnhof (DB)
    var marker4 = createMarker(point4,icon1,'<p>West Station (<em>Westbahnhof</em>)</p><p><em>Only regional trains stop here</em></p>');
    map.addOverlay (marker4);
    var point5 = new GLatLng(50.7805,6.0718); // Westbahnhof (Bus)
    var marker5 = createMarker(point5,icon2,'<p>Bus stop <em>Westbahnhof</em></p><p><em>Line 33</em></p>');
    map.addOverlay (marker5);
    var point6 = new GLatLng(50.7795,6.0582); // Halifaxstrasse
    var marker6 = createMarker(point6,icon2,'<p>Bus stop <em>Halifaxstra&szlig;e</em></p><p><em>Lines 12,22,23,33</em></p>');
    map.addOverlay (marker6);

    // Calculate zoom and center
    var bounds = new GLatLngBounds();
    bounds.extend(point2);
    bounds.extend(point3);
    bounds.extend(point4);
    bounds.extend(point6);
    map.setZoom(map.getBoundsZoomLevel(bounds));
    var clat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) /2;
    var clng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) /2;
    map.setCenter(new GLatLng(clat,clng));
  }
  else {
    alert("In order to access the location plan, you need to use a Google Maps compatible browser and enable JavaScript.");
  }
}

// Unload map

function gunload() {
  GUnload ();
}
