
if (typeof freewave == "undefined") {
   var freewave = {};
}

freewave.GoogleMap = { 
   markers: {},
   map: null,
   icon: null,
   initialLat: 46.879513,
   initialLng: 13.040771,
   initialZoom: 7,
   detailZoom: 15,
   
   addMarker: function(hash, lat, lng, title, infoHtml) {
      this.markers[hash] = new GMarker(new GLatLng(lat, lng), {
         title: title,
         icon: this.getIcon()
      });
      
      GEvent.addListener(this.markers[hash], "click", function() {
         this.openInfoWindowHtml(infoHtml);
      });
   },
   
   jumpTo: function(markerHash) {
      var marker = this.markers[markerHash];

      if (marker && this.map) {
         this.map.setZoom(this.detailZoom);
         this.map.panTo(marker.getPoint());
         GEvent.trigger(marker, "click");
      }
   },
   
   load: function(mapId, mapControl, typeControl, initalMarker) {
      if (GBrowserIsCompatible()) {
         this.map = new GMap2($(mapId));

         if (mapControl) {
            this.map.addControl(new GSmallMapControl());
         }
         
         if (typeControl) {
            this.map.addControl(new GMapTypeControl());  
         }

         if (initalMarker) {
            var marker = this.markers[initalMarker];
            this.map.setCenter(marker.getPoint(), this.detailZoom);
         } else {
            this.map.setCenter(new GLatLng(this.initialLat, this.initialLng), this.initialZoom);
         }

         for (var i in this.markers) {
            this.map.addOverlay(this.markers[i]);
         }

         if (location.search.indexOf("show") != -1) {
            this.jumpTo(location.search.split("=")[1]);
         } else if (initalMarker != null) {
            this.jumpTo(initalMarker);
         }
      }      
   },
   
   getIcon: function() {
      if (!this.icon) {
         this.icon = new GIcon();
         this.icon.image = "/images/googlemap_icon.png";
         this.icon.iconSize = new GSize(44, 42);
         this.icon.shadow = "/images/googlemap_icon_shadow.png";
         this.icon.shadowSize = new GSize(44, 42);
         this.icon.iconAnchor = new GPoint(21, 38);
         this.icon.infoWindowAnchor = new GPoint(22, 22);
      }
      
      return this.icon;
   }
};

Event.observe(window, "unload", function(e) {
   GUnload();
});
