text: var map, featureList, hotmealSearch = [], pantrySearch = [];
  
  $(window).resize(function() {
    sizeLayerControl();
  });
  
  $(document).on("click", ".feature-row", function(e) {
    $(document).off("mouseout", ".feature-row", clearHighlight);
    sidebarClick(parseInt($(this).attr("id"), 10));
  });
  
  $(document).on("mouseover", ".feature-row", function(e) {
    highlight.clearLayers().addLayer(L.circleMarker([$(this).attr("lat"), $(this).attr("lng")], highlightStyle));
  });
  
  $(document).on("mouseout", ".feature-row", clearHighlight);
  
  $("#about-btn").click(function() {
    $("#aboutModal").modal("show");
    $(".navbar-collapse.in").collapse("hide");
    return false;
  });
  
  $("#full-extent-btn").click(function() {
    map.fitBounds(boroughs.getBounds());
    $(".navbar-collapse.in").collapse("hide");
    return false;
  });
  
  $("#legend-btn").click(function() {
    $("#legendModal").modal("show");
    $(".navbar-collapse.in").collapse("hide");
    return false;
  });
  
  $("#login-btn").click(function() {
    $("#loginModal").modal("show");
    $(".navbar-collapse.in").collapse("hide");
    return false;
  });
  
  $("#list-btn").click(function() {
    $('#sidebar').toggle();
    map.invalidateSize();
    return false;
  });
  
  $("#nav-btn").click(function() {
    $(".navbar-collapse").collapse("toggle");
    return false;
  });
  
  $("#sidebar-toggle-btn").click(function() {
    $("#sidebar").toggle();
    map.invalidateSize();
    return false;
  });
  
  $("#sidebar-hide-btn").click(function() {
    $('#sidebar').hide();
    map.invalidateSize();
  });
  
  function sizeLayerControl() {
    $(".leaflet-control-layers").css("max-height", $("#map").height() - 50);
  }
  
  function clearHighlight() {
    highlight.clearLayers();
  }
  
  function sidebarClick(id) {
    var layer = markerClusters.getLayer(id);
    map.setView([layer.getLatLng().lat, layer.getLatLng().lng], 17);
    layer.fire("click");
    /* Hide sidebar and go to the map on small screens */
    if (document.body.clientWidth <= 767) {
      $("#sidebar").hide();
      map.invalidateSize();
    }
  }
  
  function syncSidebar() {
    /* Empty sidebar features */
    $("#feature-list tbody").empty();
    /* Loop through hotmeals layer and add only features which are in the map bounds */
    hotmeals.eachLayer(function (layer) {
      if (map.hasLayer(hotmealLayer)) {
        if (map.getBounds().contains(layer.getLatLng())) {
          $("#feature-list tbody").append('<tr class="feature-row" id="' + L.stamp(layer) + '" lat="' + layer.getLatLng().lat + '" lng="' + layer.getLatLng().lng + '"><td style="vertical-align: middle;"><img width="16" height="18" src="assets/img/hotmeal.png"></td><td class="feature-name">' + layer.feature.properties.name + '</td><td style="vertical-align: middle;"><i class="fa fa-chevron-right pull-right"></i></td></tr>');
        }
      }
    });
    /* Loop through pantries layer and add only features which are in the map bounds */
    pantries.eachLayer(function (layer) {
      if (map.hasLayer(pantryLayer)) {
        if (map.getBounds().contains(layer.getLatLng())) {
          $("#feature-list tbody").append('<tr class="feature-row" id="' + L.stamp(layer) + '" lat="' + layer.getLatLng().lat + '" lng="' + layer.getLatLng().lng + '"><td style="vertical-align: middle;"><img width="16" height="18" src="assets/img/pantry.png"></td><td class="feature-name">' + layer.feature.properties.name + '</td><td style="vertical-align: middle;"><i class="fa fa-chevron-right pull-right"></i></td></tr>');
        }
      }
    });
    /* Update list.js featureList */
    featureList = new List("features", {
      valueNames: ["feature-name"]
    });
    featureList.sort("feature-name", {
      order: "asc"
    });
  }
  
  /* Basemap Layers */
  var mapquestOSM = L.tileLayer("http://{s}.tiles.mapbox.com/v3/skorasaurus.jjechb76/{z}/{x}/{y}.png", {
    maxZoom: 19,
    attribution: 'Tiles courtesy of <a href="http://www.mapbox.com/" target="_blank">Mapbox</a> Map data (c) <a href="http://www.openstreetmap.org/" target="_blank">OpenStreetMap</a> contributors, ODBL'
  });
  var mapquestOAM = L.tileLayer("http://{s}.mqcdn.com/tiles/1.0.0/sat/{z}/{x}/{y}.jpg", {
    maxZoom: 18,
    subdomains: ["oatile1", "oatile2", "oatile3", "oatile4"],
    attribution: 'Tiles courtesy of <a href="http://www.mapquest.com/" target="_blank">MapQuest</a>. Portions Courtesy NASA/JPL-Caltech and U.S. Depart. of Agriculture, Farm Service Agency'
  });
  var mapquestHYB = L.layerGroup([L.tileLayer("http://{s}.mqcdn.com/tiles/1.0.0/sat/{z}/{x}/{y}.jpg", {
    maxZoom: 18,
    subdomains: ["oatile1", "oatile2", "oatile3", "oatile4"]
  }), L.tileLayer("http://{s}.mqcdn.com/tiles/1.0.0/hyb/{z}/{x}/{y}.png", {
    maxZoom: 19,
    subdomains: ["oatile1", "oatile2", "oatile3", "oatile4"],
    attribution: 'Labels courtesy of <a href="http://www.mapquest.com/" target="_blank">MapQuest</a> <img src="http://developer.mapquest.com/content/osm/mq_logo.png">. Map data (c) <a href="http://www.openstreetmap.org/" target="_blank">OpenStreetMap</a> contributors, ODBL. Portions Courtesy NASA/JPL-Caltech and U.S. Depart. of Agriculture, Farm Service Agency'
  })]);
  
  /* Overlay Layers */
  var highlight = L.geoJson(null);
  var highlightStyle = {
    stroke: false,
    fillColor: "#00FFFF",
    fillOpacity: 0.7,
    radius: 10
  };
  
  
  /* Single marker cluster layer to hold all clusters */
  var markerClusters = new L.MarkerClusterGroup({
    spiderfyOnMaxZoom: true,
    showCoverageOnHover: false,
    zoomToBoundsOnClick: true,
    disableClusteringAtZoom: 16
  });
  
  /* Empty layer placeholder to add to layer control for listening when to add/remove hotmeals to markerClusters layer */
  var hotmealLayer = L.geoJson(null);
  var hotmeals = L.geoJson(null, {
    pointToLayer: function (feature, latlng) {
      return L.marker(latlng, {
        icon: L.icon({
          iconUrl: "assets/img/hotmeal.png",
          iconSize: [32, 32],
          iconAnchor: [12, 28],
          popupAnchor: [0, -25]
        }),
        title: feature.properties.name,
        riseOnHover: true
      });
    },
    filter: function(feature, layer) {
      return feature.properties.kind == "Hot Meal";
    }, 
    onEachFeature: function (feature, layer) {
      if (feature.properties) {
        var content = "<table class='table table-striped table-bordered table-condensed'>" + "<tr><th>Name</th><td>" + feature.properties.name + "</td></tr>" + "<tr><th>Phone</th><td>" + feature.properties.call + "</td></tr>" + "<tr><th>Address</th><td>" + feature.properties.address + "&nbsp;&nbsp;" + feature.properties.zip + "</td></tr>" + "<tr><th>When</th><td>" + feature.properties.when + "</td></tr>" + "<tr><th>Eligible for</th><td>" + feature.properties.forwhom + "</td></tr>" + "<tr><th>Website</th><td><a class='url-break' href='" + feature.properties.URL + "' target='_blank'>" + feature.properties.URL + "</a></td></tr>" + "<table>";
        layer.on({
          click: function (e) {
            $("#feature-title").html(feature.properties.name);
            $("#feature-info").html(content);
            $("#featureModal").modal("show");
            highlight.clearLayers().addLayer(L.circleMarker([feature.geometry.coordinates[1], feature.geometry.coordinates[0]], highlightStyle));
          }
        });
        $("#feature-list tbody").append('<tr class="feature-row" id="' + L.stamp(layer) + '" lat="' + layer.getLatLng().lat + '" lng="' + layer.getLatLng().lng + '"><td style="vertical-align: middle;"><img width="16" height="18" src="assets/img/hotmeal.png"></td><td class="feature-name">' + layer.feature.properties.name + '</td><td style="vertical-align: middle;"><i class="fa fa-chevron-right pull-right"></i></td></tr>');
        hotmealSearch.push({
          name: layer.feature.properties.name,
          address: layer.feature.properties.address,
          source: "Hot Meals",
          id: L.stamp(layer),
          lat: layer.feature.geometry.coordinates[1],
          lng: layer.feature.geometry.coordinates[0]
        });
      }
    }
  });
  $.getJSON("places.geojson", function (data) {
    hotmeals.addData(data);
    map.addLayer(hotmealLayer);
  });
  
  /* Empty layer placeholder to add to layer control for listening when to add/remove pantries to markerClusters layer */
  var pantryLayer = L.geoJson(null);
  var pantries = L.geoJson(null, {
    pointToLayer: function (feature, latlng) {
      return L.marker(latlng, {
        icon: L.icon({
          iconUrl: "assets/img/pantry.png",
          iconSize: [32, 32],
          iconAnchor: [12, 28],
          popupAnchor: [0, -25]
        }),
        title: feature.properties.name,
        riseOnHover: true
      });
    },
    filter: function(feature, layer) {
      return feature.properties.kind == "Pantry";
    }, 
    onEachFeature: function (feature, layer) {
      if (feature.properties) {
        var content = "<table class='table table-striped table-bordered table-condensed'>" + "<tr><th>Name</th><td>" + feature.properties.name + "</td></tr>" + "<tr><th>Phone</th><td>" + feature.properties.call + "</td></tr>" + "<tr><th>Address</th><td>" + feature.properties.address + "&nbsp;&nbsp;" + feature.properties.zip + "</td></tr>" + "<tr><th>When</th><td>" + feature.properties.when + "</td></tr>" + "<tr><th>Eligible for</th><td>" + feature.properties.forwhom + "</td></tr>" + "<tr><th>Website</th><td><a class='url-break' href='" + feature.properties.URL + "' target='_blank'>" + feature.properties.URL + "</a></td></tr>" + "<table>";
        layer.on({
          click: function (e) {
            $("#feature-title").html(feature.properties.name);
            $("#feature-info").html(content);
            $("#featureModal").modal("show");
            highlight.clearLayers().addLayer(L.circleMarker([feature.geometry.coordinates[1], feature.geometry.coordinates[0]], highlightStyle));
          }
        });
        $("#feature-list tbody").append('<tr class="feature-row" id="' + L.stamp(layer) + '" lat="' + layer.getLatLng().lat + '" lng="' + layer.getLatLng().lng + '"><td style="vertical-align: middle;"><img width="16" height="18" src="assets/img/pantry.png"></td><td class="feature-name">' + layer.feature.properties.name + '</td><td style="vertical-align: middle;"><i class="fa fa-chevron-right pull-right"></i></td></tr>');
        pantrySearch.push({
          name: layer.feature.properties.name,
          address: layer.feature.properties.address,
          source: "Pantries",
          id: L.stamp(layer),
          lat: layer.feature.geometry.coordinates[1],
          lng: layer.feature.geometry.coordinates[0]
        });
      }
    }
  });
  $.getJSON("places.geojson", function (data) { 
    pantries.addData(data);
    map.addLayer(pantryLayer);
  });
  
  map = L.map("map", {
    zoom: 11,
    center: [41.501860,-81.635799],
    layers: [mapquestOSM, markerClusters, highlight],
    zoomControl: false,
    attributionControl: false
  });
  
  /* Layer control listeners that allow for a single markerClusters layer */
  map.on("overlayadd", function(e) {
    if (e.layer === hotmealLayer) {
      markerClusters.addLayer(hotmeals);
      syncSidebar();
    }
    if (e.layer === pantryLayer) {
      markerClusters.addLayer(pantries);
      syncSidebar();
    }
  });
  
  map.on("overlayremove", function(e) {
    if (e.layer === hotmealLayer) {
      markerClusters.removeLayer(hotmeals);
      syncSidebar();
    }
    if (e.layer === pantryLayer) {
      markerClusters.removeLayer(pantries);
      syncSidebar();
    }
  });
  
  /* Filter sidebar feature list to only show features in current map bounds */
  map.on("moveend", function (e) {
    syncSidebar();
  });
  
  /* Clear feature highlight when map is clicked */
  map.on("click", function(e) {
    highlight.clearLayers();
  });
  
  /* Attribution control */
  function updateAttribution(e) {
    $.each(map._layers, function(index, layer) {
      if (layer.getAttribution) {
        $("#attribution").html((layer.getAttribution()));
      }
    });
  }
  map.on("layeradd", updateAttribution);
  map.on("layerremove", updateAttribution);
  
  var attributionControl = L.control({
    position: "bottomright"
  });
  attributionControl.onAdd = function (map) {
    var div = L.DomUtil.create("div", "leaflet-control-attribution");
    div.innerHTML = "<span class='hidden-xs'></span><a href='#' onclick='$(\"#attributionModal\").modal(\"show\"); return false;'>Attribution</a>";
    return div;
  };
  map.addControl(attributionControl);
  
  var zoomControl = L.control.zoom({
    position: "bottomright"
  }).addTo(map);
  
  /* GPS enabled geolocation control set to follow the user's location */
  var locateControl = L.control.locate({
    position: "bottomright",
    drawCircle: true,
    follow: true,
    setView: true,
    keepCurrentZoomLevel: true,
    markerStyle: {
      weight: 1,
      opacity: 0.8,
      fillOpacity: 0.8
    },
    circleStyle: {
      weight: 1,
      clickable: false
    },
    icon: "icon-direction",
    metric: false,
    strings: {
      title: "My location",
      popup: "You are within {distance} {unit} from this point",
      outsideMapBoundsMsg: "You seem located outside the boundaries of the map"
    },
    locateOptions: {
      maxZoom: 18,
      watch: true,
      enableHighAccuracy: true,
      maximumAge: 10000,
      timeout: 10000
    }
  }).addTo(map);
  
  /* Larger screens get expanded layer control and visible sidebar */
  if (document.body.clientWidth <= 767) {
    var isCollapsed = true;
  } else {
    var isCollapsed = false;
  }
  
  var baseLayers = {
    "Street Map": mapquestOSM,
    "Aerial Imagery": mapquestOAM,
    "Imagery with Streets": mapquestHYB
  };
  
  var groupedOverlays = {
    "Points of Interest": {
      "<img src='assets/img/hotmeal.png' width='32' height='32'>&nbsp;Hot Meals": hotmealLayer,
      "<img src='assets/img/pantry.png' width='32' height='32'>&nbsp;Pantries": pantryLayer
    }
  };
  
  var layerControl = L.control.groupedLayers(baseLayers, groupedOverlays, {
    collapsed: isCollapsed
  }).addTo(map);
  
  /* Highlight search box text on click */
  $("#searchbox").click(function () {
    $(this).select();
  });
  
  /* Prevent hitting enter from refreshing the page */
  $("#searchbox").keypress(function (e) {
    if (e.which == 13) {
      e.preventDefault();
    }
  });
  
  $("#featureModal").on("hidden.bs.modal", function (e) {
    $(document).on("mouseout", ".feature-row", clearHighlight);
  });
  
  /* Typeahead search functionality */
  $(document).one("ajaxStop", function () {
    $("#loading").hide();
    sizeLayerControl();
    featureList = new List("features", {valueNames: ["feature-name"]});
    featureList.sort("feature-name", {order:"asc"});
  
    var hotmealsBH = new Bloodhound({
      name: "Hot Meals",
      datumTokenizer: function (d) {
        return Bloodhound.tokenizers.whitespace(d.name);
      },
      queryTokenizer: Bloodhound.tokenizers.whitespace,
      local: hotmealSearch,
      limit: 10
    });
  
    var pantriesBH = new Bloodhound({
      name: "Pantries",
      datumTokenizer: function (d) {
        return Bloodhound.tokenizers.whitespace(d.name);
      },
      queryTokenizer: Bloodhound.tokenizers.whitespace,
      local: pantrySearch,
      limit: 10
    });
  
    hotmealsBH.initialize();
    pantriesBH.initialize();
  
    /* instantiate the typeahead UI */
    $("#searchbox").typeahead({
      minLength: 3,
      highlight: true,
      hint: false
    }, {
      name: "Pantries",
      displayKey: "name",
      source: pantriesBH.ttAdapter(),
      templates: {
        header: "<h4 class='typeahead-header'><img src='assets/img/pantry.png' width='24' height='28'>&nbsp;Hot Meals</h4>",
        suggestion: Handlebars.compile(["{{name}}<br>&nbsp;<small>{{address}}</small>"].join(""))
      }
    }).on("typeahead:selected", function (obj, datum) {
      if (datum.source === "Hot Meals") {
        if (!map.hasLayer(hotmealLayer)) {
          map.addLayer(hotmealLayer);
        }
        map.setView([datum.lat, datum.lng], 17);
        if (map._layers[datum.id]) {
          map._layers[datum.id].fire("click");
        }
      }
      if (datum.source === "Pantries") {
        if (!map.hasLayer(pantryLayer)) {
          map.addLayer(pantryLayer);
        }
        map.setView([datum.lat, datum.lng], 17);
        if (map._layers[datum.id]) {
          map._layers[datum.id].fire("click");
        }
      }
      if ($(".navbar-collapse").height() > 50) {
        $(".navbar-collapse").collapse("hide");
      }
    }).on("typeahead:opened", function () {
      $(".navbar-collapse.in").css("max-height", $(document).height() - $(".navbar-header").height());
      $(".navbar-collapse.in").css("height", $(document).height() - $(".navbar-header").height());
    }).on("typeahead:closed", function () {
      $(".navbar-collapse.in").css("max-height", "");
      $(".navbar-collapse.in").css("height", "");
    });
    $(".twitter-typeahead").css("position", "static");
    $(".twitter-typeahead").css("display", "block");
  }); 
  
  // Leaflet patch to make layer control scrollable on touch browsers
  var container = $(".leaflet-control-layers")[0];
  if (!L.Browser.touch) {
    L.DomEvent
    .disableClickPropagation(container)
    .disableScrollPropagation(container);
  } else {
    L.DomEvent.disableClickPropagation(container);
  } 
  
  // mapzen-leaflet geocoder configuration 
  // for more information; see https://github.com/mapzen/leaflet-geocoder
  
  var myOptions = {
    expanded: true, 
    position: 'topright',
    placeholder: 'Type your address here', 
    autocomplete: true,
    focus: true, 
    markers:  {icon: L.icon({
              iconUrl: "assets/img/star.png",
              iconSize: [32, 32],
              iconAnchor: [12, 28],
              popupAnchor: [0, -25]
              }),
  }
  };
  
  var myGeocoder = L.control.geocoder('mapzen-c3U9gXv', myOptions).addTo(map);
  
  myGeocoder.on('select', function (e) {
    console.log('you’ve selected ' + e.feature.properties.label + ' located at ' + e.latlng.lat + ', ' + e.latlng.lng);
  map.setZoom(16); 
  });
  
  
  