﻿// JavaScript Document
//<![CDATA[

    var map;
    var geocoder;
    var mgr;
    var home = new GLatLng(48.540018, 9.228788);
    var place;

    var verein;
  var gdir;
    function load() {
      
      map = new GMap2(document.getElementById("map"));
      map.setCenter(new GLatLng(48.540018, 9.228788),13);
      mgr = new GMarkerManager(map);
      
      gdir = new GDirections(map, document.getElementById("directions"));
      
      GEvent.addListener(gdir, "error", handleErrors);
      
      marker = new GMarker(home);
      map.addOverlay(marker);
      
      
      geocoder = new GClientGeocoder();
      map.addControl(new GLargeMapControl());
      map.addControl(new GMapTypeControl(),
               new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10, 10)));
    }
    
    function handleErrors(){
	   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	     alert("Adresse konnte nicht gefunden werden. Versuche es noch einmal im Format: Straße, Ort");
	   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	     alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
	   
	   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	     alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + 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("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);

	   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	     alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
	    
	   else alert("Du musst eine Startadresse eingeben, um eine Route berechene zu können. ");
	   
	}
    
    
    // addAddressToMap() is called when the geocoder returns an
    // answer.  It adds a marker to the map with an open info window
    // showing the nicely formatted version of the address and the country code.
    function addAddressToMap(response) {
    
     
      
      map.clearOverlays();
      if (!response || response.Status.code != 200) {
        alert("Sorry, we were unable to geocode that address");
      } else {
       
        place = response.Placemark[0];
       
        point = new GLatLng(place.Point.coordinates[1],
                            place.Point.coordinates[0]);
        marker = new GMarker(point);
        map.addOverlay(marker);
        marker.openInfoWindowHtml('<div class="marker">'+ verein+'<br /><br />'
                                                        + place.address
                                                       
                                                       
                                                        +'</div>');
        map.setCenter(point);
        }
      
      
    }
    
     function setDirections(fromAddress, toAddress) {
    
      gdir.load("from: " + fromAddress + " to: " + toAddress);
    }

    // showLocation() is called when you click on the Search button
    // in the form.  It geocodes the address entered into the form
    // and adds a marker to the map at that location.
    function showLocation() {
      var address = document.getElementById("ort").q.value;
      geocoder.getLocations(address, addAddressToMap);
      
    }

   // findLocation() is used to enter the sample addresses into the form.
    function findLocation(address, test) {
      verein = test;
      document.getElementById("ort").q.value = address;
      showLocation();
    }
    
    function help(){
      alert("Um eine Spielstätte zu finden, wähle eine Spielstätte auf der Auswahlbox aus und klicke auf 'Diese Spielstätte anzeigen' .\n\n"
      +"Um die Route zu dieser angezeigt zu bekommen, trage Deine Startadresse ein und klicke dann auf 'Anfahrtsroute'. ");
    }
    //]]>

