
  var _bufferPct = 1;
  var _constMarkerTypeState = "State";
  var _constMarkerTypeZIP = "ZIP";
  var _constMarkerTypeZIP3 = "ZIP3";
  var _radiusPolygon = null;
  var _triggerPct = 1;
  var markerClustererStates = null;
  var markerClustererZIPs = null;
  var markerClustererZIP3s = null;
  var maxZoomLvlState = 6;
  var maxZoomLvlZIP = 99;
  var maxZoomLvlZIP3 = 9;
  var minZoomLvlState = 0;
  var minZoomLvlZIP = 10;
  var minZoomLvlZIP3 = 7;

  function getCountsForOccupation() {
    if (markerClustererZIPs !== null) {
      markerClustererZIPs.clearMarkers();
      markerClustererZIPs = null;
    }
    if (markerClustererStates !== null) {
      markerClustererStates.clearMarkers();
      markerClustererStates = null;
    }
    if (markerClustererZIP3s !== null) {
      markerClustererZIP3s.clearMarkers();
      markerClustererZIP3s = null;
    }
    var radius;
    if (_searchType == _constSearchTypeZip) {
      radius = $("#Radius1Txt").val();
    } else if (_searchType == _constSearchTypeMetro) {
      radius = 35;
    } else if (_searchType == _constSearchTypeState) {
      radius = 0;
    } else {
      alert("_searchType not recognized!");
    }
    var zoom = getZoomForRadius(radius);
    $("#map_ghost").show();
    $("#search_loading").show();
    if (zoom >= minZoomLvlZIP && zoom <= maxZoomLvlZIP) {
      getZIPCountsForOccupation(true);
    } else if (zoom >= minZoomLvlZIP3 && zoom <= maxZoomLvlZIP3) {
      getZIP3CountsForOccupation(true);
    } else if (zoom >= minZoomLvlState && zoom <= maxZoomLvlState) {
      getStateCountsForOccupation(true);
    }
  }
  function getCountsForOccupationMore() {
    var zoom = mymap.getZoom();
    if (zoom >= minZoomLvlZIP && zoom <= maxZoomLvlZIP) {
      getZIPCountsForOccupation(false);
    } else if (zoom >= minZoomLvlZIP3 && zoom <= maxZoomLvlZIP3) {
      getZIP3CountsForOccupation(false);
    } else if (zoom >= minZoomLvlState && zoom <= maxZoomLvlState) {
      getStateCountsForOccupation(false);
    }
  }
  
  function getZIPCountsForOccupation(newSearch) {
    var latTop = -1;
    var latBottom = -1;
    var lngLeft = -1;
    var lngRight = -1;
    var latBuffer = -1;
    var lngBuffer = -1;
    var radius;
    var entityId;
    var entityTypeId;
    var zip;
    if (newSearch) {
      if (_searchType == _constSearchTypeZip) {
        zip = $("#Zip1Txt").val();
        radius = $("#Radius1Txt").val();
      } else if (_searchType == _constSearchTypeMetro) {
        zip = metroZip;
        radius = 35;
      } else {
        alert("_searchType not recognized!");
      }
      var mapLatHeight = getLatHeightForRadius(radius);
      var mapLngWidth = getLngWidthForRadius(radius);
      latBuffer = (mapLatHeight / 2) + (mapLatHeight * (_bufferPct / 100));
      lngBuffer = (mapLngWidth / 2) + (mapLngWidth * (_bufferPct / 100));
      //
      entityId = occupationEntityId;
      entityTypeId = occupationEntityTypeId;
    } else {
      var mapBounds = mymap.getBounds();
      latTop = mapBounds.getNorthEast().lat() + (Math.abs(mapBounds.getSouthWest().lat() - mapBounds.getNorthEast().lat()) * (_bufferPct / 100));
      latBottom = mapBounds.getSouthWest().lat() - (Math.abs(mapBounds.getSouthWest().lat() - mapBounds.getNorthEast().lat()) * (_bufferPct / 100));
      lngLeft = mapBounds.getSouthWest().lng() - (Math.abs(mapBounds.getNorthEast().lng() - mapBounds.getSouthWest().lng()) * (_bufferPct / 100));
      lngRight = mapBounds.getNorthEast().lng() + (Math.abs(mapBounds.getNorthEast().lng() - mapBounds.getSouthWest().lng()) * (_bufferPct / 100));
      //
      entityId = searchOccupationEntityId;
      entityTypeId = searchOccupationEntityTypeId;
      radius = searchRadius;
      zip = searchZip;
    }
    $.post("/Ajax/getZipCountsForOccupation.aspx", {
        latBuffer: latBuffer,
        lngBuffer: lngBuffer,
        latTop: latTop.toFixed(5),
        latBottom: latBottom.toFixed(5),
        lngLeft: lngLeft.toFixed(5),
        lngRight: lngRight.toFixed(5),
        newSearch: newSearch,
        occupationEntityId: entityId,
        occupationEntityTypeId: entityTypeId,
        radius: radius,
        ZIP: zip
    }, function(result) {
        var ZIPMarkers = [];
        var items = result.split(";~;");
        var newSearch = items[0];
        var searchItems = items[1].split(",~,");
        var ZIPItems = items[2].split(":~:");
        var item;
        var city;
        var cntDirectMail;
        var cntEmail;
        var distance;
        var eeoAsian;
        var eeoBlack;
        var eeoHispanic;
        var eeoOther;
        var eeoPopulation;
        var eeoNonWhite;
        var lat;
        var lng;
        var state;
        var ZIP;
        var ZIP3;
        var marker;
        var searchZIPAdded = false;
        if (newSearch == "true") {
          searchCity = searchItems[0];
          searchStateAbbr = searchItems[1];
          searchZip = searchItems[2];
          searchZip3 = searchItems[3];
          searchZIPLat = searchItems[4];
          searchZIPLng = searchItems[5];
          searchRadius = searchItems[6];
        }
        // ZIP count results
        if (ZIPItems[0] != "") {
          for (var i = 0; i < ZIPItems.length; i++) {
            item = ZIPItems[i].split(",~,");
            ZIP = item[0];
            ZIP3 = item[1];
            lat = item[2];
            lng = item[3];
            cntDirectMail = item[4];
            cntEmail = item[5];
            city = item[6];
            state = item[7];
            distance = item[8];
            eeoNonWhite = item[9];
            eeoBlack = item[10];
            eeoAsian = item[11];
            eeoOther = item[12];
            eeoHispanic = item[13];
            eeoPopulation = item[15];
            if (ZIP == searchZip) {
              searchZIPAdded = true;
            }
            marker = new LightMarker(city, cntDirectMail, cntEmail, distance, eeoAsian, eeoBlack, eeoHispanic, eeoOther, eeoPopulation, eeoNonWhite, lat, lng, state, ZIP, ZIP3, '', (ZIP == searchZip));
            ZIPMarkers.push(marker);
          }
        }
        //
        if (!searchZIPAdded && _searchType != _constSearchTypeState) {
          marker = new LightMarker(searchCity, 0, 0, 0, -1, -1, -1, -1, -1, -1, searchZIPLat, searchZIPLng, searchStateAbbr, searchZip, "-1", '', true);
          ZIPMarkers.push(marker);
        }
        if (newSearch == "true") {
          processNewSearchResults();
          refreshStateMarkers([]);
          refreshZIP3Markers([]);
        }
        refreshZIPMarkers(ZIPMarkers);
    });
  }
  
  function getZIP3CountsForOccupation(newSearch) {
    var entityId;
    var entityTypeId;
    var zip;
    if (newSearch) {
      if (_searchType == _constSearchTypeZip) {
        zip = $("#Zip1Txt").val();
        radius = $("#Radius1Txt").val();
      } else if (_searchType == _constSearchTypeMetro) {
        zip = metroZip;
        radius = 35;
      } else {
        alert("_searchType not recognized!");
      }
      entityId = occupationEntityId;
      entityTypeId = occupationEntityTypeId;
    } else {
      entityId = searchOccupationEntityId;
      entityTypeId = searchOccupationEntityTypeId;
      radius = searchRadius;
      zip = searchZip;
    }
    $.post("/Ajax/getZip3CountsForOccupation.aspx", {
        newSearch: newSearch,
        occupationEntityId: entityId,
        occupationEntityTypeId: entityTypeId,
        radius: radius,
        ZIP: zip
    }, function(result) {
        var ZIP3Markers = [];
        var items = result.split(";~;");
        var newSearch = items[0];
        var searchItems = items[1].split(",~,");
        var ZIP3Items = items[2].split(":~:");
        var item;
        var city;
        var cntDirectMail;
        var cntEmail;
        var distance;
        var eeoAsian;
        var eeoBlack;
        var eeoHispanic;
        var eeoOther;
        var eeoPopulation;
        var eeoNonWhite;
        var lat;
        var lng;
        var state;
        var ZIP;
        var ZIP3;
        var marker;
        var searchZIP3Added = false;
        if (newSearch == "true") {
          searchCity = searchItems[0];
          searchStateAbbr = searchItems[1];
          searchZip = searchItems[2];
          searchZip3 = searchItems[3];
          searchZIPLat = searchItems[4];
          searchZIPLng = searchItems[5];
          searchRadius = searchItems[6];
        }
        // ZIP3 count results
        if (ZIP3Items[0] != "") {
          for (var i = 0; i < ZIP3Items.length; i++) {
            item = ZIP3Items[i].split(",~,");
            ZIP = item[0];
            ZIP3 = item[1];
            lat = Number(item[2]);
            lng = Number(item[3]);
            cntDirectMail = Number(item[4]);
            cntEmail = Number(item[5]);
            city = item[6];
            state = item[7];
            distance = Number(item[8]);
            eeoNonWhite = Number(item[9]);
            eeoBlack = Number(item[10]);
            eeoAsian = Number(item[11]);
            eeoOther = Number(item[12]);
            eeoHispanic = Number(item[13]);
            eeoPopulation = Number(item[15]);
            if (ZIP3 == searchZip3) {
              searchZIPAdded = true;
              lat = searchZIPLat;
              lng = searchZIPLng;
            }
            marker = new LightMarker(city, cntDirectMail, cntEmail, distance, eeoAsian, eeoBlack, eeoHispanic, eeoOther, eeoPopulation, eeoNonWhite, lat, lng, state, ZIP, ZIP3, '', (ZIP3 == searchZip3));
            ZIP3Markers.push(marker);
          }
        }
        //
        if (!searchZIP3Added && _searchType != _constSearchTypeState) {
          marker = new LightMarker("-", 0, 0, 0, -1, -1, -1, -1, -1, -1, searchZIPLat, searchZIPLng, searchStateAbbr, "-1", searchZip3, '', true);
          ZIP3Markers.push(marker);
        }
        if (newSearch == "true") {
          processNewSearchResults();
          refreshZIPMarkers([]);
          refreshStateMarkers([]);
        }
        refreshZIP3Markers(ZIP3Markers);
    });
  }
  
  function getStateCountsForOccupation(newSearch) {
    var entityId;
    var entityTypeId;
    var radius;
    var state;
    var zip;
    if (newSearch) {
      if (_searchType == _constSearchTypeZip) {
        state = "";
        zip = $("#Zip1Txt").val();
        radius = $("#Radius1Txt").val();
      } else if (_searchType == _constSearchTypeMetro) {
        state = "";
        zip = metroZip;
        radius = 35;
      } else if (_searchType == _constSearchTypeState) {
        state = $("#States1DDL :selected").text();
        zip = "";
        radius = 0;
      } else {
        alert("_searchType not recognized!");
      }
      entityId = occupationEntityId;
      entityTypeId = occupationEntityTypeId;
    } else {
      entityId = searchOccupationEntityId;
      entityTypeId = searchOccupationEntityTypeId;
      radius = searchRadius;
      state = searchStateAbbr;
      zip = searchZip;
    }
    $.post("/Ajax/getStateCountsForOccupation.aspx", {
        newSearch: newSearch,
        occupationEntityId: entityId,
        occupationEntityTypeId: entityTypeId,
        radius: radius,
        state: state,
        ZIP: zip
    }, function(result) {
        var stateMarkers = [];
        var items = result.split(";~;");
        var newSearch = items[0];
        var searchItems = items[1].split(",~,");
        var stateItems = items[2].split(":~:");
        var item;
        var city;
        var cntDirectMail;
        var cntEmail;
        var distance;
        var eeoAsian;
        var eeoBlack;
        var eeoHispanic;
        var eeoOther;
        var eeoPopulation;
        var eeoNonWhite;
        var lat;
        var lng;
        var state;
        var ZIP;
        var ZIP3;
        var marker;
        if (newSearch == "true") {
          searchCity = searchItems[0];
          searchStateAbbr = searchItems[1];
          searchZip = searchItems[2];
          searchZip3 = searchItems[3];
          searchZIPLat = searchItems[4];
          searchZIPLng = searchItems[5];
          searchRadius = searchItems[6];
        }
        // State count results
        if (stateItems[0] != "") {
          for (var i = 0; i < stateItems.length; i++) {
            item = stateItems[i].split(",~,");
            ZIP = item[0];
            ZIP3 = item[1];
            lat = Number(item[2]);
            lng = Number(item[3]);
            cntDirectMail = Number(item[4]);
            cntEmail = Number(item[5]);
            city = item[6];
            state = item[7];
            distance = Number(item[8]);
            eeoNonWhite = Number(item[9]);
            eeoBlack = Number(item[10]);
            eeoAsian = Number(item[11]);
            eeoOther = Number(item[12]);
            eeoHispanic = Number(item[13]);
            eeoPopulation = Number(item[15]);
            //
            if (newSearch == "true" && state == (searchStateAbbr == '-' ? 'KS' : searchStateAbbr) && _searchType == _constSearchTypeState) {
              searchZIPLat = lat;
              searchZIPLng = lng;
            }
            if (state == searchStateAbbr) {
              lat = searchZIPLat;
              lng = searchZIPLng;
            }
            marker = new LightMarker(city, cntDirectMail, cntEmail, distance, eeoAsian, eeoBlack, eeoHispanic, eeoOther, eeoPopulation, eeoNonWhite, lat, lng, state, ZIP, ZIP3, '', (state == searchStateAbbr));
            stateMarkers.push(marker);
          }
        }
        //
        if (newSearch == "true") {
          processNewSearchResults();
          refreshZIPMarkers([]);
          refreshZIP3Markers([]);
        }
        refreshStateMarkers(stateMarkers);
    });
  }
  
  function processNewSearchResults() {
    if (_radiusPolygon !== null) {
      _radiusPolygon.setMap(null);
    }
    // Radius, ZIP tiles, center map, update markers
    if (_searchType != _constSearchTypeState) {
      _radiusPolygon = createRadius(searchZIPLat, searchZIPLng, searchRadius, '#2A6FC7', 5, 1, '#2A6FC7', 0);
      _radiusPolygon.setMap(mymap);
    }
    var radiusZoom;
    if (searchStateAbbr == '-') {
      radiusZoom = getZoomForRadius(-1);
    } else {
      radiusZoom = getZoomForRadius(searchRadius);
    }
    mymap.setZoom(radiusZoom);
    mymap.setCenter(new google.maps.LatLng(searchZIPLat, searchZIPLng));
    $("#map_ghost").hide();
    $("#search_loading").hide();
  }
  
  function createRadius(lat, lng, radius, strokeColor, strokeWidth, strokeOpacity, fillColor, fillOpacity) {
    var circleOpts = {radius: (searchRadius*1609.344),
                               center: new google.maps.LatLng(searchZIPLat, searchZIPLng),
                               clickable: false,
                               fillOpacity: fillOpacity,
                               strokeColor: strokeColor,
                               strokeOpacity: strokeOpacity,
                               strokeWeight: strokeWidth};
    
    var circle = new google.maps.Circle(circleOpts);
    return circle;
  }
  
  function getLatHeightForRadius(radius) {
    if (radius <= 1) { return 0.0327655; }
    else if (radius == 2) { return 0.065531; }
    else if (radius >= 3 && radius <= 4) { return 0.131062; }
    else if (radius >= 5 && radius <= 8) { return 0.262124; }
    else if (radius >= 9 && radius <= 17) { return 0.5242491; }
    else if (radius >= 18 && radius <= 34) { return 1.048495; }
    else if (radius >= 35 && radius <= 69) { return 2.096966; }
    else if (radius >= 70 && radius <= 139) { return 4.193737; }
    else if (radius >= 140 && radius <= 279) { return 8.385896; }
    else if (radius >= 280 && radius <= 554) { return 16.75892; }
    else if (radius >= 555 && radius <= 1084) { return 33.40647; }
    else if (radius >= 1085 && radius <= 1986) { return 65.70303; }
    else if (radius >= 1987 && radius <= 3014) { return 119.7155; }
    else { return 170; }
  }
  function getLngWidthForRadius(radius) {
    if (radius <= 1) { return 0.03673553; }
    else if (radius == 2) { return 0.07347107; }
    else if (radius >= 3 && radius <= 4) { return 0.1469421; }
    else if (radius >= 5 && radius <= 8) { return 0.2938843; }
    else if (radius >= 9 && radius <= 17) { return 0.5877686; }
    else if (radius >= 18 && radius <= 34) { return 1.175537; }
    else if (radius >= 35 && radius <= 69) { return 2.351074; }
    else if (radius >= 70 && radius <= 139) { return 4.702148; }
    else if (radius >= 140 && radius <= 279) { return 9.404297; }
    else if (radius >= 280 && radius <= 554) { return 18.80859; }
    else if (radius >= 555 && radius <= 1084) { return 37.61719; }
    else if (radius >= 1085 && radius <= 1986) { return 75.23438; }
    else if (radius >= 1987 && radius <= 3014) { return 150.4688; }
    else { return 360; }
  }
  function getZoomForRadius(radius) {
    if (radius == -1) { return 3; }
    else if (radius == 0) { return 6; }
    else if (radius == 1) { return 14; }
    else if (radius == 2) { return 13; }
    else if (radius >= 3 && radius <= 4) { return 12; }
    else if (radius >= 5 && radius <= 8) { return 11; }
    else if (radius >= 9 && radius <= 17) { return 10; }
    else if (radius >= 18 && radius <= 34) { return 9; }
    else if (radius >= 35 && radius <= 69) { return 8; }
    else if (radius >= 70 && radius <= 139) { return 7; }
    else if (radius >= 140 && radius <= 279) { return 6; }
    else if (radius >= 280 && radius <= 554) { return 5; }
    else if (radius >= 555 && radius <= 1084) { return 4; }
    else if (radius >= 1085 && radius <= 1986) { return 3; }
    else if (radius >= 1987 && radius <= 3014) { return 2; }
    else { return 1; }
  }
   
  function refreshZIPMarkers(ZIPMarkers) {
    if (markerClustererZIPs !== null) {
      markerClustererZIPs.clearMarkers();
    }
    markerClustererZIPs = new MarkerClusterer(mymap, ZIPMarkers, 'ClusterDetailsCvs', 'ClusterDetailsGrd', 'MarkerInfoCvs', _constMarkerTypeZIP, minZoomLvlZIP, maxZoomLvlZIP);
  }
  function refreshZIP3Markers(ZIP3Markers) {
    if (markerClustererZIP3s !== null) {
      markerClustererZIP3s.clearMarkers();
    }
    markerClustererZIP3s = new MarkerClusterer(mymap, ZIP3Markers, 'ClusterDetailsCvs', 'ClusterDetailsGrd', 'MarkerInfoCvs', _constMarkerTypeZIP3, minZoomLvlZIP3, maxZoomLvlZIP3);
  }
  function refreshStateMarkers(stateMarkers) {
    if (markerClustererStates !== null) {
      markerClustererStates.clearMarkers();
    }
    markerClustererStates = new MarkerClusterer(mymap, stateMarkers, 'ClusterDetailsCvs', 'ClusterDetailsGrd', 'MarkerInfoCvs', _constMarkerTypeState, minZoomLvlState, maxZoomLvlState);
  }
  
  function displayClusterDetailsPopup(cluster) {
    pageTracker._trackPageview('/marker_click');
    $("#ClusterDetailsOccupationTxt").html(searchOccupationNamePlural);
    var cntEEOPopulationTotal = cluster.getCntEEOPopulationTotal();
    var cntNonWhite = (cntEEOPopulationTotal <= 0 ? -1 : (cluster.getCntEEOAsianTotal() + cluster.getCntEEOBlackTotal() + cluster.getCntEEOOtherTotal()));
    var pctNonWhite = (cntEEOPopulationTotal <= 0 ? 50 : ((cntNonWhite * 100) / cntEEOPopulationTotal));
    var pctWhite = 100 - pctNonWhite;
    var pctNonWhiteTxt = (cntEEOPopulationTotal <= 0 ? '-' : (pctNonWhite.toFixed(2) + '%'));
    var pctWhiteTxt = (cntEEOPopulationTotal <= 0 ? '-' : (pctWhite.toFixed(2) + '%'));
    $("#ClusterDetailsCountSummaryTxt").html(addCommas(cluster.getCntDirectMailTotal()) + ' Mailing Addresses' +
                                             (cluster.getCntEmailTotal() == 0 ? '' : ('<br/>' + addCommas(cluster.getCntEmailTotal()) + ' E-Mail Addresses')));
    $("#ClusterDetailsPctWhiteTxt").html(pctWhiteTxt);
    $("#ClusterDetailsPctNonWhiteTxt").html(pctNonWhiteTxt);
    $("#ClusterDetailsPopulationTxt").html(addCommas(cntEEOPopulationTotal));
    $("#ClusterDetailsPieChartImg").attr("src","/images/piechart/piechart_" + pctNonWhite.toFixed(0) + ".png");
    
    
    /*
      var projection = projectionOverlay_.getProjection();
      var markerLatLng = cluster.getCenter();
      var pos = projection.fromLatLngToDivPixel(new google.maps.LatLng(markerLatLng.lat(), markerLatLng.lng()));
      //alert(pos);
      var markerX = pos.x;
      var markerY = pos.y;
      
      if (markerX < 136) {
         mymap.panBy((markerX - 136), 0);
         markerX = 136;
      } else if (mapstate == "open" && markerX > 229) {
         mymap.panBy((markerX - 229), 0);
         markerX = 229;
      } else if (mapstate == "closed" && markerX > 689) {
         mymap.panBy((markerX - 689), 0);
         markerX = 689;
      }
      
      if (markerY < 244) {
         mymap.panBy(0, (markerY - 244));
         markerY = 244;
      //} else if (markerY > 400) {
      //   mymap.panBy(0, (markerY - 400));
      //   markerY = 400;
      }
      
      if (mapstate == "open") {
         $("#marker_pop").css( { "left": (markerX - 126 + 561) + "px", "top": (markerY - 234 + 86) + "px" } );
      } else {
         $("#marker_pop").css( { "left": (markerX - 126 + 31) + "px", "top": (markerY - 234 + 86) + "px" } );
      }
*/
      
    
      var clusterCenter = cluster.getCenter();
      var mapCenter = mymap.getCenter();
      var posX;
      var posY;
      
      if (mapstate == "open") {
         posX = 622;
      } else {
         if (clusterCenter.lng() > mapCenter.lng()) {
            posX = 83;
         } else {
            posX = 622;
         }
      }
      if (clusterCenter.lat() > mapCenter.lat()) {
         posY = 380;
      } else {
         posY = 96;
      }
         
      $("#marker_pop").css( { "left": posX + "px", "top": posY + "px" } );
      
      
	 $("#marker_pop").show();
  }
