;(function() {

var create_marker_for_map = function(map, business, extra) {
    extra = extra || {}
    var pos = new google.maps.LatLng(business.latitude, business.longitude)
      , shadow = business.icon_shadow && business.icon_shadow.length ? new google.maps.MarkerImage(
            business.icon_shadow
          , new google.maps.Size(32, 32)
          , new google.maps.Point(0, 0)
          , new google.maps.Point(0, 32) ) : undefined
      , icon = new google.maps.MarkerImage(
            business.icon_image
          , new google.maps.Size(business.icon_width, business.icon_height)
          , new google.maps.Point(0, 0)
          , new google.maps.Point(business.icon_width/2, business.icon_height) )
      , markeroptions = jQuery.extend(extra, 
                        { 'position': pos
                        , 'map'     : map
                        , 'shadow'  : shadow
                        , 'title'   : 'Business'
                        , 'icon'    : icon })
      , marker = new google.maps.Marker(markeroptions)
  return marker
};

jQuery.fn.map_detail = function(business) {
  return this.each(function(idx, el) {
    var pos = new google.maps.LatLng(business.latitude, business.longitude)
      , options = { 'zoom'      :15
                  , 'center'    :pos
                  , 'mapTypeId' :google.maps.MapTypeId.ROADMAP }
      , map = new google.maps.Map(el, options)

    create_marker_for_map(map, business)
  }) 
};

jQuery.fn.map_results = function(list) {
  var min 

  return this.each(function(idx, el) {
    var bounds = new google.maps.LatLngBounds()
    jQuery(list).each(function(business_idx, business) {
      business.latlng = new google.maps.LatLng(
        business.latitude, business.longitude
      )
      bounds.extend(business.latlng)
    })
    
    var options = { 'zoom'      :15
                  , 'center'    :bounds.getCenter()
                  , 'mapTypeId' :google.maps.MapTypeId.ROADMAP }
      , map = new google.maps.Map(el, options)

    map.fitBounds(bounds)
    var bubbles = []
    jQuery(list).each(function(business_idx, business) {
      var content = '<div class="marker_balloon">'+
                    '<h4>'+business.name.anchor(business.url || '#').replace('name=', 'href=')+'</h4>' +
                    (business.address ? '<p class="address">'+business.address+'</p>' : '') +
                    (business.phone ? '<p class="phone">'+business.phone+'</p>' : '') +
                    '</div>' 
        , info_option = { 'content':content }
        , info = new google.maps.InfoWindow(info_option)

      bubbles.push(info)
      var marker = create_marker_for_map(map, business)
        , locate = $('#marker_'+business.id)
      google.maps.event.addListener(info, 'closeclick', function() {
        locate.removeClass('on')
      })
      
      
      locate.click(function(ev) {
        ev.preventDefault()
        $('.place_list a.locate').removeClass('on')
        locate.addClass('on')
        for(var i = 0, len = bubbles.length; i < len; ++i) if(bubbles[i] !== info) {
          bubbles[i].close()
        } 
        info.open(map, marker)
      })

      google.maps.event.addListener(marker, 'click', function() {
        locate.addClass('on')
        for(var i = 0, len = bubbles.length; i < len; ++i) if(bubbles[i] !== info) {
          bubbles[i].close()
        } 
        info.open(map, marker)
      })
    })    
  })
};

/*
 * Map Results
 * Returns a map with plotted points.
 */
jQuery.fn.geoxml_map_results = 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];
    }
  }
}

})();

