/* 
 *  Capitol Decisions Inc. Client Map
 *  Site by Eli Van Zoeren - http://elivz.com
 */

$(document).ready(function() {

  // Client map
  if ( $('#clientMap').length ) {
    $('#clientMap').vzMapper('#clientList', {
      controls : {
          pan: true,
          zoom: 'large',
          map_type: false
        },
      markerIcon: 'http://www.capitoldecisions.com/images/map-icon.png',
      markerIconSize: [29, 46],
      markerIconAnchor: [14, 4]
    });
  }

});


//
// Use the Mapstraction library to create a Yahoo
// map from a list of locations
//
(function($) {
  $.fn.vzMapper = function(source, options) {
    var opts = $.extend({}, $.fn.vzMapper.defaults, options);
    $source = $(source);
    
    // Iterate matched elements
    return this.each(function() {
      $map = $(this);
      
      // Initialize the map
      var mapstraction = new mxn.Mapstraction($map.attr('id'), opts.provider);
      mapstraction.addControls(opts.controls);
      mapstraction.setMapType(opts.mapType);
      mapstraction.getMap().disableKeyControls();
      
      // Add the markers
      $(opts.sourceItemSelector, $source).each(function() {
        var $this = $(this);
        var data = $this.metadata({type:'attr', name:'point'});
        if (data.lat && data.lng) {
          var marker = new mxn.Marker(new mxn.LatLonPoint(data.lat, data.lng));
          if (opts.markerIcon) {
            marker.setIcon(opts.markerIcon);
            marker.setIconSize(opts.markerIconSize);
            marker.setIconAnchor(opts.markerIconAnchor);
          }
          var popupText = $(opts.popupSelector, $this).hide().html();
          marker.setInfoBubble(popupText);
          mapstraction.addMarker(marker);
          
          $('a',$this).click(function() {
            marker.openBubble();
            return false;
          });
        }
      });
      
      mapstraction.autoCenterAndZoom();
    });
  };
  
  // Default options
  $.fn.vzMapper.defaults = {
    provider : 'yahoo',
    mapType : mxn.Mapstraction.HYBRID,
    controls : {
        pan: true,
        zoom: 'small',
        map_type: false
      },
    markerIcon : false,
    sourceItemSelector : 'li',
    popupSelector : '.info-box'
  };
})(jQuery);
