var veMap = null;

//------------------------------------------------------------------------------
// Gets a map based on the provided points
//------------------------------------------------------------------------------
function GetMap(points) {

    initializeTheMap(points);
    var topLeft     = new VELatLong(bigLat, smallLon);
    var bottomRight = new VELatLong(smallLat, bigLon);
    var startPoint  = new VELatLong(avgLat, avgLon);
    veMap = GetVirtualEarthMap();
    var zoomlevel = AdjustZoomLevel(DistanceOnMap(topLeft.Latitude, topLeft.Longitude, bottomRight.Latitude, bottomRight.Longitude));
    veMap.LoadMap(startPoint, zoomlevel, VEMapStyle.Road, false, VEMapMode.Mode2D, true, 1);

    for(i = 0; i < points.length; i++) {
        AddPin(points[i]);
    }
}

//------------------------------------------------------------------------------
// Adds a push pin on the map
//------------------------------------------------------------------------------
function AddPin(point) {
    var coord = new VELatLong(point.lat, point.lon);
    var shape = new VEShape(VEShapeType.Pushpin, coord);
    if (point.type == 'H') {
        var pushpinStr = propertyMap ? '' : '<div class="text">' + point.seqNum + '</div>'; 
        shape.SetCustomIcon('<div class="pinStyle">' + pushpinStr + '</div>');
    } else {
        shape.SetCustomIcon('<div class="landmarkPinStyle"></div>');
    }
    shape.SetDescription(getMapDescription(point));
    
    veMap.AddShape(shape);
}

//------------------------------------------------------------------------------
// Get a map of the origin (used for Road Trips - usually a zip code, but
// this function can be used for address search too)
//------------------------------------------------------------------------------
function GetDirectionsMap(origin) {
    veMap = GetVirtualEarthMap();
    veMap.LoadMap();            

    try {
        veMap.Find(null, origin);
    } catch(e) {
        alert(e.message);
    }
 }  

 
//------------------------------------------------------------------------------
// Get directions from an origin to a destination
//------------------------------------------------------------------------------
function GetBiDirections(origin, dest) {
    veMap = GetVirtualEarthMap();
    veMap.LoadMap();
    veMap.GetDirections([origin, dest]);
 }  


//------------------------------------------------------------------------------
// Initializes Virtual Earth Map with the token if it exists.
// If there is an error on the server side when retrieving the token, the error
// is logged. But Virtual Earth will use the dev link instead. So the site will
// be temporarily operational.
//------------------------------------------------------------------------------
function GetVirtualEarthMap() {
    var virtualearthmap = new VEMap('veMapId');
    if (veToken != null && veToken != "") {
        virtualearthmap.SetClientToken(veToken);
    }
    
    return virtualearthmap;
}

//------------------------------------------------------------------------------
// Dispose the map if it is not null in case of a memory leak.
//------------------------------------------------------------------------------
function DisposeMap() {
   if(veMap != null) {
     veMap.Dispose();
   }
}

