var map;
var map_type;

var geocoder;
var gdir;
var geocoder = null;

var addressMarker;
var baseIcon;
var adrIcon;

var tooltip;



//----------------
//----- LOAD -----
//----------------
function load() {
  if (GBrowserIsCompatible()) {
    // map = new GMap2(document.getElementById("map"));
    map = new GMap2(document.getElementById("mapcanvas"), {draggableCursor: 'pointer', draggingCursor: 'move'});
    map.enableScrollWheelZoom();

    geocoder = new GClientGeocoder();
    gdir = new GDirections(map, document.getElementById("directions"));

    GEvent.addListener(gdir, "load", onGDirectionsLoad);
    GEvent.addListener(gdir, "error", handleErrors);

    //--------------------------------
    //----- Basis Icon festlegen -----
    //--------------------------------
    baseIcon = new GIcon();
    baseIcon.iconSize = new GSize(163, 34);
    baseIcon.shadowSize = new GSize(163, 34);
    baseIcon.iconAnchor = new GPoint(75, 30);
    baseIcon.infoWindowAnchor = new GPoint(80, 30);

    //---------------------------------
    //----- Adress Icon festlegen -----
    //---------------------------------
    adrIcon = new GIcon();
    adrIcon.iconSize = new GSize(10, 10);
    adrIcon.shadowSize = new GSize(10, 10);
    adrIcon.iconAnchor = new GPoint(5, 5);
    adrIcon.infoWindowAnchor = new GPoint(5, 5);

    //----------------------------
    //----- Karte einstellen -----
    //----------------------------
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    map.addControl(new GScaleControl ());

    map.setCenter(new GLatLng(52.46454697711546, 13.323516547679901), 15);

    //-------------------
    //----- Tooltip -----
    //-------------------
    tooltip = document.createElement("div");

    document.getElementById("mapcanvas").appendChild(tooltip);
    tooltip.style.visibility = "hidden";

    mark_bus_stops();
    mark_celtic_cottage();
  }
}



//---------------------------
//----- Marker erzeugen -----
//---------------------------
function createMarker(point, symbol, text) {
  var icon = new GIcon(baseIcon);

  icon.image = "../../Bilder/map/" + symbol + ".gif";
  icon.shadow = "../../Bilder/map/" + symbol + ".gif";

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



function createMarker2(point, symbol, text, ttip) {
  var icon = new GIcon(adrIcon);

  icon.image = "../../Bilder/map/" + symbol + ".png";
  icon.shadow = "../../Bilder/map/" + symbol + ".png";

  var marker = new GMarker(point, icon);

  if (ttip && ttip != "") {
    marker.tooltip = '<div class="tooltip">' + ttip + '</div>';
  }

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

  //  ======  The new marker "mouseover" and "mouseout" listeners  ======
  GEvent.addListener(marker, "mouseover", function() {
                                            showTooltip(marker);
                                          });
  GEvent.addListener(marker, "mouseout", function() {
                                          tooltip.style.visibility="hidden"
                                        });

  return marker;
}



function mark_bus_stops() {
  var point;
  var act_text;
  var act_ttip;

  //-------------------------------------------------------
  //----- Walther-Schreiber-Platz / Bundesallee (SSC) -----
  //-------------------------------------------------------
//  act_text =  "<div>" +
//                 "<p class=\"info_bus\">" +
//                   "Buslinien M76, X76, 181, N81" +
//                 "</p>"
//               "</div>";
//   act_ttip = "Bus M76, X76, 181, N81";
  act_text = getStopHtml("U Walther-Schreiber-Platz");
  act_ttip = "U Walther-Schreiber-Platz (Berlin)";
  point = new GLatLng(52.46550130557588, 13.327960968017578);
  map.addOverlay(createMarker2(point, "bus_stop", act_text, act_ttip));

  //---------------------------------------------------------------
  //----- Walther-Schreiber-Platz / Bundesallee (Basic Markt) -----
  //---------------------------------------------------------------
  act_text = getStopHtml("U Walther-Schreiber-Platz");
  act_ttip = "U Walther-Schreiber-Platz (Berlin)";
  point = new GLatLng(52.465998071107094, 13.32864761352539);
  map.addOverlay(createMarker2(point, "bus_stop", act_text, act_ttip));

  //----------------------------------------------------------
  //----- S-Bhf Feuerbachstr. (Richtung Thorwaldsenstr.) -----
  //----------------------------------------------------------
  act_text = getStopHtml("S Feuerbachstr.");
  act_ttip = "S Feuerbachstr. (Berlin)";
  point = new GLatLng(52.46372336194894, 13.332509994506836);
  map.addOverlay(createMarker2(point, "bus_stop", act_text, act_ttip));

  //------------------------------------------------------------------
  //----- S-Bhf Feuerbachstr. (Richtung Walther-Schreiber-Platz) -----
  //------------------------------------------------------------------
  act_text = getStopHtml("S Feuerbachstr.");
  act_ttip = "S Feuerbachstr. (Berlin)";
  point = new GLatLng(52.46401097534536, 13.332509994506836);
  map.addOverlay(createMarker2(point, "bus_stop", act_text, act_ttip));

  //-----------------------------------------------
  //----- Walther-Schreiber-Platz / Rheinstr. -----
  //-----------------------------------------------
  act_text = getStopHtml("U Walther-Schreiber-Platz");
  act_ttip = "U Walther-Schreiber-Platz (Berlin)";
  point = new GLatLng(52.46530521237559, 13.32918405532837);
  map.addOverlay(createMarker2(point, "bus_stop", act_text, act_ttip));

  //----------------------------------------------------
  //----- Walther-Schreiber-Platz (Forum Steglitz) -----
  //----------------------------------------------------
  act_text = getStopHtml("U Walther-Schreiber-Platz");
  act_ttip = "U Walther-Schreiber-Platz (Berlin)";
  point = new GLatLng(52.46432473327107, 13.327853679656982);
  map.addOverlay(createMarker2(point, "bus_stop", act_text, act_ttip));

  //---------------------------------
  //----- Schloßstr. (Wertheim) -----
  //---------------------------------
  act_text = getStopHtml("U Schloßstr.");
  act_ttip = "U Schloßstr. (Berlin)";
  point = new GLatLng(52.462063011517664, 13.325364589691162);
  map.addOverlay(createMarker2(point, "bus_stop", act_text, act_ttip));

  //-------------------------------------
  //----- Schildhornstr. (Wertheim) -----
  //-------------------------------------
  act_text = getStopHtml("U Schloßstr.");
  act_ttip = "U Schloßstr. (Berlin)";
  point = new GLatLng(52.46189305085386, 13.324828147888184);
  map.addOverlay(createMarker2(point, "bus_stop", act_text, act_ttip));

  //--------------------------------
  //----- Schildhornstr. (C&A) -----
  //--------------------------------
  act_text = getStopHtml("U Schloßstr.");
  act_ttip = "U Schloßstr. (Berlin)";
  point = new GLatLng(52.4614223871297, 13.324356079101562);
  map.addOverlay(createMarker2(point, "bus_stop", act_text, act_ttip));

  //-------------------------------------------------
  //----- Schloßstr. (Werken,Spielen, Schenken) -----
  //-------------------------------------------------
  act_text = getStopHtml("U Schloßstr.");
  act_ttip = "U Schloßstr. (Berlin)";
  point = new GLatLng(52.461252423992754, 13.325257301330566);
  map.addOverlay(createMarker2(point, "bus_stop", act_text, act_ttip));

  //--------------------------------------------------------------
  //----- Schildhornstr. / Lepsiusstr. (Richtung Schloßstr.) -----
  //--------------------------------------------------------------
  act_text = getStopHtml("Lepsiusstr.");
  act_ttip = "Lepsiusstr. (Berlin)";
  point = new GLatLng(52.46331808533729, 13.319098949432373);
  map.addOverlay(createMarker2(point, "bus_stop", act_text, act_ttip));

  //------------------------------------------------------------
  //----- Schildhornstr. / Lepsiusstr. (Richtung Autobahn) -----
  //------------------------------------------------------------
  act_text = getStopHtml("Lepsiusstr.");
  act_ttip = "Lepsiusstr. (Berlin)";
  point = new GLatLng(52.46322657074733, 13.320043087005615);
  map.addOverlay(createMarker2(point, "bus_stop", act_text, act_ttip));
}



function mark_celtic_cottage() {
  var point;
  var act_text;

  act_text =  "<div id=\"info\">" +
                "<p class=\"info_absatz\">" +
                  "<h1>Herzlich willkommen</h1>" +
                  "<h2>im</h2>" +
                "</p>" +
                "<img src=\"../../Bilder/map/celtic_text.gif\" /><br><br>" +
                "<img src=\"../../Bilder/map/laden.jpg\" />" +
                "<p class=\"info_adr\">" +
                  "Markelstr. 13, 12163 Berlin<br>" +
                  "Tel.: +49 (030) 792 45 07" +
                "</p>" +
              "</div>";

  point = new GLatLng(52.46454697711546, 13.323516547679901);
  map.addOverlay(createMarker(point, "celtic_marker", act_text));
}



function gotoAddress(address, mark, info) {
  geocoder.getLatLng(address, function(point) {
                                if (!point) {
                                  alert(address + " nicht gefunden!");
                                }
                                else {
                                  map.setCenter(point, 15);
                                  if (mark) {
                                    map.addOverlay(createMarker2(point, "red_dot", info));
                                  }
                                }
                              }
                    );
}



function setDirections(fromAddress, toAddress, locale) {
  gdir.load("from: " + fromAddress + " to: " + toAddress, {"locale": locale});
}



function handleErrors() {
  if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
    alert("Eine der angegebenen Adressen konnte nicht lokalisiert werden. Entweder ist die Adresse relativ neu oder sie ist falsch.\nFehlercode: " + gdir.getStatus().code);
  else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
    alert("Bei der Bearbeitung der Anfrage ist ein unbekannter Fehler aufgetreten.\n Fehlercode: " + gdir.getStatus().code);
  else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
    alert("Unvollständige Anfrage.\n Fehlercode: " + gdir.getStatus().code);
//        else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong//          alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);
  else if (gdir.getStatus().code == G_GEO_BAD_KEY)
    alert("Ungültiger API-Schlüssel. \n Fehlercode: " + gdir.getStatus().code);
  else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
    alert("Es wurde keine Route gefunden.\n Fehlercode: " + gdir.getStatus().code);
  else
    alert("Unbekannter Fehler.");
}



function onGDirectionsLoad() {
  // Use this function to access information about the latest load()
  // results.
  // e.g.
  // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
  // and yada yada yada...
}



function clear_route() {
  gdir.clear();

  var i1 = document.getElementById("fromAddress");
  i1.value = "";

  var i2 = document.getElementById("toAddress");
  i2.value = "";
}



function clear_fi() {
  var i1 = document.getElementById("from");
  i1.value = "";

  var i2 = document.getElementById("to");
  i2.value = "";

  var i3 = document.getElementById("date");
  i3.value = "";

  var i4 = document.getElementById("time");
  i4.value = "";
}



//---------------------------
//----- Tooltip anzeigen -----
//---------------------------
function showTooltip(marker) {
	tooltip.innerHTML = marker.tooltip;

  var point = map.getCurrentMapType().getProjection().fromLatLngToPixel(map.getBounds().getSouthWest(),map.getZoom());
  var offset = map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),map.getZoom());
  var anchor = marker.getIcon().iconAnchor;
  var width = marker.getIcon().iconSize.width;
  var pos = new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(offset.x - point.x - anchor.x + width,- offset.y + point.y + anchor.y));

  pos.apply(tooltip);
  tooltip.style.visibility = "visible";
}



function getStopHtml(hst) {
  var strHtml =  "<div>" +
                   "<p class=\"info_bus\">" +
                     hst + "<br><br>" +
                     "<a href='http://www.fahrinfo-berlin.de/Fahrinfo/bin/stboard.bin/dn?ld=0.1&input=\"" + hst + "\" target=\"_blank\"'>Ankunfts-/Abfahrtsplan</a>" +
                   "</p>" +
                 "</div>";
  return strHtml;
}
