var map;
var gdir;
var geocoder = null;
var addressMarker;

var trefpuntLat = 52.973574;
var trefpuntLon = 6.882449;

function load() {
  if (GBrowserIsCompatible()) {      
    map = new GMap2(document.getElementById("map"));

    gdir = new GDirections(map, document.getElementById("directions"));
    GEvent.addListener(gdir, "load", onGDirectionsLoad);
    GEvent.addListener(gdir, "error", handleErrors);
    map.setCenter(new GLatLng(trefpuntLat, trefpuntLon), 13);
    map.addControl(new GSmallMapControl());
    //map.addControl(new GMapTypeControl());

    var infoTabs = [
      new GInfoWindowTab("Adres", "<b>Onrust</b><br />Noorderblokken 5<br>9523 TJ Drouwenermond<br />t: 0599 653042<br />f: 0599 653304"),
    ];
    
    var point = new GLatLng(trefpuntLat,trefpuntLon);
    var marker = new GMarker(point);
    GEvent.addListener(marker, "click", function() {
    	marker.openInfoWindowTabsHtml(infoTabs);
    });
    map.addOverlay(marker);	
    //marker.openInfoWindowTabsHtml(infoTabs);			
  }
}

// Creates a marker at the given point with the given number label
function createMarker(point, number) {

}

function setDirections(fromAddress, toAddress, locale) {
  gdir.load("from: " + fromAddress + " to: " + toAddress,{ "locale": locale });
}

function handleErrors(){
 if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
   document.getElementById('gmap_error').innerHTML = '<p>Het adres kon niet gevonden worden.</p>';
 else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
  document.getElementById('gmap_error').innerHTML = '<p>Google maps kon op dit moment niet bereikt worden.</p>';
 
 else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
   document.getElementById('gmap_error').innerHTML = '<p>U dient een adres op te geven.</p>';
 
 else if (gdir.getStatus().code == G_GEO_BAD_KEY)
   document.getElementById('gmap_error').innerHTML = '<p>Deze functie kan momenteel niet gebruikt worden.</p>';

 else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
  document.getElementById('gmap_error').innerHTML = '<p>Onverwachte fout probeer het later nogmaals.</p>';
  
 else document.getElementById('gmap_error').innerHTML = '<p>Onverwachte fout probeer het later nogmaals.</p>';
 
}

function onGDirectionsLoad(){ 
// Use this function to access information about the latest load()
// results.
// document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
}

function getDirections(){
  //document.getElementById('contact_gegevens').style.display = 'none';
  setDirections(document.getElementById('adres').value, 'Noorderblokken 5, 9523 TJ Drouwenermond', 'nl');
}
