// Common map functions for Mapbox GL JS

var defaultStyle = {};
var defaultOpacity = {};
var defaultFilter = {};

// Highlight a layer collection
function mapHighlight(item) {

  var collectionName = $(item).attr('data-map-layer');
  var color = $(item).attr('data-map-layer-highlight');

  // Loop through collection and store defaults before changing them
  for (var i = 0; i < mapLayerCollection[collectionName].length; i++) {

    var obj = mapLayerCollection[collectionName][i];

    // Choose an appropriate property to change
    if (map.getLayer(obj).type == 'raster')
      prop = 'raster-opacity';
    if (map.getLayer(obj).type == 'fill')
      prop = 'fill-color';
    if (map.getLayer(obj).type == 'line')
      prop = 'line-color';
    if (map.getLayer(obj).type == 'circle')
      prop = 'circle-color';

    // Backup the value before changing it
    var propObj = {};
    propObj[prop] = map.getPaintProperty(obj, prop);

    defaultStyle[obj] = propObj;
    map.setPaintProperty(obj, prop, color);

  }

};

// Reset style of a collection to default
function mapHighlightReset() {

  for (var collectionName in mapLayerCollection) {
    // Loop through collection and and reset properties to stored defaults
    for (var i = 0; i < mapLayerCollection[collectionName].length; i++) {

      var obj = mapLayerCollection[collectionName][i];
      var prop;

      // Choose an appropriate property to change
      if (map.getLayer(obj).type == 'fill')
        prop = 'fill-color';
      if (map.getLayer(obj).type == 'line')
        prop = 'line-color';
      if (map.getLayer(obj).type == 'circle')
        prop = 'circle-color';

      // Revert to default style if known
      try {
        if (defaultStyle[obj][prop])
          map.setPaintProperty(obj, prop, defaultStyle[obj][prop]);
      } catch (e) {}

    }
  }

};

// Toggle visibility of a layer collection using opacity
function mapToggle(item) {

  var collectionName = $(item).attr('data-map-layer');

  // Loop through collection and toggle visibility
  for (var i = 0; i < mapLayerCollection[collectionName].length; i++) {

    var obj = mapLayerCollection[collectionName][i];
    var prop;

    // Choose an appropriate property to change
    if (map.getLayer(obj).type == 'raster')
      prop = 'raster-opacity';
    if (map.getLayer(obj).type == 'fill')
      prop = 'fill-opacity';
    if (map.getLayer(obj).type == 'line')
      prop = 'line-opacity';
    if (map.getLayer(obj).type == 'circle')
      prop = 'circle-opacity';

    // Apply original opacity if existing is 0, else make a backup and set to 0
    if (!map.getPaintProperty(obj, prop)) {
      try {
        map.setPaintProperty(obj, prop, defaultOpacity[obj][prop]);
      } catch (e) {
        // The original must have been 0
      }
    } else {
      var propObj = {};
      propObj[prop] = map.getPaintProperty(obj, prop);

      defaultOpacity[obj] = propObj;
      map.setPaintProperty(obj, prop, 0);
    }
  }

};

// Change the filter values of set of layers
function mapFilter(item){
  var layer = $(item).data('map-layer');
  var filter = $(item).data('map-layer-filter');
  map.setFilter(layer, filter);
}

//Location functions
// Set pitch and fly to location
function mapLocate(location) {
  map.setPitch(mapLocation[location].pitch);
  map.flyTo(mapLocation[location]);
  if (location == "reset") {}
}

@@ -198,35 +198,30 @@
 e = $(item).
+d
 at
-tr('data-
+a('
 map-layer');
@@ -239,35 +239,30 @@
 r = $(item).
+d
 at
-tr('data-
+a('
 map-layer-hi
@@ -1947,19 +1947,14 @@
 em).
+d
 at
-tr('data-
+a('
 map-

Use $.data method