
var proximitySearch = Class.create( {

	geocoder: null, 
	map: null, 
	
	error_element: null, 
	list_element: null,
	
	initialize: function( map_canvas, list_element, error_element ) 
	{
		this.error_element = $( error_element );
		this.list_element = $( list_element );
		
		if( GBrowserIsCompatible() ) 
		{
			this.map = new GMap2( $( map_canvas ) );
			this.map.addControl(new GSmallMapControl());
			this.geocoder = new GClientGeocoder();
		}
	}, 

	search: function( postal_code, proximity )
	{	
		proximitySearch = this;
		
		if( this.geocoder ) 
		{
			this.geocoder.getLatLng(
				postal_code,
	
				function( point ) 
				{
			   	if( !point )
			   	{
			   		proximitySearch.logError( "Postal Code " + postal_code + " not found" );
			   	} 
			   	else
			   	{
			   		proximitySearch.map.setCenter( point, 6 );
			   		proximitySearch.map.clearOverlays();
			   		
			   		proximitySearch.list_element.innerHTML = "";
			   		
			   		results = new Array();
			   		
			     		resorts.each( function( hash_pair )
			     		{
			     			resort = hash_pair.value;
			     		
			     			if( resort.longitude && resort.latitude )
			     			{
			     				resortLocation = new GLatLng( resort.latitude, resort.longitude );
			     				
			     				distance = resortLocation.distanceFrom( point );

			     				var distanceInMiles = distance * 0.000621371192;

			     				if( distanceInMiles <= proximity )
			     				{
			     					resort.distanceInMiles = distanceInMiles;
			     					resort.location = resortLocation;
			     					
			     					results[ results.length ] = resort;
			     				}
			     			}
			     			
			     		} );
			     		
			     		results = results.sortBy( function( resort )
			     		{			     			
			     			return( resort.distanceInMiles );
			     			
			     		} );
			     		
			     		results.each( function( resort )
			     		{
			     			
	     					marker = new GMarker( resort.location, { clickable: true } );
	     				
	     					GEvent.addListener( marker, "click", function() {
	     						staticLocation = new GLatLng( resort.latitude, resort.longitude );
	     						proximitySearch.map.setCenter( staticLocation );
	     						proximitySearch.map.openInfoWindowHtml( staticLocation, 
	     							"<b>" + resort.name + "</b> <br />" + 
									"Distinace: " + resort.distanceInMiles.toFixed( 2 ) + " mi<br /><br />" + 
									resort.address + "<br />" + 
									resort.city + ", " + resort.state + " " + resort.postal_code + "<br /><br />" + 
									"<a href=\"/resorts/" + resort.url_alias + "\">View Resort</a>" );
	     					} );
	     				
	     					proximitySearch.map.addOverlay( marker );
	     					
	     					li = document.createElement( "li" );
	     					li.innerHTML = "<a href=\"/resorts/" + resort.url_alias + "\">" + 
							  	"<b>" + resort.name + "</b></a> (" + resort.distanceInMiles.toFixed( 2 ) + " mi)";
						   
						   proximitySearch.list_element.appendChild( li );
								   
			     		} );
			   	}
			 	}
			 	
			);
		}
		else
		{
			this.logError( "Failed to instantiate geocoder" );	
		}
	},
	
	logError: function( message )
	{
		this.error_element.innerHTML = message;
	}
	
} );

var resortProximitySearch;
