﻿var map, polygon, gridID, dispState, dispCounty, curZoom, geocoder, prevPoint = null, y = 0, x = 0, gRows = 0, gCols = 0, zGridDraw = 10;color = "#005500";
function initialize() {
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map_canvas"));
        map.enableScrollWheelZoom();
        map.addControl(new GLargeMapControl3D);
        map.addControl(new GMapTypeControl);
        map.setCenter(new GLatLng(33.1576, -99.7337), 6);
        GEvent.addListener(map, "maptypechanged", checkColor);
        GEvent.addListener(map, "zoomstart", mapClearDraw);
        GEvent.addListener(map, "dragend", mapClearDraw);
        GEvent.addListener(map, "moveend", function() {
            map.getZoom() < zGridDraw ? mapClearDraw() : refreshGrids()
        })
    };
}
function checkColor() {
    color = map.getCurrentMapType() == G_NORMAL_MAP ? "#005500" : "#fff";
    mapClearDraw()
}
function mapClearDraw() {
    map.clearOverlays();
    map.getZoom() >= zGridDraw && refreshGrids()
}
function refreshGrids() {
    var a = map.getBounds().getNorthEast().lng(), b = map.getBounds().getSouthWest().lat(), e = map.getBounds().getNorthEast().lat(), d = map.getBounds().getSouthWest().lng(), c = Math.ceil((e - parseInt(e, 0)) / 0.25), g = parseInt(e, 0), f = Math.floor((d - parseInt(d, 0)) / 0.25);
    if (a * -1 > 0) {
        d = parseInt(d, 0);
        c = g - 0.25 + c * 0.25;
        g = f = d + f * 0.25;
        if (c > 49.25) c = 49.25;
        if (c < 25) c = 25;
        if (f < -125.25) f = -125.25;
        if (f > -69.25) f = -69.25;
        a -= f;
        b = e - b;
        gCols = gRows = 0;
        gRows = b > 0.5 ? 3 : b > 0.3 ? 2 : 1;
        gCols = a > 1.2 ? a * 4 : a > 0.6 ? (gCols = a * 4) : 2;
        for (y = x = y = 0; y <= gRows; y++) {
            for (x = 0; x <= gCols; x++) {
                polygon = new GPolygon([new GLatLng(c, f), new GLatLng(c + 0.25, f), new GLatLng(c + 0.25, f + 0.25), new GLatLng(c, f + 0.25), new GLatLng(c, f)], color, 1, 1, "#fff", 0);
                f += 0.25;
                map.addOverlay(polygon);
                handleClick(polygon, map)
            } f = g;
            c -= 0.25
        }
    };
}
function searchFromDD() {
    var a = document.getElementById("ctl00_ContentPlaceHolder1_ddState");
    a = a.options[a.selectedIndex].text;
    var b = document.getElementById("ctl00_ContentPlaceHolder1_ddCounty");
    b = b.options[b.selectedIndex].text;
    document.getElementById("searchLoc").value = b + ", " + a + " County";
    searchLocations()
}
function searchLocations() {
    var a = document.getElementById("searchLoc").value;
    if (!(a == "")) {
        document.getElementById("searchLoc").value = "";
        geocoder = new GClientGeocoder;
        geocoder.getLocations(a, searchToMap)
    }
}
function searchToMap(a) {
    if (a.Status.code != G_GEO_SUCCESS) alert("Location Not Found");
    else {
        map.clearOverlays();
        document.getElementById("searchLoc").value = "";
        curZoom = map.getZoom();
        if (curZoom < zGridDraw) curZoom = zGridDraw;
        place = a.Placemark[0];
        point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
        map.setCenter(point, curZoom);
        a = new GMarker(point);
        map.addOverlay(a);
        prevPoint = a;
        calculateGridID(point);
        popInfoWin()
    }
}
function popInfoWin() {
    try {
        if (place.AddressDetails.Country.CountryNameCode == "US") {
            var a = place.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName;
            try {
                var b = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName;
                b += ", ";
            } catch (e) {
                b = "";
            } try {
                var d = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.SubAdministrativeAreaName
                dispState = a;
                dispCounty = d;
                document.getElementById("ctl00_ContentPlaceHolder1_HiddenState").value = dispState;
                document.getElementById("ctl00_ContentPlaceHolder1_HiddenCounty").value = dispCounty;
                document.getElementById("ctl00_ContentPlaceHolder1_hiddenVar").click();
                prevPoint.openInfoWindow("<p style='padding:0px;margin:0px;'><span style='font-weight:bold;'>County: </span>" + d + "</p><p style='padding:0px;margin:0px;'><span style='font-weight:bold;'>Location: </span>" + b + a + "</p><p style='padding:0px;margin:0px;'><span style='font-weight:bold;'>Grid ID#: </span>" + gridID + "</p>")

            } catch (c) {
                var z = parseInt(place.Point.coordinates[1]) + ".001 , " + parseInt(place.Point.coordinates[0]) + ".001";
                geocoder = new GClientGeocoder;
                geocoder.getLocations(z, searchToMap)
            }
        }
        else {
            map.clearOverlays();
         };
        } 
    catch (g) {
    }
}
function handleClick(a, b) {
    GEvent.addListener(a, "click", function(e) {
        mapClearDraw();
        test(e);
        geocoder = new GClientGeocoder;
        geocoder.getLocations(e, addToMap)
    })
}
function test(a) {
    prevPoint && map.removeOverlay(prevPoint);
    curZoom = map.getZoom();
    if (curZoom < zGridDraw) curZoom = zGridDraw;
    calculateGridID(a);
    a = new GMarker(a);
    map.addOverlay(a);
    prevPoint = a
}
function addToMap(a) {
    if (a.Status.code != G_GEO_SUCCESS) alert("Location Not Found");
    else {
        place = a.Placemark[0];
        point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
        popInfoWin()
    }
}
function calculateGridID(a) {
    var b = Math.ceil((a.x - parseInt(a.x, 0)) / 0.25), e = parseInt(a.x, 0), d = Math.floor((a.y - parseInt(a.y, 0)) / 0.25);
    a = parseInt(a.y, 0) + d * 0.25;
    var c = 0;
    d = e - 0.25 + b * 0.25;
    c = a;
    c -= 20;
    c *= 1200;
    d -= -130.25;
    d *= 4;
    gridID = d + c;
    document.getElementById("ctl00_ContentPlaceHolder1_hiddenGridId").value = gridID
}
function disableEnterKey(e) {
    var key;
    if (window.event)
        key = window.event.keyCode; //IE
    else
        key = e.which; //firefox
    if (key == 13) { document.getElementById("searchBtn").focus(); };
    return (key != 13);
}

