/**
 * Class GoogleMap Control
 */
function clsGoogleMap()
{
    this.iMarkerIdx  = -1;
    this.oMarker     = new Array();
    this.oMarkerIcon = new Array();
    this.oMap        = null;
    this.oMapObject  = null;
    /**
     * initialisiert die 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.addControl(new GSmallMapControl());
        }
        this.clearMarker();

        this.oMap.setCenter(new GLatLng(poCoords.lat, poCoords.lng), 15);
        if (typeof (document.getElementById(psMapId + '_description')) == 'object') {
            this.oDirectionsPanel = document.getElementById(psMapId + '_description');
        }

        /**
        * Berechnet die GoogleMap-Route und zeigt sie an
        */
        this.do_calculate_route = function(poRoute) {
            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;
        }
    }

    /**
     * Malt die 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); //var oPos = new GLatLng(53.55494344, 9.98970509);

        this.oMarkerIcon[this.iMarkerIdx] = new GIcon();
        if (piMarkerType == 1) {
            this.oMarkerIcon[this.iMarkerIdx].imageSrcOff = 'images/gm_marker_userposition_off.png';
            this.oMarkerIcon[this.iMarkerIdx].imageSrcOn = 'images/gm_marker_userposition_on.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(30, 33);
            this.oMarkerIcon[this.iMarkerIdx].shadowSize = new GSize(30, 33);
        }
        else if (piMarkerType == 2) {
            this.oMarkerIcon[this.iMarkerIdx].imageSrcOff = 'images/gm_marker_default_off.png';
            this.oMarkerIcon[this.iMarkerIdx].imageSrcOn = 'images/gm_marker_default_on.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(38, 38);
            this.oMarkerIcon[this.iMarkerIdx].shadowSize = new GSize(38, 38);
        }
        this.oMarkerIcon[this.iMarkerIdx].iconAnchor = new GPoint(6, 20);
        this.oMarkerIcon[this.iMarkerIdx].infoWindowAnchor = new GPoint(20, 6);

        this.oMarker[this.iMarkerIdx] = new GMarker(oTmpCoords, this.oMarkerIcon[this.iMarkerIdx]);
        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 auf Marker
        */
        this.oMarker[this.iMarkerIdx].do_toggle_info = function(psStatus) {
            if (psStatus) {
                this.openInfoWindowHtml(this.sInfoWindowText);

                if (this.oSearchResultDetail && !this.oSearchResultDetail.bStatus)
                    do_toggle_result_detail(document.getElementById('tr_result' + this.iIdx));
            }
            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();
    }
}