﻿GoogleMaps.INITIAL_MAP_X = 50.764259357116465;
GoogleMaps.INITIAL_MAP_Y = 10.17333984375;
GoogleMaps.INITIAL_MAP_ZOOM = 6;
GoogleMaps.instance = function() {
        if (!GoogleMaps._instance) {
                GoogleMaps._instance = new GoogleMaps();
        }
        return GoogleMaps._instance;
}
GoogleMaps._instance = null;
function GoogleMaps() {
}
GoogleMaps.prototype.map = null;
GoogleMaps.prototype.geoCode = null;
GoogleMaps.prototype.currentMarker = null;

GoogleMaps.prototype.loadMap = function() {
        var startPoint = new GLatLng(GoogleMaps.INITIAL_MAP_X, GoogleMaps.INITIAL_MAP_Y);
        this.map = new GMap2(document.getElementById("gMapsArea"));
        this.map.setCenter(startPoint, GoogleMaps.INITIAL_MAP_ZOOM);
        this.map.addControl(new GLargeMapControl());
        this.map.addControl(new GMapTypeControl());
        this.map.addControl(new GScaleControl());
        this.map.openInfoWindowHtml(startPoint, "<strong>Willkommen,</strong><br /><br/Bitte warten Sie einen Augenblick, die Adresse wird ermittelt...");
       
        this.geoCode = new GClientGeocoder();
       
        var obj = this;
        GEvent.addListener(this.map, "dragstart", function() {
                obj.map.closeInfoWindow();
        });
        GEvent.addListener(this.map, "moveend", function() {
        var center = obj.map.getCenter();
        document.getElementById("x").value = center.x;
        document.getElementById("y").value = center.y;
        document.getElementById("zoom").value = obj.map.getZoom();
    });
}
GoogleMaps.prototype.getLocationData = function (query) {
        var thisObject = this;
        var markers = [];
       
        this.geoCode.getLocations(query, function(addresses) {
                thisObject.map.closeInfoWindow();
                if (addresses.Status.code != 200) {
                        //alert("Sorry, we cannot find the location of the following address:\n " + query);
                        try {
                                thisObject.map.removeOverlay(thisObject.currentMarker);
                                thisObject.currentMarker = null;
                                if (thisObject.currentMarker.poly) {
                                        thisObject.map.removeOverlay(thisObject.currentMarker.poly);
                                }
                        } catch (e) {
                                //
                        }
                        return ;
                } else {
                        if (thisObject.currentMarker) {
                                thisObject.map.removeOverlay(thisObject.currentMarker);
                                thisObject.currentMarker = null;
                        }
                        thisObject.currentMarker = thisObject.createNewMarker();
                        var result = addresses.Placemark[0];
                        thisObject.currentMarker.howMany = addresses.Placemark.length;
                       
                        thisObject.currentMarker.response = result.address;
//                        var details = "Fuldaer Pokerfreunde, Sportplatz Eichenzell" || {};
                        var details = result.AddressDetails || {};
                        thisObject.currentMarker.accuracy = details.Accuracy||0;
                        var lat = 50.489491;
                        var lng = 9.693478;
//                        var lat = result.Point.coordinates[1];
//                        var lng = result.Point.coordinates[0];
                        thisObject.currentMarker.LAT = result.Point.coordinates[1];
                        thisObject.currentMarker.LNG = result.Point.coordinates[0];
                        var responsePoint = new GLatLng(lat, lng);
                        thisObject.currentMarker.setLatLng(responsePoint);
                        if (thisObject.currentMarker.poly) {
                                thisObject.map.removeOverlay(thisObject.currentMarker.poly);
                        }
                        thisObject.currentMarker.index = markers.length;
                        markers.push(thisObject.currentMarker);
                        thisObject.map.setCenter(responsePoint);
                        thisObject.map.setZoom(thisObject.currentMarker.accuracy * 4 + -1);
                        if (result.address) {
                                thisObject.showInfo();
                        }
                }
        });
}
GoogleMaps.prototype.createNewMarker = function() {
        var thisObject = this;
       
        this.currentMarker = new GMarker(this.map.getCenter()/*, this.pointer()*/);
        this.map.addOverlay(this.currentMarker);
       
        /*
        GEvent.addListener(this.currentMarker, "dragend", function(markerPoint) {
                if (!thisObject.map.getBounds().containsLatLng(markerPoint)) {
                        thisObject.map.removeOverlay(this);
                        thisObject.map.removeOverlay(this.poly);
                } else {
                        thisObject.getLocationData(thisObject.currentMarker.getLatLng(), this);
                }
        });
        */
        return this.currentMarker;
}
GoogleMaps.prototype.pointer = function() {
        var GVIcon = new GIcon();
        GVIcon.image = "./flag_48x48.png";
        GVIcon.iconSize = new GSize(48, 48);
        GVIcon.iconAnchor = new GPoint(23,50);
        GVIcon.infoWindowAnchor = new GPoint(25, 5);
        return {icon:GVIcon, draggable:true, autoPan:false};
}
GoogleMaps.prototype.showInfo = function() {
        var iwContents = this.currentMarker.response.replace(/,/g, ",<br/>");
        this.currentMarker.bindInfoWindowHtml(iwContents);
        this.map.openInfoWindowHtml(this.currentMarker.getLatLng(), iwContents);
}
