var LineasMap = Class.create ({
  /**
   * Initialize map
   * @param options object specifiying these options:
   *        mapContainer:  map container element
   *        lat:           map center latitude
   *        lng:           map center longitude
   *        zoom:          initial map zoom
   *        width:         map width in pixels
   *        height:        map height in pixels
   *        title:         map center marker tooltip
   *        info:          map center marker info window contents
   *        customers:     array of customers with these attributes:
   *                       lat:      customer marker latitude
   *                       lng:      customer marker longitude
   *                       imageUrl: customer marker image URL
   *                       title:    customer marker text
   */
  initialize: function (options) {
    if (google.maps.BrowserIsCompatible ()) {
      this.mapContainer     = options.mapContainer;
      this.center           = new google.maps.LatLng (options.lat || 52.255307, options.lng || 10.518047);
      this.zoom             = options.zoom || 13;
      this.info             = options.info;
      this.size             = new google.maps.Size (options.width || 640, options.height || 480);
      this.title            = options.title || "LINEAS Informationstechnik GmbH";
      this.customers        = options.customers || [];
      this.customersVisible = false;

      // init map
      this.map = new google.maps.Map2 ($(this.mapContainer), {size: this.size});
      this.map.enableScrollWheelZoom ();
      this.map.setCenter  (this.center, this.zoom);
      this.map.addControl (new google.maps.LargeMapControl ());
      this.map.addControl (new google.maps.HierarchicalMapTypeControl ());
      this.markerBounds = new google.maps.LatLngBounds ();

      // init company marker
      var lineasMarker = new google.maps.Marker (this.map.getCenter (), {title: this.title});
      lineasMarker.bindInfoWindowHtml (this.info);
      this.map.addOverlay (lineasMarker);
      google.maps.Event.trigger (lineasMarker, "click");
      this.markerBounds.extend (this.map.getCenter ());

      // init customer markers
      this.customerMarkers = [];
      this.customers.each (function (customer) {
        var markerPosition = new google.maps.LatLng (customer.lat, customer.lng);
        var marker = new google.maps.Marker (markerPosition, {title: customer.title});
        marker.bindInfoWindowHtml ("<div class='markerInfo'>" +
                                   "<div class='infoHeader'>" + customer.title + "</div>" +
                                   "<div><img src='" + customer.imageUrl + "'/></div>" +
                                   "</div>");
        this.map.addOverlay (marker);
        marker.hide ();
        this.customerMarkers.push (marker);
        this.markerBounds.extend (markerPosition);
      }, this);

      // add destructor call
      Event.observe (window, "unload", function () {
        this.destroy ();
      }.bind (this));
    }
  },

  /**
   * Toggles visibility of customer markers.
   */
  toggleCustomers: function () {
    this.customersVisible = !this.customersVisible;
    if (this.customersVisible)
    {
      // show customer markers
      this.map.setCenter (this.markerBounds.getCenter (), this.map.getBoundsZoomLevel (this.markerBounds));
      this.customerMarkers.each (function (marker) {
        marker.show ();
      });
    }
    else
    {
      // hide customer markers
      this.customerMarkers.each (function (marker) {
        marker.hide ();
      });
    }
  },

  /**
   * Destroys map.
   */
  destroy: function () {
    google.maps.Unload ();
  }
});
