﻿// call load function on page load
$(document).ready(function () { Load(); });

// set onresize function
if (typeof window.onresize != 'function')
    window.onresize = ResizeMap;
else
    window.onresize = function () { ResizeMap(); };

function Load() {
    LoadMap();
    ResizeMap();
    LoadClients();
}

// set up map
function LoadMap() {
    var mapOptions = {
        credentials: "AnAV_DdLV3J0jGe9ivcR6q1GKRR-BCIxlqQWhTDl4JaLR-NTX1MfMQprJgv1MUZc",
        zoom: 9,
        center: new Microsoft.Maps.Location(43.05584433592095, -88.08013916015625),
        mapTypeId: Microsoft.Maps.MapTypeId.road,
        enableClickableLogo: false,
        enableSearchLogo: false,
        showCopyright: false,
        showLogo: false
    }

    map = new Microsoft.Maps.Map(document.getElementById("map"), mapOptions);
}

function LoadClients() {
    ShowLoadingMessage("Loading...");
    EliteProp.MapService.InvestmentClientSearch(ApplyBounds, onFailure);
}

// display loading message
function ShowLoadingMessage(message) {
    $("#loading").css("display", "block");
    $("#loadingMessage").html(message);
}

function ApplyBounds(clients) {
    var locs = new Array;
    $(clients).each(function (i, v) {
        locs.push(new Microsoft.Maps.Location(v.Latitude, v.Longitude));
    });

    map.setView({ bounds: Microsoft.Maps.LocationRect.fromLocations(locs), padding: 50 });
    UpdateMap(clients);
}

// process results
function UpdateMap(results) {
    map.entities.clear();
    PlotPoints(results);
    $("#loading").css("display", "none");
}

// plot search results on map
function PlotPoints(results) {
    $(results).each(function (i, v) {
        var pin = new Microsoft.Maps.Pushpin(
            new Microsoft.Maps.Location(v.Latitude, v.Longitude),
            { icon: 'Images/house.gif', typeName: "pp" + v.InvestmentClientID, anchor: (0, 0), height: ppHeight, width: ppWidth });
        map.entities.push(pin);
        $(".pp" + v.InvestmentClientID + " img").css("cursor", "pointer");
        $(".pp" + v.InvestmentClientID + " img").hover(
            function () { ShowPopup(v.InvestmentClientID, v.Latitude, v.Longitude, v.Style); },
            function () { HidePopup(); }
        );
    });
}

// set up for loading message
function SetupLoadingMessage() {
    var leftOffset = 79 + ((PageWidth - 179) / 2);
    $("#loading").css({ top: 409, left: leftOffset });
}

// show pushpin popup
function ShowPopup(id, lat, lng, html) {
    $("#ClientPopup").html(html);
    $("#PopupHolder").css("display", "block");
    PositionPopup(id, lat, lng);
}
