﻿var geocoder, map, addressMarker, gdir, fromAddress, toAddress;

function initGoogleMaps() {

    if (GBrowserIsCompatible()) {
        //settings
        var companyMarkerImage = "googlemaps_logo.png";
        var companyMarkerShadow = "googlemaps_logo_shadow.png";
        var companyLatLng = new GLatLng(50.8474, 5.6996);
   

        var companyMarkerSize = new GSize(30, 30); //width, height

        var defaultZoomLevel = 7;
        //end settings

        //setup elements
        map = new GMap2(document.getElementById("map_canvas"));
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        gdir = new GDirections(map, document.getElementById("directions"));

        //error handler
        GEvent.addListener(gdir, "error", handleErrors);

        //set company marker
        var companyMarker = createMarker(companyLatLng, companyMarkerImage, companyMarkerSize, companyMarkerShadow);

        //set map center
        map.setCenter(companyLatLng, defaultZoomLevel);
        map.addOverlay(companyMarker);

        //Maastricht
        /*companyLatLng = new GLatLng(50.8474, 5.6996);
        companyMarker = createMarker(companyLatLng, companyMarkerImage, companyMarkerSize, companyMarkerShadow);
        map.addOverlay(companyMarker);
*/
        //Maastricht geusselt
        companyLatLng = new GLatLng(50.8584, 5.7185);
        companyMarker = createMarker(companyLatLng, companyMarkerImage, companyMarkerSize, companyMarkerShadow);
        map.addOverlay(companyMarker);

        //Eindhoven
        companyLatLng = new GLatLng(51.4441, 5.4751);
        companyMarker = createMarker(companyLatLng, companyMarkerImage, companyMarkerSize, companyMarkerShadow);
        map.addOverlay(companyMarker);

        //Amstelveen
        companyLatLng = new GLatLng(52.31205, 4.86168);
        companyMarker = createMarker(companyLatLng, companyMarkerImage, companyMarkerSize, companyMarkerShadow);
        map.addOverlay(companyMarker);

    }
}

function overlayDirections() {

    toAddress = document.getElementById("to_address").options[document.getElementById("to_address").selectedIndex].value;
    fromAddress =
      document.getElementById("street").value
      + " " + document.getElementById("city").value
      + " " + document.getElementById("zip").value;

    GEvent.addListener(gdir, "load", function(){
        setTimeout(function(){
            if (gdir.getStatus().code == G_GEO_SUCCESS ){
                $('#contact_formulier').fadeOut(250, function(){
                    $('#toonbeschrijving').show(500, function(){
                        $('#map_canvas').height(440);    
                    });
                });
            }    
        }, 1000);    
    });

    gdir.load("from: " + fromAddress + " to: " + toAddress, { "locale": "nl", getSteps: true, travelMode: G_TRAVEL_MODE_DRIVING });
}

function createMarker(latlng, imageURL, imageSize, shadowURL) {

    var marker = new GIcon(G_DEFAULT_ICON);
    marker.image = imageURL;
    marker.shadow = shadowURL;
    marker.iconSize = imageSize;
    //marker.shadowSize = new GSize(22, 20);

    var options = { icon: marker };

    return new GMarker(latlng, options);

}

function handleErrors() {


    if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
        alert("Geen geografische locatie gevonden. Vul aub de juiste velden correct in, of voeg een woonplaats toe...");
    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_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("An unknown error occurred.");
}
