﻿
gmap = 
{
  map                 : null,
  mapContainer        : null,
  places              : null,
  markers             : [],
  mouseoverListeners  : [],
  mouseoutListeners   : [],
  loadedMarkerCount   : 0,
  tooltip             : null,
  customMarkerIcon    : null,
  
  init : function(placeData, enableMap, mapWidth, mapHeight, mapZoomLevel)
  {
    if (enableMap)
    {
      this.mapContainer = $('.map');
      
      var width = (typeof(mapWidth) != 'undefined' && mapWidth != null) ? mapWidth : 300;
      var height = (typeof(mapHeight) != 'undefined' && mapHeight != null) ? mapHeight : 175;
      var zoomLevel = (typeof(mapZoomLevel) != 'undefined' && mapZoomLevel != null) ? mapZoomLevel : 13;
      
      this.mapContainer.width(width);
      this.mapContainer.height(height);
      
      this.map = new GMap2(this.mapContainer[0]);
      this.map.setCenter(new GLatLng(40.2292920,-74.9367020), zoomLevel);
      this.map.addControl(new GSmallZoomControl3D());
      //this.map.addControl(new GMapTypeControl());
      
      this.customMarkerIcon = new GIcon(G_DEFAULT_ICON);
      this.customMarkerIcon.iconSize = new GSize(16, 16);
      this.customMarkerIcon.image = "http://www.atasteofnewtown.com/includes/images/flower.png";
      this.customMarkerIcon.shadow = "http://www.atasteofnewtown.com/includes/images/shadow-flower.png";
      this.customMarkerIcon.shadowSize = new GSize(25, 16);
      this.customMarkerIcon.iconAnchor = new GPoint(8, 8);

      this.tooltip = $("<div id='tooltip'></div>");
      this.mapContainer.append(this.tooltip);
      this.tooltip.hide();

      this.places = placeData;
      this.loadMarkers();
      
      $('H1.name').each(function(i) {
        $(this).bind('mouseenter', {index:i}, function(event){
          var marker = gmap.markers[event.data.index];
          
          if (typeof(marker) != 'undefined')
          {
            if (gmap.map.getBounds().containsLatLng(marker.getLatLng()))
            {
              gmap.showTooltip(marker);
            }
          }
        }).bind('mouseleave', function(){
          gmap.tooltip.hide();
        });
      });
    }

  },
  
  loadMarkers : function()
  {
    var place = null;
    var point = null;
    var marker = null;

    for(var p = 0; p < gmap.places.length; p++)
    {
      place = gmap.places[p];
      point = new GLatLng(place.Marker.Coordinates.Longitude, place.Marker.Coordinates.Latitude);
      marker = new GMarker(point, { icon: gmap.customMarkerIcon });
      marker.tooltip = '<div class="tooltip">' + place.Name + '<\/div>';
      
      gmap.map.addOverlay(marker);
      gmap.markers.push(marker);
    }

    // load mouseovers
    $(gmap.markers).each(function(i, marker){
      gmap.mouseoverListeners[i] = 
        GEvent.addListener(marker, "mouseover", function(){
          gmap.showTooltip(marker);
        });   
    });

    // load mouseouts
    $(gmap.markers).each(function(i, marker){
      gmap.mouseoutListeners[i] = 
        GEvent.addListener(marker, "mouseout", function(){
          gmap.tooltip.hide();
        });
    });

  },
  
  
  showTooltip : function (marker) {
  	gmap.tooltip.html(gmap.prettyTooltip(marker.tooltip));
    
    var point=gmap.map.getCurrentMapType().getProjection().fromLatLngToPixel(gmap.map.getBounds().getSouthWest(),gmap.map.getZoom());
    var offset=gmap.map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),gmap.map.getZoom());
    var anchor=marker.getIcon().iconAnchor;
    var width=marker.getIcon().iconSize.width;
    var adjX = 5;
    var adjY = 5; //40;
    //(offset.y + gmap.tooltip.height())
    var pos = new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(offset.x - point.x - anchor.x + width - adjX,- offset.y + point.y +anchor.y - adjY)); 
    
    pos.apply(gmap.tooltip[0]);
    gmap.tooltip.show();
  },
  
  prettyTooltip : function(tooltip)
  {
    return tooltip;
  },

  
  isValidMarker : function(marker)
  {
    return (marker.Coordinates != null && marker.Coordinates.length > 0);
  },
  
  buildAddressQuery : function(place)
  {
    var addr = '';
    addr += place.Address1.replace(/\ /g, "\+");
    
    if (place.Address2 != null && $.trim(place.Address2).length > 0)
    {
      addr += '+' + place.Address2.replace(/\ /g, "\+"); 
    }
    
    addr += '+' + place.City.replace(/\ /g, "\+"); 
    addr += '+' + place.State; 
    addr += '+' + place.Zip;
    
    addr += '?' + place.ID;
    
    return addr;
  },
  
  getPlace : function(placeID)
  {
    for(p = 0; p < gmap.places.length; p++)
    {
      if (gmap.places[p].ID == placeID)
      {
        return gmap.places[p];
      }
    }
    
    return null;
  }
}
