﻿function showAjaxMap(container, key, latitude, longitude, zoom, mapParams) {
    var map = new VEMap(container.id);
    map.SetCredentials(key);
    map.LoadMap(new VELatLong(latitude, longitude), Math.round(zoom), VEMapStyle.Aerial);

    var proxy = new P1Services.IMapService();
    proxy.GetMapData(mapParams, function(results) {
        var points = $j.map(results, function(i) { return { "Latitude": i.Latitude, "Longitude": i.Longitude }; });

        var mapLocations = $j.map($j.unique(points), function(i) {
            var itemsAtLocation = $j.grep(results, function(r) { return r.Latitude == i.Latitude && r.Longitude == i.Longitude; });
            var tooltips = $j.map(itemsAtLocation, function(ial) { return String.format("<a href='{0}'>{1}</a>", ial.DocumentUrl, ial.Name); });
            return { "Latitude": i.Latitude, "Longitude": i.Longitude, "Tooltip": tooltips.join("<br />") };
        });

        $j.each(mapLocations, function() {
            var pin = new VEShape(VEShapeType.Pushpin, new VELatLong(this.Latitude, this.Longitude));
            pin.SetTitle(this.Tooltip);
            map.AddShape(pin);
        });
    });
}

function onSilverlightError(sender, args) {
    var appSource = "";
    if (sender != null && sender != 0) {
        appSource = sender.getHost().Source;
    }

    var errorType = args.ErrorType;
    var iErrorCode = args.ErrorCode;

    if (errorType == "ImageError" || errorType == "MediaError") {
        return;
    }

    var errMsg = "Unhandled Error in Silverlight Application " + appSource + "\n";

    errMsg += "Code: " + iErrorCode + "    \n";
    errMsg += "Category: " + errorType + "       \n";
    errMsg += "Message: " + args.ErrorMessage + "     \n";

    if (errorType == "ParserError") {
        errMsg += "File: " + args.xamlFile + "     \n";
        errMsg += "Line: " + args.lineNumber + "     \n";
        errMsg += "Position: " + args.charPosition + "     \n";
    }
    else if (errorType == "RuntimeError") {
        if (args.lineNumber != 0) {
            errMsg += "Line: " + args.lineNumber + "     \n";
            errMsg += "Position: " + args.charPosition + "     \n";
        }
        errMsg += "MethodName: " + args.methodName + "     \n";
    }

    throw new Error(errMsg);
}

