var UrpagesMap = {
  map: null,
  infoWindow: null,
  bounds: null,
  markers: {},



  init : function(center,zoom,mapCanvasId) {
    /**
    * Called only once on initial page load to initialize the map.
    */
    $.log("Initialising GoogleMaps")
    centerLatLng = new google.maps.LatLng(center.lat, center.lng);

    // Create single instance of a Google Map.
    $.log("Creating map instance")
    this.map = new google.maps.Map(document.getElementById(mapCanvasId), {
      zoom: zoom,
      center: centerLatLng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    // Create a single instance of tmarkerhe InfoWindow object which will be shared
    // by all Map objects to display information to the user.
    $.log("Creating infoWindow instance")
    this.infoWindow = new google.maps.InfoWindow();

  // Make the info window close when clicking anywhere on the map.
    $.log("Attaching event listeners")
    google.maps.event.addListener(this.map, 'click', this.closeInfoWindow);
  },



  closeInfoWindow : function() {
    /*
  * Called when clicking anywhere on the map and closes the info window.
    */
    $.log("Closing infoWindow")
    this.infoWindow.close();
  },



  openInfoWindowId : function(markerid){
    $.log("Opening infoWindow from ID", markerid)
    this.openInfoWindow(this.markers[markerid]);
  },



  openInfoWindow : function(marker) {
    $.log("Opening infoWindow from object", marker)
    /**
    * Opens the shared info window, anchors it to the specified marker, and
    * displays the marker's position as its content.
    */
    var markerLatLng = marker.getPosition();
    this.infoWindow.setContent(
    $("<div>")
    .addClass("gmap-bubble")
    .html( marker.getTitle() )
    );
    this.infoWindow.open(this.map, marker);
  },



  initOneMarker : function(lat,lng,zoom,mapCanvasId,title){
    $.log("Initialising a marker : ", lat,lng,zoom,mapCanvasId,title)

    var center = {lat:lat,lng:lng};
    this.init(center,zoom,'map-canvas');
    var centerLatLng = new google.maps.LatLng(lat,lng);
    var marker = new google.maps.Marker({
      map: this.map,
      position: centerLatLng,
      title: String(title)
    });
    // Register event listeners to each marker to open a shared info
    // window displaying the marker's position when clicked or dragged.
    google.maps.event.addListener(marker, 'click', function() {
      this.openInfoWindow(marker);
    });

  }

}


