/**
 *
 */
function GoogleMap(mapCenterLatitude, mapCenterLongitude)
{
    var _mapObj             = null;
    var _mapZoomFactor      = 16;
    var _mapCenterLatitude  = parseFloat(mapCenterLatitude);
    var _mapCenterLongitude = parseFloat(mapCenterLongitude);
    var _mapMarkers         = [];
    
    /**
     * 
     */
    this.getMarkers = function()
    {
      return _mapMarkers;
    }
    
    /**
     *
     */
    this.setMarker = function(markerObj)
    {
      _mapMarkers.push(markerObj);
    }
    
    /**
     * Liefert die Google-Map
     */
    this.getMap = function()
    {
      return _mapObj;
    }
    
    /**
     * Setzt das Google-Map Objekt
     */
    this.setMap = function(newMapObj)
    {
      _mapObj = newMapObj;
    }
    
    this.getMapCenterLatitude  = function() { return _mapCenterLatitude;  }
    this.getMapCenterLongitude = function() { return _mapCenterLongitude; }
    
    /**
     *
     */
    this.getMapCenter = function()
    {
      return new GLatLng(_mapCenterLatitude, _mapCenterLongitude);
    }
    
    /**
     * Liefert den aktuellen Zoomfaktor
     */
    this.getZoom = function()
    {
      return _mapZoomFactor;
    }
    
    /**
     * Setzt einen neuen Zoomfaktor
     */
    this.setZoom = function(newZoom)
    {
      _mapZoomFactor = newZoom;
    }
}

/**
 * Setzt das Marker-Icon
 */
GoogleMap.prototype.createIcon = function(iconUrl, iconWidth, iconHeight)
{
    var mapIcon = new GIcon();
    
    mapIcon.image            = iconUrl;
    mapIcon.iconAnchor       = new GPoint(16, 16);
    mapIcon.infoWindowAnchor = new GPoint(16,  0);
    mapIcon.iconSize         = new GSize(iconWidth, iconHeight);
    
    return mapIcon;
}

/**
 * Funktion setzt den Marker
 */
GoogleMap.prototype.addCenterMarker = function(infoHtml, markerIcon)
{
    this.addMarker(this.getMapCenterLatitude(), this.getMapCenterLongitude(),
        infoHtml, markerIcon, true);
}

/**
 * Funktion setzt den Marker
 */
GoogleMap.prototype.addMarker = function(markerLat, markerLong,
    infoHtml, markerIcon, showInfoHtml)
{
    var markerObj = new Object();
    
    markerObj['location'] = new GLatLng(markerLat, markerLong);
    
    // Markierung erzeugen
    if (markerIcon != null)
        markerObj['marker'] = new GMarker(markerObj['location'], { icon: markerIcon });
    else
        markerObj['marker'] = new GMarker(markerObj['location']);
    
    markerObj['infoHtml'] = infoHtml;
    
    if ((showInfoHtml != null) && showInfoHtml) {
        this.getMap().openInfoWindowHtml(markerObj['location'], markerObj['infoHtml']);
    }
    
    // ein onClick-Event für den Marker einfügen der das Infofenster öffnet
    GEvent.addListener(markerObj['marker'], "click",
        function() { markerObj['marker'].openInfoWindowHtml(markerObj['infoHtml']) }
    );
    
    // Marker in Liste eintragen
    this.setMarker(markerObj);
    
    // Markierung eintragen
    this.getMap().addOverlay(markerObj['marker']);
}

/**
 * Funktion zentriert die Karte auf die Koordinaten
 */
GoogleMap.prototype.setCenter = function(centerPoint)
{
    // Karte auf Punkte zentrieren
    this.getMap().setCenter(centerPoint, true);
}

/**
 * Funktion zentriert die Karte auf die Koordinaten
 */
GoogleMap.prototype.setCenterLocation = function()
{
    // Karte auf Punkte zentrieren
    this.getMap().setCenter(this.getMapCenter(), true);
    this.getMap().setZoom(this.getZoom());
}

/**
 * Funktion erzeugt ein Google-Map-Objekt
 */
GoogleMap.prototype.create = function(containerId)
{
    if ((this.getMap() == null) && GBrowserIsCompatible())
    {
        var containerElement = document.getElementById(containerId);
        
        if (containerElement)
        {
            // Google Map Objekt erzeugen
            this.setMap(new GMap2(containerElement));
            this.setCenterLocation();
            
            var mapObj = this.getMap();
            
            // Satellitenkarte
            mapObj.setMapType(G_SATELLITE_MAP);
            mapObj.enableDoubleClickZoom();
            mapObj.enableScrollWheelZoom();
            
            // Kontrollelemente einfügen
            mapObj.addControl(new GLargeMapControl());  // Großes Zoomelement
            mapObj.addControl(new GMapTypeControl());   // Karte, Satellit, Hybrid
            
            // �bersichtskarte rechts unten
            overviewMap = new GOverviewMapControl();
            overviewMap.setMapType(G_SATELLITE_MAP);
            
            mapObj.addControl(overviewMap);
        }
    }
}

/**
 * Lädt eine Liste von Hotels (inkl. Geo-Koordinaten) von der angegebenen Url
 * und trägt sie in der Karte ein.
 */
GoogleMap.prototype.downloadMarkerList = function(baseUrl, queryPart, iconUrl, iconWidth, iconHeight)
{
    var googleMapObj   = this;    
    var downloadFinish = function(responseBody, responseStatus)
    {
        var hotelList = eval('(' + responseBody +  ')');
        
        for (var i = 0; i < hotelList.length; ++i)
        {
            var newIcon = null;
            
            if (iconUrl != null)
                newIcon = googleMapObj.createIcon(iconUrl, iconWidth, iconHeight);
            
            var infoHtml = "<table class=\"googleMap\" border=\"0\" cellspacing=\"2\" cellpadding=\"3\">" +
                           "<tr style=\"height: 32px;\">" +
                           "<td><img src=\"" + baseUrl + "images/bed.jpg\" alt=\"\" class=\"hotelIcon\" \/><\/td>" +
                           "<td><span class=\"hotelText\"><a target=\"_blank\" href=\"" + baseUrl + hotelList[i].hotelLink + "\" title=\"zum Hotel\">Hotel " + hotelList[i].hotelName + "<\/a><\/span><\/td>" +
                           "<\/tr><\/table>";
            
            googleMapObj.addMarker(
                hotelList[i].hotelLatitude,
                hotelList[i].hotelLongitude,
                infoHtml,
                newIcon);
        }
    }
    
    GDownloadUrl(baseUrl + queryPart, downloadFinish);
}

