﻿/**
* Class für GoogleMap
*/
function clsGoogleMap() {
    this.iMarkerIdx = -1;
    this.oMarker = new Array();
    this.oMarkerIcon = new Array();
    this.oMap = null;
    this.oMapObject = null;
    this.oGeoCoder = null;
    this.noControlls = false;
    /**
    * initialisiert Map
    */
    this.init_map = function (psMapId, poCoords) {
        if (!poCoords) return;
        //

        if (this.oMap == null) {
            this.oMapObject = document.getElementById(psMapId);
            this.oMap = new GMap2(this.oMapObject);
            //this.oMap.setUIToDefault();
            if (this.noControlls == false) {
                this.oMap.addControl(new GSmallMapControl());
                // Mausrad
                //this.oMap.enableScrollWheelZoom();
            }
        }
        //Marker leeren
        this.clearMarker();

        this.oMap.setCenter(new GLatLng(poCoords.lat, poCoords.lng), 12);
        //Routen planung
        if (typeof (document.getElementById(psMapId + '_description')) == 'object') {
            this.oDirectionsPanel = document.getElementById(psMapId + '_description');
        }

        if (this.oGeoCoder == null) {
            this.oGeoCoder = new GClientGeocoder();
            this.oGeoCoder.setBaseCountryCode(countryCode);
        }
    }

    this.init_geocoder = function() {
        if (this.oGeoCoder == null) {
            this.oGeoCoder = new GClientGeocoder();
            this.oGeoCoder.setBaseCountryCode(countryCode);
        }
    }
    
    this.do_getCity = function (address,callback) {
    
        this.oGeoCoder.getLocations(address, 
            function(response) {
                  var city = "Berlin";
                  if (!response || response.Status.code != 200) {
                    /* alert("\"" + address + "\" not found"); */
                  } else {
                    try {
                        for(var i=0; i<response.Placemark.length; i++) {
                            if(response.Placemark[i].AddressDetails.Country) {
                                city = response.Placemark[i].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName;
                                break;
                            }
                        }
                    } catch (e) {
                        callback(city);
                    }
                 }
                 callback(city);
        });
    }

    this.do_getExactCity = function(address, callback) {
    
        this.oGeoCoder.getLocations(address,
            function(response) {
                //debugger;
                var city = "";
                if (!response || response.Status.code != 200) {
                    /* alert("\"" + address + "\" not found"); */
                } else {
                    try {
                        for (var i = 0; i < response.Placemark.length; i++) {
                            if (response.Placemark[i].AddressDetails.Country) {
                                if (response.Placemark[i].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.DependentLocality.Thoroughfare.ThoroughfareName)
                                    city += response.Placemark[i].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.DependentLocality.Thoroughfare.ThoroughfareName + ", ";
                                if (response.Placemark[i].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.DependentLocality.PostalCode.PostalCodeNumber)
                                    city += response.Placemark[i].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.DependentLocality.PostalCode.PostalCodeNumber + " ";
                                if (response.Placemark[i].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName)
                                    city += response.Placemark[i].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName;
                                break;
                            }
                        }
                    } catch (e) {
                        callback(city);
                    }
                }
                callback(city);
            });
    }

    this.do_getLocation = function(address, callback) {
    this.oGeoCoder.setBaseCountryCode(countryCode);
        this.oGeoCoder.getLocations(address,
            function(response) {
                if (!response || response.Status.code != 200) {
                    alert("\"" + address + "\" nicht gefunden");
                } else {
                    try {
                        var lat = response.Placemark[0].Point.coordinates[1];
                        var lon = response.Placemark[0].Point.coordinates[0];
                        callback(lat, lon);
                    } catch (e) { }
                }
            });
    }
    
    /**
    * Berechnet die GoogleMap-Route und zeigt sie an
    */
    this.do_calculate_route = function(poRoute,psMapId) {
        this.oMap.clearOverlays(); // alte route ggf. loeschen
        oDirectionsPanel = document.getElementById(psMapId + '_description');
        this.oDirectionsPanel.innerHTML = '';
        oDirections = new GDirections(this.oMap, this.oDirectionsPanel);

        //for (e in oDirections.getMarker) alert(e);
        //G_END_ICON.image.src = 'images/gm_marker_userposition_on.png';

        oDirections.load('from: ' + poRoute.from + ' to: ' + poRoute.to);
        //setTimeout("oDirections.getMarker(0).hide()", 500);
        this.routeData = new Array();
        this.routeData['sFrom'] = poRoute.from;
        this.routeData['sTo'] = poRoute.to;
    }

    /**
    * Marker
    * @param - object - die Koordinaten
    * @param - string - Text des Popupwindows
    * @param - string - Typ des Marker-Images
    */
    this.add_marker = function(poCoords, psInfoWindowText, piMarkerType) {
        if (!poCoords) return;

        this.iMarkerIdx++;

        var oTmpCoords = new GLatLng(poCoords.lat, poCoords.lng);

        this.oMarkerIcon[this.iMarkerIdx] = new GIcon();
        if (piMarkerType == 1) {
            if (jQuery.browser.msie) {
                //No home icon in IE, because Zindex for markers is not working in IE
            }
            else {
                this.oMarkerIcon[this.iMarkerIdx].imageSrcOff = imagePath + 'DesktopModules/GoogleMapGeo/images/home.png';
                this.oMarkerIcon[this.iMarkerIdx].imageSrcOn = imagePath + 'DesktopModules/GoogleMapGeo/images/home.png';
                this.oMarkerIcon[this.iMarkerIdx].image = this.oMarkerIcon[this.iMarkerIdx].imageSrcOff;
                this.oMarkerIcon[this.iMarkerIdx].shadow = this.oMarkerIcon[this.iMarkerIdx].imageSrcOff;
                this.oMarkerIcon[this.iMarkerIdx].iconSize = new GSize(32, 32);
                this.oMarkerIcon[this.iMarkerIdx].shadowSize = new GSize(32, 32);
            }
        }
        else if (piMarkerType == 2) {
            this.oMarkerIcon[this.iMarkerIdx].imageSrcOff = imagePath + 'DesktopModules/GoogleMapGeo/images/autoservice_marker1.png';
            this.oMarkerIcon[this.iMarkerIdx].imageSrcOn = imagePath + 'DesktopModules/GoogleMapGeo/images/autoservice_marker2.png';
            this.oMarkerIcon[this.iMarkerIdx].image = this.oMarkerIcon[this.iMarkerIdx].imageSrcOff;
            this.oMarkerIcon[this.iMarkerIdx].shadow = this.oMarkerIcon[this.iMarkerIdx].imageSrcOff;
            this.oMarkerIcon[this.iMarkerIdx].iconSize = new GSize(32, 32);
            this.oMarkerIcon[this.iMarkerIdx].shadowSize = new GSize(32, 32);
        }
        this.oMarkerIcon[this.iMarkerIdx].iconAnchor = new GPoint(6, 20);
        this.oMarkerIcon[this.iMarkerIdx].infoWindowAnchor = new GPoint(20, 6);

        //Check for IE -> Marker Zindex is not working in IE
        if (jQuery.browser.msie) {
            this.oMarker[this.iMarkerIdx] = new GMarker(oTmpCoords, this.oMarkerIcon[this.iMarkerIdx]);
        }
        else {
            function importanceOrder(marker, b) {
                return this.iMarkerIdx;

            }
            var markerOptions = { icon: this.oMarkerIcon[this.iMarkerIdx], zIndexProcess: importanceOrder };
            this.oMarker[this.iMarkerIdx] = new GMarker(oTmpCoords, markerOptions);
        }
        this.oMarker[this.iMarkerIdx].sInfoWindowText = psInfoWindowText;
        this.oMarker[this.iMarkerIdx].oParentMap = this.oMap;
        this.oMarker[this.iMarkerIdx].oIcon = this.oMarkerIcon[this.iMarkerIdx];
        this.oMarker[this.iMarkerIdx].iIdx = this.iMarkerIdx;

        this.oMap.addOverlay(this.oMarker[this.iMarkerIdx]);

        /**
        * Over und Out der Marker
        * @param - bool - Status der visualisiert werden soll
        */
        this.oMarker[this.iMarkerIdx].do_hover = function(psStatus, pbCallByLink) {
            if (psStatus) {
                this.setImage(this.oIcon.imageSrcOn);
                if (!pbCallByLink && (this.oSearchResultDetail && this.oSearchResultDetail)) do_result_detail_hover(this.oSearchResultDetail, 1, 1);
                //this.openInfoWindowHtml(this.sInfoWindowText);
            }
            else {
                this.setImage(this.oIcon.imageSrcOff);
                if (!pbCallByLink && (this.oSearchResultDetail && !this.oSearchResultDetail.bStatus)) do_result_detail_hover(this.oSearchResultDetail, 0, 1);
                //this.closeInfoWindow();
            }
        }

        GEvent.addListener(this.oMarker[this.iMarkerIdx], "mouseover", function() {
            this.do_hover(1);
        });

        GEvent.addListener(this.oMarker[this.iMarkerIdx], "mouseout", function() {
            if (this.oSearchResultDetail && this.oSearchResultDetail.bStatus) return;
            this.do_hover(0);
        });

        /**
        * Click AS Marker
        */
        this.oMarker[this.iMarkerIdx].do_toggle_info = function(psStatus) {
            if (psStatus) {
                this.openInfoWindowHtml(this.sInfoWindowText);
                //                this.openInfoWindowHtml(
                //                              this.oMap,
                //                              "extInfoWindow_Box",
                //                              "<p>I'm a funky ExtInfoWindow with HTML content.</p>",
                //                              {beakOffset: 1}
                //                );
            }
            else {
                this.closeInfoWindow();
            }
        }

        if (piMarkerType != 1) {
            GEvent.addListener(this.oMarker[this.iMarkerIdx], "click", function() {
                this.do_toggle_info(1);
            });
        }
    }

    this.do_fit2marker = function() {
        var oBounds = new GLatLngBounds;

        for (var i = 0; i < this.oMarker.length; i++) {
            oBounds.extend(this.oMarker[i].getPoint());
        }

        if (!this.oMap) return;

        this.oMap.setZoom(this.oMap.getBoundsZoomLevel(oBounds));
        this.oMap.setCenter(oBounds.getCenter());
    }

    this.clearMarker = function() {
        GEvent.clearNode(this.oMapObject);
        this.oMap.clearOverlays();

        this.iMarkerIdx = -1;
        this.oMarker = new Array();
        this.oMarkerIcon = new Array();
    }
}
