var view = "map";

// 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();
    LoadRentals();
}

// 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);
}

// change to listview
function ShowList() {
    if (view != "list")
        view = "list";
    $("#map").css("display", "none");
    $("#ctl00_cphMain_hypHelp").css("display", "none");
    if ($("#ctl00_cphMain_litAddNew").length)
        $("#ctl00_cphMain_litAddNew").css("display", "none");
    $("#MapViewTab").removeClass("selected");
    $("#ListViewTab").addClass("selected");
    // add border to tab element for list view
    $("#tabs").css("borderBottom", "solid 1px #000000");
}

// change to mapview
function ShowMap() {
    if (view != "map")
        view = "map";
    $("#map").css("display", "block");
    $("#ctl00_cphMain_hypHelp").css("display", "inline");
    if ($("#ctl00_cphMain_litAddNew").length)
        $("#ctl00_cphMain_litAddNew").css("display", "inline");
    $("#MapViewTab").addClass("selected");
    $("#ListViewTab").removeClass("selected");
    // remove border on tab element
    $("#tabs").css("borderBottom", "none");
}

// set up for loading message
function SetupLoadingMessage() {
    var leftOffset = 79 + ((PageWidth - 179) / 2);
    $("#loading").css({ top: 359, left: leftOffset });
    $("#error").css({ top: 359, left: leftOffset - 25 });
}

function UpdateMapBounds() {
    nwLat = map.getBounds().getNorth();
    nwLng = map.getBounds().getWest();
    seLat = map.getBounds().getSouth();
    seLng = map.getBounds().getEast();
    $("#ctl00_cphMain_hidNWLat").val(nwLat);
    $("#ctl00_cphMain_hidNWLng").val(nwLng);
    $("#ctl00_cphMain_hidSELat").val(seLat);
    $("#ctl00_cphMain_hidSELng").val(seLng);
    $("#ctl00_cphMain_btnHidSearch").click();
}

function LoadRentals() {
    ShowLoadingMessage("Loading...");
    if ($("#ctl00_cphMain_chkDelisted").length)
        EliteProp.MapService.RentalSearch(0, 0, 0, 0, "list", $("#ctl00_cphMain_chkDelisted").attr("checked"), "Rent", ApplyBounds, onFailure);
    else
        EliteProp.MapService.RentalSearch(0, 0, 0, 0, "list", false, "Rent", ApplyBounds, onFailure);
}

// display loading message
function ShowLoadingMessage(message) {
    $("#Message").css("display", "none");
    $("#loading").css("display", "block");
    $("#loadingMessage").html(message);
}

// display results message
function ShowResultsMessage(count) {
    if (count == 0)
        $("#Message").html("No Properties Found");
    else {
        if (count == 1)
            $("#Message").html(count + " Property Shown");
        else
            $("#Message").html(count + " Properties Shown");
    }
    $("#loading").css("display", "none");
    $("#Message").css("display", "block");
}

// process results
function UpdateMap(results) {
    map.entities.clear();
    PlotPoints(results);
    ShowResultsMessage(results.length);
}

function ApplyBounds(rentals) {
    var locs = new Array;
    $(rentals).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(rentals);
}

// plot search results on map
function PlotPoints(results) {
    $("#error").css("display", "none");
    $(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.ListingID, anchor: (0, 0), height: ppHeight, width: ppWidth });
        map.entities.push(pin);
        $(".pp" + v.ListingID + " img").click(function () { RedirectToDetails(v.ListingID); });
        $(".pp" + v.ListingID + " img").css("cursor", "pointer");
        $(".pp" + v.ListingID + " img").hover(
            function () { ShowPopup(v.ListingID, v.Latitude, v.Longitude, v.PopupHtml); },
            function () { HidePopup(); }
        );
    });
}

function RedirectToDetails(rentalID) {
    window.location = "RentalDetails.aspx?id=" + rentalID;
}

// show pushpin popup
function ShowPopup(id, lat, lng, html) {
    if (view == "map") {
        $("#RentalPopup").html(html);
        $("#PopupHolder").css("display", "block");
        PositionPopup(id, lat, lng);
    }
}
