$(document).ready(function() {
    initialize();
    $('#aspnetForm').submit(function() {
        return false;
    });
    $('#adres').keyup(function(e)
    {
        if(e.keyCode == 13) {
            $('#route').click();
        }
    });
    //$('.container').html('<img src="/_design/images/wegwijzer_small.jpg" alt="Wegwijzer"/>');
});
 
var map;
var gdir;
var geocoder = null;
var addressMarker;
 
var baseIcon = new GIcon(G_DEFAULT_ICON);
//baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
//baseIcon.iconSize = new GSize(20, 34);
//baseIcon.iconSize = new GSize(50, 16);
//baseIcon.shadowSize = new GSize(37, 34);
//baseIcon.iconAnchor = new GPoint(9, 34);
//baseIcon.infoWindowAnchor = new GPoint(9, 2);
baseIcon.iconSize = new GSize(75, 24);
baseIcon.shadowSize = new GSize(0, 0);
baseIcon.iconAnchor = new GPoint(37, 24);
baseIcon.infoWindowAnchor = new GPoint(37, 24);


function initialize()
{
    try
    {
    if (GBrowserIsCompatible())
    {
        map = new GMap2(document.getElementById("map_canvas"));
        gdir = new GDirections(map, document.getElementById("directions"));
        //map = new GMap2(document.getElementById("routebeschrijving_map"));
        //gdir = new GDirections(map, document.getElementById("routebeschrijving_text"));
        GEvent.addListener(gdir, "load", onGDirectionsLoad);
        GEvent.addListener(gdir, "error", handleErrors);
 
        map.setCenter(new GLatLng(52.0277, 4.355532), 16);
        map.setUIToDefault();
 
        var letteredIcon = new GIcon(baseIcon);
        letteredIcon.image = "http://www.xuntos.nl/xuntos_logo.jpg";//"http://www.google.com/mapfiles/markerX.png";
        
        // Set up our GMarkerOptions object
        markerOptions = { icon:letteredIcon };
        var marker = new GMarker(new GLatLng(52.0277, 4.355532), markerOptions);
        
        //GEvent.addListener(marker, "click", function() {
        //    marker.openInfoWindowHtml("<img src='http://www.xuntos.nl/xuntos_logo.jpg' alt='Xuntos B.V.' width='150'/><br/>Xuntos B.V.<br/>Patrijsweg 80<br/>2289 EX Rijswijk");
        //});
        //map.addOverlay(marker); //Logo marker
        //map.addOverlay(new GMarker(new GLatLng(52.0277, 4.355532))); //Default marker
        map.openInfoWindowHtml(map.getCenter(), "<img src='http://www.xuntos.nl/xuntos_logo.jpg' alt='Xuntos B.V.' width='150'/><br/><b>Xuntos B.V.</b><br/>Patrijsweg 80<br/>2289 EX Rijswijk");
 
        //setDirections("Netherlands, Rotterdam", "Netherlands, Rijswijk, Patrijsweg 80", "nl_NL");
    }
    }
    catch(err){}
}
 
function setDirections(fromAddress, toAddress, locale)
{
    gdir.load("from: " + fromAddress + " to: " + toAddress, { "locale": locale });
}
 
function handleErrors()
{
    if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
    {
        //alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
        alert("Het adres is niet gevonden, probeer een ander adres");
    }
    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);
        alert("Helaas kunnen we de routebeschrijving op dit moment niet ophalen");
    }
    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);
        alert("Het adres is niet gevonden, probeer een ander adres");
    }
    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);
        alert("Helaas kunnen we de routebeschrijving op dit moment niet ophalen");
    }
    else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
    {
        //alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
        alert("Helaas kunnen we de routebeschrijving op dit moment niet ophalen");
    }
    else
    {
        //alert("An unknown error occurred.");
        alert("Er is een fout opgetreden. Helaas kunnen we de routebeschrijving op dit moment niet ophalen");
    }
}
 
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...
}

