/*
 * Map Detail
 * Returns a map with a single plotted point.
 */
jQuery.fn.map_detail = function(p_business_obj, mapDefaults) {
  var mapDefaults = {};

  return this.each(function() {
    if (GBrowserIsCompatible()) {
      var map = new GMap2(this);
      var business = p_business_obj;

      map.addControl(new GSmallZoomControl());
      map.addControl(new GMenuMapTypeControl());
      map.setCenter(new GLatLng(business.latitude, business.longitude), 15);

      var point = new GLatLng(business.latitude, business.longitude);

      var icon = new GIcon();
      icon.image = business.icon_image;
      icon.iconSize = new GSize(business.icon_width, business.icon_height);
      icon.iconAnchor = new GPoint(business.icon_width/2, business.icon_height);
      icon.infoWindowAnchor = new GPoint(0, 0);
      icon.shadow = business.icon_shadow;

      var marker = new GMarker(point, icon);
      map.addOverlay(marker);
    }
  });
}


/*
 * Map Results
 * Returns a map with plotted points.
 */
jQuery.fn.map_results = function(p_business_list, mapDefaults) {
  var mapDefaults = {};

  return this.each(function() {
    if (GBrowserIsCompatible()) {
      var current_marker = null;
      var map = new GMap2(this);
      var business_list = p_business_list;
      var marker_list = [];
      var bounds = new GLatLngBounds();

      map.addControl(new GSmallZoomControl());
      map.addControl(new GMenuMapTypeControl());
      map.setCenter(new GLatLng(0, 0), 0);

      var manager = new MarkerManager(map);

      /*
       * Pane Open
       * Displays the current markers overlay and triggers the
       * map:paneOpened event.
       */
      function paneOpen(e, marker) {
        if (current_marker) {
          jQuery(document).trigger('map:paneClose');
        }
        if(typeof marker == 'string') {
          current_marker = find_object(marker_list, 'id', marker);
        }
        else {
          current_marker = marker;
        }
        current_marker.openInfoWindowHtml(current_marker.marker_balloon, {maxWidth: 200});
        jQuery(document).trigger('map:paneOpened', current_marker);
      }

      /*
       * Close Pane
       * Removes the current_maker's overlay from map and clears out
       * the current_marker variable.
       */
      function paneClose() {
        if (current_marker) {
          current_marker.closeInfoWindow();
        }
        current_marker = null;
      }

      /*
       * Map Resize
       * Listens for map:resize trigger so the map can refresh itself.
       */
      function mapResize(e) {
        map.checkResize();
        map.setCenter(bounds.getCenter());
      }

      jQuery(document).bind('map:resize', mapResize);
      jQuery(document).bind('map:paneOpen', paneOpen);
      jQuery(document).bind('map:paneClose', paneClose);

      for (var i=0; i<business_list.length; i++) {
        var business = business_list[i];
        if ((business.latitude && business.longitude) && (business.latitude != 'None' && business.longitude != 'None')) {
          var point = new GLatLng(business.latitude, business.longitude);
          var icon = new GIcon({
            image: business.icon_image,
            iconSize: new GSize(business.icon_width, business.icon_height),
            iconAnchor: new GPoint(business.icon_width/2, business.icon_height),
            infoWindowAnchor: new GPoint(15, 5),
            shadow: business.icon_shadow
          });

          var marker_balloon = '<div class=\"marker_balloon\">';
          if (business.url) {
            marker_balloon += '<h4><a href="'+business.url+'">'+business.name+'</a></h4>';
          }
          else {
            marker_balloon += '<h4>'+business.name+'</h4>';
          }
          if (business.address) {
            marker_balloon += '<p class="address">'+business.address+'</p>';
          }
          if (business.phone) {
            marker_balloon += '<p class="phone">'+business.phone+'</p>';
          }
          marker_balloon += '</div>';

          var marker = new GMarker(point, {icon: icon});
          marker.id = business.id;
          marker.marker_balloon = marker_balloon;

          GEvent.addListener(marker, 'click', function() {
            jQuery(document).trigger('map:paneOpen', [this]);
          });
          marker_list.push(marker);
          bounds.extend(point);
        }
      };

      map.setZoom(map.getBoundsZoomLevel(bounds));
      map.setCenter(bounds.getCenter());
      manager.addMarkers(marker_list, 1);
      manager.refresh();
    }
  });
}


/*
 * Map Results
 * Returns a map with plotted points.
 */
jQuery.fn.geoxml_map_results = function(p_obj, mapDefaults) {
  var mapDefaults = {};

  return this.each(function() {
    if (GBrowserIsCompatible()) {
      var map = new GMap2(this);
      map.addControl(new GSmallZoomControl());
      map.addControl(new GMenuMapTypeControl());
      map.setCenter(new GLatLng(p_obj.latitude, p_obj.longitude), p_obj.zoom);
      map.addOverlay(p_obj.geo_xml);
    }
  });
}


/*
 * Find object
 * Handy function to return an object within a list.
 */
function find_object(object_list, key, value) {
  for (var i=0; i<object_list.length; i++) {
    if (object_list[i][key] == value) {
      return object_list[i];
    }
  }
}