/*------------------------------------------------------------------------------
Function:       EasyGoogleMap()
Author:         Aaron Gustafson (aaron at easy-designs dot net)
Creation Date:  2009-06-23
Version:        0.1
Homepage:       http://github.com/easy-designs/EasyGoogleMap.js
License:        MIT License (see homepage)
Note:           If you change or improve on this script, please let us know by
                emailing the author (above) with a link to your demo page.
------------------------------------------------------------------------------*/
(function(){
  
  var EasyGoogleMap = function( $el, options )
  {
    this.version = '0.1';
    if ( GBrowserIsCompatible() )
    {
      initialize( $el, options );
    }
  },
  settings = {
    'center':   [0,0],
    'controls': 'small-zoom',
    'drag':     true,
    'zoom':     8,
    'icon':     false
  },
  map    = false,
  bounds = false,
  icon   = false;
  
  function initialize( $el, options )
  {
    
    // bring in any custom settings
    if ( options instanceof Object )
    {
      for ( var option in options )
      {
        if ( typeof( settings[option] ) != 'undefined' )
        {
          settings[option] = options[option];
        }
      }
    }
    
    settings['width'] = $el.width();
    settings['height'] = $el.height();
    
    // capture any geo data
    var position = null;
    if ( $el.find('.latitude').length > 0 )
    {
      position = getLatLon( $el );
      settings['center'] = position;
    }
    
    // create the map
    initializeMap( $el[0] );
    
    // create the icon
    createIcon();
    
    if ( position !== null )
    {
      addPoint( position );
    }
  }
  
  function initializeMap( el )
  {
    var lat = settings['center'][0],
        lon = settings['center'][1];
    
    map = new GMap2( el, {
      size: new GSize( settings['width'], settings['height'] )
    });
    
    if ( settings['controls'] !== false )
    {
      switch( settings['controls'] )
      {
        case 'large-3D':
          map.addControl( new GLargeMapControl3D() );
          break;
        case 'large':
          map.addControl( new GLargeMapControl() );
          break;
        case 'small':
          map.addControl( new GSmallMapControl() );
          break;
        case 'small-zoom-3D':
          map.addControl( new GSmallZoomControl3D() );
          break;
        case 'scale':
          map.addControl( new GScaleControl() );
          break;
        case 'type':
          map.addControl( new GMapTypeControl() );
          break;
        case 'hierarchical':
          map.addControl( new GHierarchicalMapTypeControl() );
          break;
        case 'overview':
          map.addControl( new GOverviewMapControl() );
          break;
        case 'label':
          map.addControl( new GNavLabelControl() );
          break;
        default:
          map.addControl( new GSmallZoomControl() );
          break;
      }
    }
    if ( settings['drag'] === false )
    {
      map.disableDragging();
    }
    map.setCenter( new GLatLng( lat, lon ), settings['zoom'] );
    
    // handle the boundaries
    bounds = new GLatLngBounds( new GLatLng( lat-.001, lon-.001 ),
                                new GLatLng( lat+.001, lon+.001 ) );
  }


  function createIcon()
  {
    icon = new GIcon();
    if ( settings['icon'] !== false )
    {
      if ( typeof( settings['icon']['marker'] ) != 'undefined' )
      {
        icon.image = settings['icon']['marker']['image'];
        icon.iconSize = new GSize( settings['icon']['marker']['size'][0], settings['icon']['marker']['size'][1] );
        if ( typeof( settings['icon']['anchor'] ) != 'undefined' )
        {
          icon.iconAnchor = new GPoint( settings['icon']['anchor'][0], settings['icon']['anchor'][1] );
          icon.infoWindowAnchor = new GPoint( settings['icon']['anchor'][0], settings['icon']['anchor'][1] );
        }
        else
        {
          icon.iconAnchor = new GPoint( 0, settings['icon']['marker']['size'][1] );
          icon.infoWindowAnchor = new GPoint( 0, settings['icon']['marker']['size'][1] );
        }
      }
      if ( typeof( settings['icon']['shadow'] ) != 'undefined' )
      {
        icon.shadow = settings['icon']['shadow']['image'];
        icon.shadowSize = new GSize( settings['icon']['shadow']['size'][0], settings['icon']['shadow']['size'][1] );
      }
    }
  }
  
  function addPoint( position )
  {
    position = new GLatLng( position[0], position[1] );
    map.addOverlay( new GMarker( position ) );
    bounds.extend( position );
    map.panTo( bounds.getCenter() );
    map.setZoom( map.getBoundsZoomLevel( bounds ) );
  }
  EasyGoogleMap.addPoint = addPoint;
  
  function getLatLon( $el )
  {
    return [ parseFloat( $el.find('.latitude').text() ), parseFloat( $el.find('.longitude').text() ) ];
  }
  
  // set the global object
  window.EasyGoogleMap = EasyGoogleMap;
})();
