﻿
//<![CDATA[

var map;
var geocoder;
var mgr;
var icons = {};
var directionsPanel;
var directions;
    

function gm_initialize(address, latitude, longitude, zoom, title, taal, KID) {
    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, KID);
            
        }
        else {

            if (geocoder) {

                geocoder.getLatLng(address, function(point) {
                    if (!point) {
                        if (taal == 'F' || taal == 'f') {
                            alert("Les coordonnées n'ont pas été définies et ne peuvent être déterminées automatiquement par Geocoder (KID=<% = GM_KantoorID %>)");
                        }
                        else {
                            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, KID);
                    }
                }
            );

            }
            else {

                if (taal == 'F' || taal == 'f') {
                    alert("Geocoder non disponible");
                }
                else {
                    alert("Geocoder niet beschikbaar");
                }
                
                map.setCenter(new GLatLng(50.62507306341437, 4.3121337890625), 8);

            }



        }

        
    }
}

function createmarker(point, title, addressfrom, taal, KID) {

    if (point) {

        // Create our "tiny" marker icon
        var icon = new GIcon();
        icon.image = "Images/GoogleMaps/Immotheker.png";
        //icon.shadow = "Images/googlemarkersmall/mm_20_shadow.png";
        icon.iconSize = new GSize(25, 31);
        //icon.shadowSize = new GSize(22, 20);
        icon.iconAnchor = new GPoint(13, 35);
        icon.infoWindowAnchor = new GPoint(15, 1);

        pin = new GMarker(point, icon, { draggable: false });
        //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>Itinéraire à partir des lieux:</b><br><input type=\"hidden\" name=\"addressto\" id=\"addressto\" value=\"" + addressfrom + "\" /><input type=\"text\" name=\"addressfrom\" id=\"addressfrom\" value=\"Rue n°, Commune\" 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('" + taal + "','" + KID + "',1);\" /></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\" id=\"BtnRouteGo\" type=\"button\" value=\"Go\" style=\"font-size:10px;border:solid 1px #000;\" onclick=\"showroute('" + taal + "','" + KID + "',1);\" /></form></div>");
        }


    }


}

function showroute(taal,KID,showPrintLink) {
    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);

    if (taal == 'F' || taal == 'f') {
        directions.load("from: " + addressfrom + ", Belgium to: " + addressto, { "locale": "fr" });
    }
    else {
        directions.load("from: " + addressfrom + ", Belgium to: " + addressto, { "locale": "nl" });
    }

    if (showPrintLink == 1) {

        //voeg link toe naar printoptie
        var link = document.createElement("a");
        link.setAttribute("href", "PopUps/GM_ContactLocation.aspx?L=" + taal + "&KID=" + KID + "&From=" + escape(addressfrom) + "&To=" + escape(addressto));
        link.setAttribute("target", "_blank");
        link.className = "print";
        if (taal == 'F' || taal == 'f') {
            link.innerText = " Imprimez le discription d'itinéraire";
        }
        else {
            link.innerText = "Print de routebeschrijving";
        }

        var extradiv = document.createElement("div");
        extradiv.style.padding = "12px";

        directionsPanel.appendChild(link);
        directionsPanel.appendChild(extradiv);

        var iw = map.getInfoWindow();  //verberg infovenster
        iw.hide();   
    }
    else {

        map.disableInfoWindow(); 

    }


}



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();
    }
}


//]]>

