var map = null;
var gotDirections = false;
         

      
function checkDirections() {
	

	
try
{
	if (!$('#address').val()) {
		
		alert('You must enter either an address or an airport code!');

	} else {
    	
       	$start = $('#address').val();
       
        $end = '1000 East Atlantic Avenue, Delray Beach, Florida 33483'; 
		
        map.GetRoute($start, $end); 
        var options = new VERouteOptions();
	    options.RouteCallback = onGotRoute;
		map.GetDirections([$start, $end], options);
        
        gotDirections = true
        toggleMap(true);
      	map.LoadMap();
      	
      	$('#from_location').val($start);
      	addPushPins();
	}
	

	
}catch(err){
	alert('error: ' + err.message);  
}

	
}

function getDirectionsFromAirport(){
	
	
	switch($('#airport_code').val()){
		case 'FLL':
			$start = 'FORT LAUDERDALE AIRPORT';
			break;
		case 'PBI':
			$start = 'PALM BEACH INTERNATIONAL AIRPORT';
			break;
		case 'MIA':
			$start = 'MIAMI INTERNATIONAL AIRPORT';
			break;
		default:
			break;
		
	
	}
		
	$end = '1000 East Atlantic Avenue, Delray Beach, Florida 33483'; 
	
	
    
    
	map.GetRoute($start, $end); 
	var options = new VERouteOptions();
    options.RouteCallback = onGotRoute;
	map.GetDirections([$start, $end], options);
    gotDirections = true;
    toggleMap(true);
	map.LoadMap();
	$('#from_location').val($start);
	addPushPins();
	
}

function toggleViewMapLink($forceMapLoad){
	
	if($forceMapLoad || ($('#view_map_link').html() == 'View Map')){
    	$('#view_map_link').html('Hide Map');
    }else{
    	$('#view_map_link').html('View Map');
    }
}

function toggleMap($forceMapLoad){
	
	
	if($forceMapLoad || ($('#DefaultText').css('display') != 'none')){
		
		$('#DefaultText').hide();
		$('#DirMapContainer').show();
		toggleViewMapLink(true);
		addPushPins();
		
    }else{
		
		$('#DefaultText').show();
		$('#DirMapContainer').hide();
		toggleViewMapLink();
			
    }
    
    
    
    if(!gotDirections){
    	
    	map.Find(null, '1000 East Atlantic Avenue, Delray Beach, Florida 33483');
    	
    	map.LoadMap();
    	addPushPins();
    	
    }
    
    
    
}

function onGotRoute(route){
   
   // Unroll route
   var legs     = route.RouteLegs;
   var turns    = "<br /><h3>Total distance: " + route.Distance.toFixed(1) + " miles</h3>";
   var numTurns = 0;
   var leg      = null;

   // Get intermediate legs
    for(var i = 0; i < legs.length; i++)
    {
       // Get this leg so we don't have to derefernce multiple times
       leg = legs[i];  // Leg is a VERouteLeg object
          
       // Unroll each intermediate leg
       var turn = null;  // The itinerary leg
          
       for(var j = 0; j < leg.Itinerary.Items.length; j ++)
       {
          turn = leg.Itinerary.Items[j];  // turn is a VERouteItineraryItem object
          numTurns++;
          turns += "<p><span class='turn_num'>" + numTurns + ")</span> " + turn.Text + " <span class='turn_miles'>(" + turn.Distance.toFixed(1) + " miles)</span></p>";
       }
    }
	
    $('#DirText').html(turns);
 }




function addPushPins(){

	map.ClearInfoBoxStyles();
	var hotelLatLong = new VELatLong(26.461675,-80.062006);
	var hotelPin = new VEShape(VEShapeType.Pushpin, hotelLatLong);
	
	
	hotelPin.SetTitle("<h3>The Seagate Hotel &amp; Spa</h3>");
	hotelPin.SetDescription('1000 East Atlantic Avenue, Delray Beach, Florida 33483');
	hotelPin.SetCustomIcon('<img src="images/pushpinicon.gif" alt="" />');
	
	map.AddShape(hotelPin);
	
	var clubLatLong = new VELatLong(26.453997,-80.059211);
	
	var clubPin = new VEShape(VEShapeType.Pushpin, clubLatLong);
	clubPin.SetTitle("<h3>The Seagate Beach Club</h3>");
	clubPin.SetDescription('401 South Ocean Blvd., Delray Beach, FL 33483');
	clubPin.SetCustomIcon('<img src="images/pushpinicon.gif" alt="" />');
	map.AddShape(clubPin);
		
}

 



