﻿
//<![CDATA[

var map;
var geocoder;
var mgr;
var icons = {};
var directionsPanel;
var directions;
    

function gm_initialize(address, latitude, longitude, zoom, title, taal) {
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map_canvas"));
        geocoder = new GClientGeocoder();
        map.addControl(new GLargeMapControl());  // GLargeMapControl - GSmallMapControl - GSmallZoomControl - GScaleControl - GMapTypeControl - GHierarchicalMapTypeControl - GOverviewMapControl
        //map.addControl(new GOverviewMapControl());
        //map.addControl(new GMapTypeControl());
        map.enableDoubleClickZoom();
        map.setMapType(G_NORMAL_MAP); //G_NORMAL_MAP - G_SATELLITE_MAP - G_HYBRID_MAP - G_DEFAULT_MAP_TYPES - G_PHYSICAL_MAP

        if (latitude != '' | longitude != '') {
            map.setCenter(new GLatLng(latitude, longitude), zoom);

            var point = new GLatLng(latitude, longitude);

            // Bepaal punt uit coordinaten
            // ***************************
            createmarker(point, title, address, taal);
            
        }
        else {

            if (geocoder) {

                geocoder.getLatLng(address, function(point) {
                    if (!point) {
                        alert("Coordinatien zijn niet gedefinieerd en kunnen niet automatisch bepaald worden via Geocoder (KID=<% = GM_KantoorID %>)");
                    } else {
                        map.setCenter(point, 15);

                        // Bepaal punt via geocoder
                        // *************************
                        createmarker(point, title, address, taal);
                    }
                }
            );

            }
            else {
                alert("Geocoder niet beschikbaar");
                map.setCenter(new GLatLng(50.62507306341437, 4.3121337890625), 8);

            }



        }

        
    }
}

function createmarker(point, title, addressfrom, taal) {

    if (point) {
        pin = new GMarker(point, { draggable: false });

        map.addOverlay(pin);

        if (taal == 'F' || taal == 'f') {
            pin.openInfoWindowHtml("<div style=\"padding-top:12px;color:#202020;\"><form name=\"gmformfind\" id=\"gmformfind\" onsubmit=\"return false;\"><b>Route à partir de:</b><br><input type=\"hidden\" name=\"addressto\" id=\"addressto\" value=\"" + addressfrom + "\" /><input type=\"text\" name=\"addressfrom\" id=\"addressfrom\" value=\"straat nr, Gemeente\" onfocus=\"this.value=''\" width=\"50\" style=\"width:180px;border:solid 1px #808080;padding:2px;font-size:10px;\" />&nbsp;<input name=\"BtnRouteGo\" type=\"button\" value=\"Go\" style=\"font-size:10px;border:solid 1px #000;\" onclick=\"showroute();\" /></form></div>");
        }
        else {
            pin.openInfoWindowHtml("<div style=\"padding-top:12px;color:#202020;\"><form name=\"gmformfind\" id=\"gmformfind\" onsubmit=\"return false;\"><b>Route vanaf locatie:</b><br><input type=\"hidden\" name=\"addressto\" id=\"addressto\" value=\"" + addressfrom + "\" /><input type=\"text\" name=\"addressfrom\" id=\"addressfrom\" value=\"straat nr, Gemeente\" onfocus=\"this.value=''\" width=\"50\" style=\"width:180px;border:solid 1px #808080;padding:2px;font-size:10px;\" />&nbsp;<input name=\"BtnRouteGo\" type=\"button\" value=\"Go\" style=\"font-size:10px;border:solid 1px #000;\" onclick=\"showroute();\" /></form></div>");
        }


    }


}

function showroute() {
    var addressfrom = document.getElementById("addressfrom").value;
    var addressto = document.getElementById("addressto").value;
    //alert(addressfrom);
    //alert(addressto);
    directionsPanel = document.getElementById("route");
    directions = new GDirections(map, directionsPanel);
    //GEvent.addListener(directions, "load", onGDirectionsLoad);
    GEvent.addListener(directions, "error", handleErrors);
    directions.load("from: " + addressfrom + ", Belgium to: " + addressto + ", Belgium");

    var iw = map.getInfoWindow();
    iw.hide();
}


function handleErrors(){
    if (directions.getStatus().code == G_GEO_UNKNOWN_ADDRESS) {
        alert("De locatie van het opgegeven vertrekadres kon niet worden bepaald.nFoutcode: " + directions.getStatus().code);
        var iw = map.getInfoWindow();
        iw.show();
    }
    else if (directions.getStatus().code == G_GEO_SERVER_ERROR) {
        alert("Er is iets misgegaan. Maar het is onduidelijk WAT!n Foutcode: " + directions.getStatus().code);
        var iw = map.getInfoWindow();
        iw.show();
    }
    else if (directions.getStatus().code == G_GEO_MISSING_QUERY) {
        alert("Een parameter ontbreekt. Misschien was er geen vertrekpunt ingevoerd.n Foutcode: " + directions.getStatus().code);
        var iw = map.getInfoWindow();
        iw.show();
    }
    else if (directions.getStatus().code == G_GEO_BAD_KEY) {
        alert("De Google Maps sleutel is ongeldig of de gebruikte sleutel is niet geldig voor dit domein. n Foutcode: " + directions.getStatus().code);
        var iw = map.getInfoWindow();
        iw.show();
    }
    else if (directions.getStatus().code == G_GEO_BAD_REQUEST) {
        alert("De aanvraag voor het plannen van een route is mislukt.n Foutcode: " + directions.getStatus().code);
        var iw = map.getInfoWindow();
        iw.show();
    }
    else {
        alert("Er is iets misgegaan. Maar het is volstrekt onduidelijk WAT de oorzaak is!");
        var iw = map.getInfoWindow();
        iw.show();
    }
}


//]]>

