var map;

function LoadMap(CenterLat, CenterLon, Zoom) {
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("Map"));
        var centerMap = new GLatLng(CenterLat, CenterLon);
        map.setCenter(centerMap, Zoom);
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
    }
}

function ClearMap() {
    map.clearOverlays();
}

function ShowInfo(id) {
    document.getElementById(mapHdnID).value = id;
    __doPostBack(mapBtnDetailsPostBackID, '');
    document.getElementById(searchHdnID).value = id;
    __doPostBack(searchBtnDetailsPostBackID, '');
}

function AddMarker(Icon, Lat, Lon, Id, FacilityName, Phone, Address, FromURL, ToURL, IsSchool) {
    var icn = new GIcon();
    icn.iconSize = new GSize(28, 28);
    icn.iconAnchor = new GPoint(14, 14);
    icn.infoWindowAnchor = new GPoint(14, 28);
    icn.infoShadowAnchor = new GPoint(14, 28);
    icn.image = Icon;
    var edit = '<span class="EditFacility"><a href="/survey/edit.aspx?id=' + Id + '" target="_blank">Edit this facility</a><br />';
    var detail = '<a href="javascript:void(0);" onclick="ShowInfo(' + Id + ');">Show More Information</a><br />';
    if (IsSchool) edit = '';
    if (IsSchool) detail = '';

    var Content = '<table>';
    Content = Content + '<tr>';
    Content = Content + '<td>';
    Content = Content + '<div class="InfoWindow">';
    if (FacilityName != "") {
        Content = Content + '<span class="InfoName">';
        Content = Content + FacilityName;
        Content = Content + '</span>';
        Content = Content + '<br />';
    }
    if (Phone != "") {
        Content = Content + '<span class=\"InfoPhone\">';
        Content = Content + Phone;
        Content = Content + '</span>';
        Content = Content + '<br />';
    }
    if (Address != "") {
        Content = Content + '<span class=\"InfoAddress\">';
        Content = Content + Address;
        Content = Content + '</span>';
        Content = Content + '<br />';
    }
    Content = Content + detail;
    Content = Content + edit;
    Content = Content + '</div>';
    Content = Content + '</td>';
    Content = Content + '<td>';

    if (Icon != "") {
        Content = Content + '<img src="' + Icon.replace('.png', 'lg.png') + '" />';
    }

    Content = Content + '</td>' +
                    '</tr>' +
                    '<tr>' +
                        '<td colspan="2">' + '<span class="InfoDirections">Directions: <a href="' + FromURL + '" target="_blank">From Here</a> | <a href="' + ToURL + '" target="_blank">To Here</a></span>' +
                        '</td>' +
                    '</tr>' +
                '</table>';

    var marker = new GMarker(new GLatLng(Lat, Lon));
    if (Icon != '') marker = new GMarker(new GLatLng(Lat, Lon), { icon: icn });
    GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(Content); });
    map.addOverlay(marker);
}

function ShowLoading() {
    document.getElementById('Loading').style.display = 'block';
}

function HideLoading() {
    document.getElementById('Loading').style.display = 'none';
}

function goToLocation(id) {
    opener.location = '/map/default.aspx?locationId=' + id;
    self.close();
    return false;
}
function openLocations() {
    childWindow = open("/survey/gotomaplist.aspx", "Window1", "menubar=no,width=1150,height=800,toolbar=no,scrollbars=yes");
    if (childWindow.opener == null) childWindow.opener = self;
}
