var map;
var gdir;
var geocoder = null;
var addressMarker;

function initMaps() {
   if ($('map') && GBrowserIsCompatible()) {      
      Event.observe(window,'unload', GUnload, false)
      map = new GMap2($('map'));
      map.addControl(new GSmallMapControl());
      map.addControl(new GMapTypeControl());
      setCenter();
      if ($('directions')) {
         gdir = new GDirections(map, $("directions"));
         GEvent.addListener(gdir, "load", onGDirectionsLoad);
         GEvent.addListener(gdir, "error", handleErrors);
      }
   }
}

/*
   Original files:
   http://www.google.com/intl/en_ALL/mapfiles/shadow50.png
   http://www.google.com/intl/en_ALL/mapfiles/marker.png
   http://www.google.com/intl/en_ALL/mapfiles/markerff.gif
*/

function setCenter() {
   map.setCenter(new GLatLng(52.252137479247466, 5.214493274688721), 14);
   map.addOverlay(createMarker(map.getCenter(),1))
}

function createMarker(point, number) {
   var icon = new GIcon();
   icon.image = "http://www.medid.eu/images/gmarker.png";
   icon.shadow = "http://www.google.com/intl/en_ALL/mapfiles/shadow50.png";
   icon.iconSize = new GSize(20, 34);
   icon.shadowSize = new GSize(37, 34);
   icon.iconAnchor = new GPoint(6, 20);
   icon.infoWindowAnchor = new GPoint(5, 1);
   
  var marker = new GMarker(point,icon);
  GEvent.addListener(marker, "click", function() {
   marker.openInfoWindowHtml('<b>Medid Webapplications</b><br />Postbus 1112<br />1200 BC Hilversum<br /><a href="http://www.medid.eu/">www.medid.eu</a>');
  });
  return marker;
}




function clearMap() {
   if ( directions ) {
      directions.clear()
   }
}

function setMsg(msg) {
   $('status_direction').innerHTML = msg
   $('status_direction').show()
   new Effect.Highlight('status_direction')
}

function handleErrors(){
   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
      setMsg("Er is geen overeenkomende geografische lokatie gevonden van het gespecificeerde adres. Het kan zijn dat het adres te nieuw is of, omdat het incorrect is")
   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
      setMsg("Er heeft zich een fout voorgedaan, probeer het nogmaals")
   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
      setnMsg("U heeft geen adres opgegeven")
   else if (gdir.getStatus().code == G_GEO_BAD_KEY)
      setMsg("Uw Google API Key is incorrect")
   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
       setMsg("Het heeft niet helemaal gewerkt, probeer het nogmaals")
   else setMsg("Een onbekende fout was opgetreden.");
   
   $('directions').innerHTML = ''
}

function clearToDefault(e) {
   if ( e.value == 'Bijvoorbeeld: Stationsstraat 50, Amsterdam' ||
        e.value == 'U dient een route in te geven' ) {
      e.value = ''
   }  
}
   
function calcRoute() {
   $('directions').innerHTML = ''
   $('status_direction').hide()
   $('print_route').hide();

   if ($F('to') != '' ) {
      gdir.load("from: " + $F('to') +
                ", Nederland to: Hilversumseweg 16A, Laren, Nederland" ,
                {"locale": 'nl_NL'});
   } else {
      $('to').value = 'U dient een route in te geven'
      new Effect.Highlight('to')
   }
   return false
}


function onGDirectionsLoad(){ 
   if (gdir.getStatus().code == '200') {
       $('print_route').show();
   }
}

Event.observe(window, 'load', initMaps, false)
