﻿/// <reference path="~/js/jQuery/jquery-1.3.2.js"/>
/// <reference path="~/js/jQuery/jquery-1.3.2-vsdoc2.js"/>
/// <reference name="MicrosoftAjax.js"/>

var map = null;
var points = [];
var shapes = [];
var center = null;
function LoadMap(mapDivId, latitude, longitude, onMapLoaded) {


    map = new VEMap(mapDivId);

    options = new VEMapOptions();
    options.EnableBirdseye = false;
    // Makes the control bar less obtrusize.
    map.SetDashboardSize(VEDashboardSize.Small);
    if (onMapLoaded != null)
        map.onLoadMap = onMapLoaded;
    if (latitude != null && longitude != null) {
        center = new VELatLong(latitude, longitude);
    }
    map.LoadMap(center, null, null, null, null, null, null, options);

}
function LoadPin(LL, name, description) {
    if(map!=null)
        clearMap();
  
    var shape = new VEShape(VEShapeType.Pushpin, LL);
    //Make a nice Pushpin shape with a title and description
    shape.SetTitle("<span class=\"pinTitle\"> " + escape(name) + "</span>");
    if (description !== undefined) {
        shape.SetDescription("<p class=\"pinDetails\">" +
        escape(description) + "</p>");
    }
    if (map == null) {
        map = new VEMap(mapdiv);
        options = new VEMapOptions();
        options.EnableBirdseye = false;
        // Makes the control bar less obtrusize.
        map.SetDashboardSize(VEDashboardSize.Small);
        
        map.LoadMap(center, null, null, null, null, null, null, options);
    }
    //map.LoadMap(LL);
    map.AddShape(shape);
    points.push(LL);
    shapes.push(shape);
    map.SetMapView(points);
    map.SetZoomLevel(15);
}
function FindAddressOnMap(where) {
    var numberOfResults = 1;
    var setBestMapView = true;
    var showResults = true;
//    if (map == null)
//        map = new VEMap(mapdiv);
//    if (map != null)
//        clearMap();
    map.Find("", where, null, null, null,
    numberOfResults, showResults, true, true,
    setBestMapView, callbackForLocation);
}
function callbackForLocation(layer, resultsArray, places, hasMore, VEErrorMessage) {

    //clearMap();
    if (places == null)
        return;

    //Make a pushpin for each place we find
    $.each(places, function(i, item) {
        var description = "";
        if (item.Description !== undefined) {
            description = item.Description;
        }
        var LL = new VELatLong(item.LatLong.Latitude,
        item.LatLong.Longitude);

//        var shape = new VEShape(VEShapeType.Pushpin, LL);
//        //Make a nice Pushpin shape with a title and description
//        shape.SetTitle("<span class=\"pinTitle\"> " + escape(item.name) + "</span>");
//        if (description !== undefined) {
//            shape.SetDescription("<p class=\"pinDetails\">" +
//        escape(description) + "</p>");
//        }
//        map.LoadMap(LL);
//        map.AddShape(shape);
//        points.push(LL);
//        shapes.push(shape);
//        map.SetMapView(points);
        LoadPin(LL, item.Name, description);
    });

    //Make sure all pushpins are visible
    if (points.length > 1) {
        map.SetMapView(points);
    }
    //If we've found exactly one place, that's our address.
    if (points.length === 1) {
        $("#Latitude").val(points[0].Latitude);
        $("#Longitude").val(points[0].Longitude);
    }
}
function clearMap() {
   map.Clear();
    points = [];
    shapes = [];
}
