﻿/****************************************
*                                       *
*   Global variables                    *
*                                       *
*****************************************/

var map;

/****************************************
*                                       *
*   Constants                           *
*                                       *
*****************************************/

var zoomLevels = {   // [min, max]
    "None": [1, 4],
    "Counties": [5, 9],
    "Areas": [10, 14],
    "Offices": [12, 17],
    "Listings": [15, 17]
};

var iconRoot = "/Assets/googlemaps/";
var iconSuffix = "_icon.png";
var icons = {
    "County": new GIcon(G_DEFAULT_ICON, iconRoot + "county" + iconSuffix),
    "Area": new GIcon(G_DEFAULT_ICON, iconRoot + "area" + iconSuffix),
    "Office": new GIcon(G_DEFAULT_ICON, iconRoot + "office" + iconSuffix),
    "Listing": new GIcon(G_DEFAULT_ICON, iconRoot + "noth" + iconSuffix),
    "OpenHouse": new GIcon(G_DEFAULT_ICON, iconRoot + "oh" + iconSuffix)
}

/****************************************
*                                       *
*   Initial load                        *
*                                       *
*****************************************/

$(document).unload(function() {
    GUnload();
});


function initializeMap(latitude, longitude, zoom, mapType) {
    if (GBrowserIsCompatible()) {
        var map = new GMap2($("#map_canvas").get(0), { size: new GSize($("#map_canvas").width(), $("#map_canvas").height()) });
        map.setCenter(new GLatLng(latitude, longitude), zoom);
        map.setUIToDefault();
        if (mapType != null)
            map.setMapType(mapType);
        map.disableScrollWheelZoom();
        trackMapView();
        return map;
    }
}

//Track map view via local service
function trackMapView(location) {
    location = window.location.pathname.toLowerCase();
    if (location != undefined && location.length > 0) {
        trackMapViewWithLocation(location);
    }
    else {
        trackMapViewWithoutLocation();
    }
}

function trackMapViewWithLocation(location) {
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "/Services/TrackGoogleMaps.asmx/TrackMapViewLocation",
        data: "{'location':'" + location + "'}",
        dataType: "json"
    });
}

function trackMapViewWithoutLocation() {
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "/Services/TrackGoogleMaps.asmx/TrackMapView",
        dataType: "json"
    });
}
