﻿
var map = null;
var points = [];
var shapes = [];
var center = null;
var mileRadius = 5;
var saleOrLease = 0;
var mypropertyTypes = [];
var strMyPropertyTypes = '';


function LoadMap(latitude, longitude, onMapLoaded) {
    map = new VEMap('theMap');
    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) {
    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>");
    }
    map.AddShape(shape);
    points.push(LL);
    shapes.push(shape);
}

function FindAddressOnMap(where) {
    var numberOfResults = 20;
    var setBestMapView = true;
    var showResults = true;

    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);

        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 = [];
}

function FindListingsGivenLocation(where, miles, forSaleOrLease, propertyTypes) {
    //alert("here999 propTypes.length=" + propertyTypes.length);
    mileRadius = miles;
    saleOrLease = forSaleOrLease;
    mypropertyTypes = propertyTypes;

    strMyPropertyTypes = '';
    for (x in propertyTypes) {
        if (strMyPropertyTypes != '') {
            strMyPropertyTypes = strMyPropertyTypes + '|';
            }
        strMyPropertyTypes = strMyPropertyTypes + propertyTypes[x];
    }
    //alert(" my json " + strMyPropertyTypes);


    
    map.Find("", where, null, null, null, null, null, false,
       null, null, callbackUpdateMapListings);
    //alert("here4");
}

function callbackUpdateMapListings(layer, resultsArray, places, hasMore, VEErrorMessage) {
    //alert(" my json " + JSON.stringify(mypropertyTypes)); 
    $("#listingList").empty();
    clearMap();
    var center = map.GetCenter();

    var s = JSON.stringify(mypropertyTypes);

    

//    var s = { latitude: center.Latitude,
//        longitude: center.Longitude,
//        mileRadius: mileRadius,
//        saleOrLease: saleOrLease
//    }
//    alert(" my json " + JSON.stringify(s));

    //$.post("/Search/SearchByLocation", JSON.stringify(s)
     $.post("/Search/SearchByLocation", { latitude:center.Latitude,
        longitude:center.Longitude, 
        mileRadius:mileRadius,
        saleOrLease:saleOrLease,
        sPropertyTypes:strMyPropertyTypes
    }, function(listings) {
        $.each(listings, function(i, listing) {
            
            var LL = new VELatLong(listing.Latitude, listing.Longitude, 0, null);

            //alert("here6" + listing.ID);
            // Add Pin to Map
            LoadPin(LL, '<a href="/Listing/Details/' + listing.ID + '">'
                        + listing.Title + '</a>',
                        "<p>" + listing.Description + "</p> Property Type " + listing.PropertyTypeDescription + "<br> For " + listing.LeaseOrSale);

            //Add a listing to the <ul> listingList on the right
            $('#listingList').append($('<li/>')
                            .attr("class", "listingItem")
                            .append($('<a/>').attr("href",
                                      "/Listing/Details/" + listing.ID)
                            .html(listing.Title)).append(" <br>" + listing.PropertyTypeDescription + "<br>(For " + listing.LeaseOrSale + ")"));
        });

        // Adjust zoom to display all the pins we just added.
        if (points.length > 1) {
            map.SetMapView(points);
        }

        // Display the event's pin-bubble on hover.
        $(".listingItem").each(function(i, listing) {
            $(listing).hover(
                function() { map.ShowInfoBox(shapes[i]); },
                function() { map.HideInfoBox(shapes[i]); }
            );
        });
    }, "json");
    
    //alert("here6");
}

