﻿var Map;
var GeoCoder = null;
var Bounds;
var Reasons = [];
var CurrentCountry
var CurrentZoom

function LoadGoogleMaps(PlaceHolder) {
    if (document.getElementById(PlaceHolder)) {
        if (GBrowserIsCompatible()) {
            // Setup Google Maps
            Map = new GMap2(document.getElementById(PlaceHolder));
            Map.setCenter(new GLatLng(26, 16), 2);
            Map.setMapType(G_NORMAL_MAP);
            Map.addControl(new GSmallMapControl(0, 1));
            
            new GKeyboardHandler(Map);

            // Setup GeoCoder
            GeoCoder = new GClientGeocoder();
            GeoCoder.setCache(null);

            // Setup bounds
            Bounds = new GLatLngBounds;

            Reasons[G_GEO_SUCCESS] = "Success";
            Reasons[G_GEO_MISSING_ADDRESS] = "Missing Address: The address was either missing or had no value.";
            Reasons[G_GEO_UNKNOWN_ADDRESS] = "Unknown Address:  No corresponding geographic location could be found for the specified address.";
            Reasons[G_GEO_UNAVAILABLE_ADDRESS] = "Unavailable Address:  The geocode for the given address cannot be returned due to legal or contractual reasons.";
            Reasons[G_GEO_BAD_KEY] = "Bad Key: The API key is either invalid or does not match the domain for which it was given";
            Reasons[G_GEO_TOO_MANY_QUERIES] = "Too Many Queries: The daily geocoding quota for this site has been exceeded.";
            Reasons[G_GEO_SERVER_ERROR] = "Server error: The geocoding request could not be successfully processed.";

            window.setTimeout(GeocodeAll, 50);
        }
    }
}

function SetBoundZoom() {

    Map.setZoom(Map.getBoundsZoomLevel(Bounds));
    Map.setCenter(Bounds.getCenter());
//    var clat = (Bounds.getNorthEast().lat() + Bounds.getSouthWest().lat()) / 2;
//    var clng = (Bounds.getNorthEast().lng() + Bounds.getSouthWest().lng()) / 2;
//    Map.setCenter(new GLatLng(clat, clng));
//    zoomlevel = Map.getZoom();
}

var AddressMarker;
var NumGeocoded = 0;

function GeocodeAll() {
    if (Addresses.length > 0) {
        for (i = 0; i < Addresses.length; i++) {
            GeoCoder.getLocations(Addresses[i][0], function(Response) {
                if (Response.Status.code == G_GEO_SUCCESS) {
                    try {
                        var Place = Response.Placemark[0];
                        var Point = new GLatLng(Place.Point.coordinates[1],
                                             Place.Point.coordinates[0]);

                        Bounds.extend(Point);
                        var Marker = new GMarker(Point);
                        var ButtonId = GetButtonID(Response.name);
                        var Callback = function(Args) {
                            return function(Id) {
                                document.getElementById(Args).click();
                            };
                        };

                        GEvent.addListener(Marker, "click", Callback(ButtonId));

                        Map.addOverlay(Marker);
                    }
                    catch (ex) { alert(ex); }
                }
                NumGeocoded++;
                if (NumGeocoded >= Addresses.length) {
                    setTimeout(SetBoundZoom, 5);
                }
            }
            );
        }
    } else {
            GeoCoder.getLocations(CurrentCountry, function(Response) {
                if (Response.Status.code == G_GEO_SUCCESS) {
                    try {
                        var Place = Response.Placemark[0];
                        var Point = new GLatLng(Place.Point.coordinates[1],
                                                     Place.Point.coordinates[0]);

                        Bounds.extend(Point);
                        
                    }
                    catch (ex) { alert(ex); }
                }

                Map.setZoom(CurrentZoom);
                Map.setCenter(Bounds.getCenter());
                document.getElementById('divMarkerDealer').style.visibility = 'hidden'
            }
            );
    }

    if (Customer != '') {
        GeoCoder.getLocations(Customer, function(Response) {
            if (Response.Status.code == G_GEO_SUCCESS) {
                try {
                    var GreenIcon = new GIcon();
                    GreenIcon.image = "/Design/marker_34_green.png";
                    GreenIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
                    GreenIcon.iconSize = new GSize(20, 34);
                    GreenIcon.shadowSize = new GSize(37, 34);
                    GreenIcon.iconAnchor = new GPoint(6, 34);
                    GreenIcon.infoWindowAnchor = new GPoint(5, 1);

                    var Place = Response.Placemark[0];
                    var Point = new GLatLng(Place.Point.coordinates[1],
                                         Place.Point.coordinates[0]);

                    var Marker = new GMarker(Point, GreenIcon);

                    Bounds.extend(Point);

                    Map.addOverlay(Marker);

                    document.getElementById('divMarkerKlant').style.display = '';

                    setTimeout(SetBoundZoom, 5);
                }
                catch (ex) { alert(ex); }
            }
        }
        );
    }
}

function GetButtonID(Address) {
    for (i = 0; i < Addresses.length; i++) {
        if (Addresses[i][0] == Address) {
            return Addresses[i][1];
        }
    }
}

function GoogleUnload() {
     // Remove the Google Crap
    var GoogleDiv = document.getElementById('GoogleMap');
    if (GoogleDiv) {
        if (GoogleDiv.childNodes) {
            if (GoogleDiv.childNodes.length > 2) {
                GoogleDiv.childNodes[2].style.visibility = 'hidden';
            }
        } else {
            if (GoogleDiv.children.length > 2) {
                GoogleDiv.children[2].style.visibility = 'hidden';
            }
        }
    }

    var GoogleLogo = document.getElementById('logocontrol');
    if (GoogleLogo) {
        GoogleLogo.style.visibility = 'hidden';
    }
}

function DoGoogleInit(Country) {
    AddBodyEvent('load', LoadGoogleMaps('GoogleMap'));
    AddBodyEvent('unload', GoogleUnload());
    if (Country == 1) {
        CurrentCountry = 'Nederland';
        CurrentZoom = 6;
    }
    else if (Country == 243) {
        CurrentCountry = 'Duitsland';
        CurrentZoom = 5;
    }
    else {
        CurrentCountry = 'Nederland';
        CurrentZoom = 6;
    }

}

function PerfomRadioClick(Checkbox, Button) {
    if (document.getElementById(Checkbox).checked) {
        document.getElementById(Button).click();
    }
}
