Drupal.gmap.addHandler('gmap', function(elem) {
  var obj = this;
  // Respond to incoming movements.
  // Make it a gmap.
  // Hide away a reference to the map
  //obj.map = map;
  obj.bind('init', function() {

    GEvent.addListener(obj.map, "click", function(overlay, latlng) {
      var lat = obj.vars.latitude;
      var lng = obj.vars.longitude;
      var zoom = Lat49.Tile.convertGMap2Zoom(obj.vars.zoom);
      Lat49.updateAdByLatLon('adcontainer',lat,lng,zoom);
      });
    GEvent.addListener(obj.map, "moveend", function(overlay, latlng) {
      var lat = obj.vars.latitude;
      var lng = obj.vars.longitude;
      var zoom = Lat49.Tile.convertGMap2Zoom(obj.vars.zoom);
      Lat49.updateAdByLatLon('adcontainer',lat,lng,zoom);
      });
  
  });
});

var AdPushpin = function() {
  //private members
  var map = null;
  var markersArray = new Array();
  //public members
  return {
    // Set the map variable and define the pushpin icon.
    setMap:function(m)
    {
      map = m;
    },
    //Update the marker info with a title and address
    updateMarker:function(marker,title, address)
    {
      GEvent.addListener(marker, "click", function() {
      marker.openInfoWindowHtml("<b>" + title + "</b><br> " + address);});
    },
    //Hide all the pushpins
    hidePushPin:function()
    {
      for (var i = 0; i < markersArray.length; i++)
      {
        map.removeOverlay(markersArray[i]);
      }
      markersArray = new Array();
    },
    //Push new markers into the markersArray and display them on the map.
    //The parameter is an array of objects that include latitude, longitude, title, address, and pinurl.
    //var pin-lat = data[i].lat.
    //var pin-lon = data[i].lon.
    //var pin-title = data[i].title.
    //var pin-address = data[i].address.
    //var pin-url = data[i].pinurl.
    showPushPin:function(loc)
    {
      var maxpins = 7;
      for (var i=0; i<loc.length;i++)
      {
        if(i<=maxpins)
        {
          var point = new GLatLng(loc[i].lat, loc[i].lon);
          var adicon = new GIcon(G_DEFAULT_ICON);
          adicon.image = loc[i].pinurl;
          adicon.shadow ="";
          adicon.iconSize = new GSize(36,32);
          var marker = new GMarker(point,{clickable: true, bouncy: true, icon:adicon});
          map.addOverlay(marker);
          // add markers to array
          markersArray.push(marker);
          this.updateMarker(marker, loc[i].title, loc[i].address);
        }
      }
    } 
  }
}();