var map;
var gdir;
var geocoder = null;
var addressMarker;

function initialize() {
	if (GBrowserIsCompatible()) {      
	  map = new GMap2(document.getElementById("map_canvas"));
	  map.addControl(new GSmallMapControl());
	  gdir = new GDirections(map, document.getElementById("directions"));
	  GEvent.addListener(gdir, "load", onGDirectionsLoad);
	  GEvent.addListener(gdir, "error", handleErrors);
	  // Added by Matt Petri at Fzzz! Media
	  geocoder = new GClientGeocoder(); 
	  showAddress($( 'resort_longitude' ).value, $( 'resort_latitude' ).value, $( 'resort_address' ).value) // Pull Resort Address and initiate map at location
	  map.checkResize() // fix half grey on load problem
	  
	}
}

// geocode address
function showAddress(longitude,latitude,address) {
	point = new GLatLng(latitude,longitude);
	
	if( !point )
	{
		alert( "Location not found!" );
	}
	else
	{
        map.setCenter(point, 7);
        var marker = new GMarker(point);
        map.addOverlay(marker);
        marker.openInfoWindowHtml(address);
   }
}


function setDirections(fromAddress, toAddress) 
{

	if( !fromAddress )
	{
		setError( "No address specified" );
		return;
	}
	
	document.getElementById( 'directions_container' ).style.display = 'inline';
	
	from = new GLatLng( $( 'resort_latitude' ).value, $( 'resort_longitude' ).value );

  if (geocoder) {
     geocoder.getLatLng(
       fromAddress,
       function(point) {
         if (!point) {
           setError(toAddress + " not found");
         } else {
				gdir.loadFromWaypoints( [ point, from ], { "locale": "en_US" } );
				map.checkResize()
         }
       }
     );
   }

	
	//gdir.loadFromWaypounts( [ from, to ], { "locale": "en_US" } );
	//map.checkResize()
}

function handleErrors()
{	
	document.getElementById( 'directions_container' ).style.display = 'none';
	
	if( gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS )
		setError( "No corresponding geographic location could be " +  
			"found for one of the specified addresses. This may be due to the fact that the address is " + 
			"relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code );
			
	else if( gdir.getStatus().code == G_GEO_SERVER_ERROR )
		setError( "A geocoding or directions request could not be " + 
			"successfully processed, yet the exact reason for the failure is not known.\n Error code: " + 
			gdir.getStatus().code );

	else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
		setError( "The HTTP q parameter was either missing or had no " + 
			"value. For geocoder requests, this means that an empty address was specified as input. For " + 
			"directions requests, this means that no query was specified in the input.\n Error code: " + 
			gdir.getStatus().code );

	else if (gdir.getStatus().code == G_GEO_BAD_KEY)
  		setError( "The given key is either invalid or does not match " + 
  			"the domain for which it was given. \n Error code: " + gdir.getStatus().code );

	else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
  		setError( "A directions request could not be successfully " + 
  		"parsed.\n Error code: " + gdir.getStatus().code );
 
	else 
		setError( "An unknown error occurred." );

}

function setError( error )
{
	document.getElementById( 'map_error' ).innerHTML = error;
}

function onGDirectionsLoad()
{ 
	// Use this function to access information about the latest load()
	// results.

	// e.g.
	// document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
	// and yada yada yada...
}
