﻿// this file contains utility variables / methods used to render Google Maps on the physician details control and the default find a location page.
var mapv3;
var defaultNsIcon = "/images/googlemaps/marker2.png";
var shadowImg = "/images/googlemaps/shadow50.png";
var currentWindow;
var bounds = new google.maps.LatLngBounds();
var boundsCount = 0;

function CreateDefaultMap(elementId, zoom) {
    var initLatLng = new google.maps.LatLng(42.160506, -87.96087);
    var myOptions = {
        zoom: zoom,
        center: initLatLng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    mapv3 = new google.maps.Map(document.getElementById(elementId), myOptions);
}
function AddLocationsToMap(ids, markerIcon, markerArray) {
    $.ajax({
        url: "/webservices/mglocationservice.svc/GetAnyLocationCoordinatesByIds",
        data: '{ "locationIdsString": "' + ids + '" }',
        type: "POST",
        processData: true,
        contentType: "application/json",
        timeout: 10000,
        dataType: "json",
        success: function(data) { AddLocationMarkers(data, markerIcon, markerArray); },
        error: function() { alert('Error loading locations map.'); }
    });
}
function AddLocationMarkers(data, markerIcon, markerArray) {
    var dynamicIcons = false;
    if (markerIcon == undefined)
        dynamicIcons = true;
    for (var i = 0; i < data.d.length; i++) {
        if (dynamicIcons) markerIcon = "/images/googlemaps/marker" + String.fromCharCode("a".charCodeAt(0) + i) + ".png";
        var tempLatLng = new google.maps.LatLng(data.d[i].Lat, data.d[i].Long);
        bounds.extend(tempLatLng);
        boundsCount++;
        var marker = new google.maps.Marker({
            position: tempLatLng,
            map: mapv3,
            icon: markerIcon,
            shadow: shadowImg,
            title: data.d[i].Title
        });
        if (markerArray != undefined)
            markerArray.push(marker);
        AddInfoWindow(marker, GenerateInfoWindowHtml(data.d[i]));
    }
    // if you fitBounds on a single point, it does a max zoom on that point == hard to tell if the map rendered correctly due to lack of persepctive.
    // therefore, center the map if there's a single point in the bounds collection.
    if (boundsCount == 1)
        mapv3.setCenter(bounds.getCenter());
    else if (boundsCount > 1)
        mapv3.fitBounds(bounds);
}
function GenerateInfoWindowHtml(data) {
    var html;
    if (data.IsMedicalGroupLocation)
        html = '<a href="/apps/findalocation/location.aspx?locid=' + data.LocationId + '"><h2>' + data.Title + '</h2></a>';
    else
        html = '<h2>' + data.Title + '</h2>';
    html = html + '<div>' + data.Phone + '</div>';
    html = html + '<div>' + data.Address1 + '</div>';
    if (data.Address2 != '') {
        html = html + '<div>' + data.Address2 + '</div>';
    }
    html = html + '<div>' + data.City + ', ' + data.State + ' ' + data.Zip + '</div>';
    return html;
}
function AddInfoWindow(marker, content) {
    var infowindow = new google.maps.InfoWindow({ content: content });
    google.maps.event.addListener(marker, 'click', function() {
        if (currentWindow) currentWindow.close();
        infowindow.open(mapv3, marker);
        currentWindow = infowindow;
    });
}
// locations should be an array of google marker objects
function SetMarkersVisible(locations, visible) {
    for (i in locations) {
        if (!visible) {
            locations[i].setMap(null);
        }
        else {
            locations[i].setMap(mapv3);
        }
    }
}
