﻿
var map = null;
var points = [];
var shapes = [];
var center = null;

function LoadMap(containerId, latitude, longitude, onMapLoaded, gimp) {
    map = new VEMap(containerId);
    options = new VEMapOptions();
    options.EnableBirdseye = false;

    // set app key
    map.SetCredentials("AjFSlqHTWgROrFLPBF4mk5bhWGW72densWbwMMtyzAWy99BayIJoms_6nRze3TxM");

    // Makes the control bar less obtrusize.
    map.SetDashboardSize(VEDashboardSize.Tiny);

    if (onMapLoaded != null)
        map.onLoadMap = onMapLoaded;

    if (latitude != null && longitude != null) {
        center = new VELatLong(latitude, longitude);
    }

    if (typeof gimp != "undefined" && gimp) {
        // Prevent zooming and panning
        map.AttachEvent("onmousedown", StopZoomAndPan);
        map.AttachEvent("onmousewheel", StopZoomAndPan);

        // Don't Show Dashboard
        map.HideDashboard();
    }

    map.LoadMap(center, null, null, null, null, null, null, options);
}

function StopZoomAndPan() {
    return true;
}

function LoadPin(LL, name, description, iconType, layer) {
    var shape = new VEShape(VEShapeType.Pushpin, LL);

    if (typeof layer == "undefined") {
        layer = map;
    }

    if (typeof iconType != "undefined") {
        var icon = new VECustomIconSpecification();
        icon.Image = "/images/maps/" + iconType + ".png";
        //icon.ImageOffset = new VEPixel(16, 37);
        icon.TextContent = " ";

        shape.SetCustomIcon(icon);
    }

    //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>");
    }
    layer.AddShape(shape);
    points.push(LL);
    shapes.push(shape);

    return shape;
}

function FindLayer(title)
{
    var layer = null;
    for (var i = 0; i < map.GetShapeLayerCount(); i++)
    {
        layer = map.GetShapeLayerByIndex(i);
        if (layer.GetTitle() == title) break;
        layer = null;
    }
    return layer;
}

function FindAddressOnMap(where, cb) {
    var numberOfResults = 20;
    var setBestMapView = true;
    var showResults = false;

    if (map) {
        if (typeof cb == "undefined") {
            map.Find("", where, null, null, null,
           numberOfResults, showResults, true, true,
           setBestMapView, callbackForLocation);
        }
        else {
            map.Find("", where, null, null, null,
           numberOfResults, showResults, true, true,
           setBestMapView, cb);
        }
    }
}

function callbackForLocation(layer, resultsArray, places,
            hasMore, VEErrorMessage) {

    clearMap();

    if (places == null)
        return;

    //Make a pushpin for each place we find
    jQuery.each(places, function (i, item) {
        description = "";
        if (item.Description !== undefined) {
            description = item.Description;
        }
        var LL = new VELatLong(item.LatLong.Latitude,
                        item.LatLong.Longitude);

        LoadPin(LL, item.Name, description, "home");
    });

    //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) {
        jQuery("#Latitude").val(points[0].Latitude);
        jQuery("#Longitude").val(points[0].Longitude);
    }
}

function clearMap() {
    map.Clear();
    points = [];
    shapes = [];
}
