var map = null;
var startGeocoded = null;
var destinationGeocoded = null;

function goMap24(){
alert(document.getElementById("maparea2"));
map = Map24.Webservices.getMap24Application({
    AppKey: "FJX22315e7734826c4169791f751416eX14",
    MapArea: document.getElementById("maparea2"),
    MapWidth: 480,
    MapHeight: 480
});
alert(map);
}

function calculateRouteAddr( start, destination )
{
alert(start);
alert(destination);
//if( isNull( start ) ) return;
//if( isNull( destination ) ) return;
//alert('1');
geocodeStartAndDestination( start, destination );
return;
}

function geocodeStartAndDestination( start, destination ){
//1. Geocode the start address
geocode( start, onGeocodeStart )

//2. When start address has been geocoded, then geocode destination address
function onGeocodeStart( geoRes ){
    startGeocoded = geoRes.firstResult;
    geocode( destination, onGeocodeDest )
}
//3. When both start and dest address have been geocoded, then start route calculation
function onGeocodeDest( geoRes ){
    destinationGeocoded = geoRes.firstResult;
    calculateRouteCoord( startGeocoded, destinationGeocoded );
}
}

//Calculate a route between two addresses
function calculateRouteCoord( startGeocoded, destinationGeocoded ){
if( Map24.isNull( startGeocoded ) ) return;
if( Map24.isNull( destinationGeocoded ) ) return;

var routeRequest = new Map24.Webservices.Request.CalculateRoute( map );
routeRequest.Start = new Map24.Webservices.Request.CalculateRoute.CoordinateAndAddress();
routeRequest.Start.Coordinate = new Map24.Coordinate( startGeocoded.Coordinate.Longitude,
                                                        startGeocoded.Coordinate.Latitude );
routeRequest.Destination = new Map24.Webservices.Request.CalculateRoute.CoordinateAndAddress();
routeRequest.Destination.Coordinate = new Map24.Coordinate( destinationGeocoded.Coordinate.Longitude,
                                                            destinationGeocoded.Coordinate.Latitude );
map.Webservices.sendRequest( routeRequest );

//This listener is called when the route calculation has finished
map.onCalculateRoute = function( event ){
    var mrcContainer = new Map24.Webservices.Request.MapletRemoteControl( map );
    mrcContainer.push( 
    new Map24.Webservices.MRC.DeclareMap24RouteObject({
        MapObjectID: "actualRoute",
        Map24RouteID: event.Info.RouteID,
        Color: new Map24.Color( 255, 0, 0, 180 )
    }) 
    );
    //Center the map view on the route visualization
    mrcContainer.push( genSetMapView( 0, 70, null, null, "actualRoute" ) );
    
    //Enable the visibility of the route 
    mrcContainer.push( genControlMapObject( "ENABLE", "actualRoute" ) );
    
    map.Webservices.sendRequest( mrcContainer );
}
}

//Geocoding
function geocode( address, onGeoFunc ){
alert('geocode start');
map.Webservices.sendRequest(
    new Map24.Webservices.Request.MapSearchFree(map, {
    SearchText: address,
    MaxNoOfAlternatives: 50
    })
);
alert('geocode end');    

map.onMapSearchFree = function( event ){
    var geoRes = new Object();
    geoRes.Alternatives = event.Alternatives;
    geoRes.firstResult = geoRes.Alternatives[0];

    onGeoFunc( geoRes );
}
}    

//Factory functions
function genSetMapView( ClippingWidth, ClippingPercent, lon, lat, objID ){
//Hashtable Clip { MinimumWidth: ClippingWidth, ViewPercentage: ClippingPercent } is created
var Clip = new Array();
if( !Map24.isNull( ClippingWidth ) ) 
    Clip['MinimumWidth'] = ClippingWidth;
if( !Map24.isNull( ClippingPercent ) )
    Clip['ViewPercentage'] = ClippingPercent;

if( !Map24.isNull( lon ) && !Map24.isNull( lat ) )
{
    var SetMapView = new Map24.Webservices.MRC.SetMapView({
    ClippingWidth: new Map24.Webservices.ClippingWidth( Clip ),
    Coordinates: new Map24.Coordinate( lon, lat )
    });
} 
else if( !Map24.isNull( objID ) )
{
    var SetMapView = new Map24.Webservices.MRC.SetMapView({
    ClippingWidth: new Map24.Webservices.ClippingWidth( Clip ),
    MapObjectIDs: objID 
    });
}	
return SetMapView;	
}		

function genControlMapObject( Control, objID ){
var ControlObject = new Map24.Webservices.MRC.ControlMapObject({
    Control: Control,
    MapObjectIDs: objID
});
return ControlObject;
}      

