var Ellington = window.Ellington || {};


/*
 * GeoMicroFormat
 */


Ellington.GeoMicroFormat = function() {
  var inactive_image = STATIC_MEDIA_URL+'ellington_defaults/2.2.0/images/maps/marker.png';
  var shadow_image = STATIC_MEDIA_URL+'ellington_defaults/2.2.0/images/maps/marker_shadow.png';
  var active_image = STATIC_MEDIA_URL+'ellington_defaults/2.2.0/images/maps/marker_hover.png';

  function addMarker(point, map) {
    var position = $(point).attr('title').split(';');
    var latlng = new google.maps.LatLng(position[0], position[1]);

    var marker = new google.maps.Marker({
      'inactive': new google.maps.MarkerImage(inactive_image,
        new google.maps.Size(23, 32),
        new google.maps.Point(0, 0),
        new google.maps.Point(11, 32)
      ),
      'shadow': new google.maps.MarkerImage(shadow_image,
        new google.maps.Size(25, 16),
        new google.maps.Point(0, 0),
        new google.maps.Point(0, 16)
      ),
      'active': new google.maps.MarkerImage(active_image,
        new google.maps.Size(23, 32),
        new google.maps.Point(0, 0),
        new google.maps.Point(11, 32)
      ),
      'position': latlng,
      'map': map
    });
    marker.setIcon(marker.inactive);
    return marker;
  }

  return function(element) {
    var maps = $(element);
    var marker_list = [];

    maps.each(function(i) {
      var points = $('abbr', this);
      var latlng = new google.maps.LatLng(37.032, -95.617);
      var bounds = new google.maps.LatLngBounds();

      // Create map.
      var map = new google.maps.Map(this, {
        zoom: 15,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        scrollwheel: false,
        mapTypeControl: false,
        navigationControl: true,
        navigationControlOptions: {
          style: google.maps.NavigationControlStyle.SMALL
        }
      });

      // Add points to map.
      points.each(function(j) {
        var marker = addMarker(this, map);
        marker_list.push(marker);
        bounds.extend(marker.getPosition());
      });

      // If we use fitBounds with a single marker the map will zoom in to
      // much so we just center the map in that case.
      if (marker_list.length > 1) {
        map.fitBounds(bounds);
      }
      else {
        map.setCenter(marker_list[0].getPosition());
      }
    });
  }
}();


jQuery(document).ready(function($) {
  Ellington.GeoMicroFormat('.map');
});
