/* ---------------
 * Create map with baselayer
 * ---------------
 */

function drawMap() {
    window.map;
    var MAPTYPE_ID = 'custom_style';
    var mapStyle = [
	{ "featureType": "water", "elementType": "geometry", "stylers": [ { "color": "#000000" }, { "lightness": 17 } ] },
	{ "featureType": "landscape", "elementType": "geometry", "stylers": [ { "color": "#000000" }, { "lightness": 20 } ] },
	{ "featureType": "road.highway", "elementType": "geometry.fill", "stylers": [ { "color": "#000000" }, { "lightness": 17 } ] },
	{ "featureType": "road.highway", "elementType": "geometry.stroke", "stylers": [ { "color": "#000000" }, { "lightness": 29 }, { "weight": 0.2 } ] },
	{ "featureType": "road.arterial", "elementType": "geometry", "stylers": [ { "color": "#000000" }, { "lightness": 18 } ] },
	{ "featureType": "road.local", "elementType": "geometry", "stylers": [ { "color": "#000000" }, { "lightness": 16 } ] },
	{ "featureType": "poi", "elementType": "geometry", "stylers": [ { "color": "#000000" }, { "lightness": 21 } ] },
	{ "elementType": "labels.text.stroke", "stylers": [ { "visibility": "on" }, { "color": "#000000" }, { "lightness": 16 } ] },
	{ "mapType": "Map", "elementType": "labels.text.fill", "stylers": [ { "saturation": 36 }, { "color": "#000000" }, { "lightness": 40 } ] },
	{ "elementType": "labels.icon", "stylers": [ { "visibility": "off" } ] },
	{ "featureType": "transit", "elementType": "geometry", "stylers": [ { "color": "#000000" }, { "lightness": 19 } ] },
	{ "featureType": "administrative", "elementType": "geometry.fill", "stylers": [ { "color": "#000000" }, { "lightness": 20 } ] },
	{ "featureType": "administrative", "elementType": "geometry.stroke", "stylers": [ { "color": "#000000" }, { "lightness": 17 }, { "weight": 1.2 } ] }
    ];
    var featureOpts = mapStyle;
    var mapOptions = {
	zoom: 7,
	center: new google.maps.LatLng(50.4, 4),
	mapTypeControlOptions: {
	    mapTypeIds: [google.maps.MapTypeId.ROADMAP, google.maps.MapTypeId.HYBRID, MAPTYPE_ID]
	},
	mapTypeId: MAPTYPE_ID
    };

    window.map = new google.maps.Map(document.getElementById('map'),  mapOptions);
    var styledMapOptions = {
	name: 'Custom Style'
    };
    var customMapType = new google.maps.StyledMapType(featureOpts, styledMapOptions);
    window.map.mapTypes.set(MAPTYPE_ID, customMapType);
    showBlackoutData(window.map);
};


/* ---------------
 * Add layer with blackout data
 * ---------------
 */
function showBlackoutData(map) {
    var sql = "SELECT * FROM public.municipalities_blackout_pct";
    cartodb.createLayer(map, {
	user_name: 'datafable',
	type: 'cartodb',
	sublayers: [{
	    sql: sql,
	    cartocss: '#municipalities_blackout_pct { [excluded_pct > 90] {polygon-fill: #F0F0F0;} [excluded_pct < 90] {polygon-fill: #F0F0F0;} [excluded_pct < 80] {polygon-fill: #F0F000;} }'
	}]
    })
    .addTo(map)
    .done(function(layer) {
        window.mapLayer = layer;
    });
};

// Update cartocss based on selected section
function showBySection(sectionnr) {
    var section = 'section_' + sectionnr;
    var cartocss = vsprintf('#municipalities_blackout_pct { [ %s_pct < 90] {polygon-fill: #FF0000; } [ %s_pct < 80] { polygon-fill: #00FF00} }', [section, section]);
    window.mapLayer.setCartoCSS(cartocss);
}


// jQuery bindings to controls
function bindControls() {
    $("input:radio[name=section]").change(function() {
        showBySection(this.value);
    });
}

// Add the base map to the page
bindControls();
window.onload = drawMap;

@@ -2502,30 +2502,23 @@
  public.
-municipalities
+rolling
 _blackou
@@ -2518,20 +2518,16 @@
 blackout
-_pct
 %22;%0A    c
@@ -2646,55 +2646,300 @@
 : '#
-municipalities_blackout_pct %7B %5Bexcluded
+rolling_blackout %7B polygon-fill: #1a9850; polygon-opacity: 0.8; line-color: #333333; line-width: 0.5; line-opacity: 1; %5Ball_sections_pct = 100%5D %7Bpolygon-fill: #d73027; %7D %5Ball_sections_pct %3C 100%5D %7Bpolygon-fill: #f79272; %7D %5Ball_sections_pct %3C 80%5D %7Bpolygon-fill: #fed6b0; %7D %5Ball_sections
 _pct 
-%3E 9
+%3C 6
 0%5D %7B
@@ -2957,34 +2957,89 @@
 l: #
-F0F0F0;%7D %5Bexcluded
+fff2cc; %7D %5Ball_sections_pct %3C 40%5D %7Bpolygon-fill: #d2ecb4; %7D %5Ball_sections
 _pct %3C 
-9
+2
 0%5D %7B
@@ -3057,34 +3057,38 @@
 l: #
-F0F0F0;%7D %5Bexcluded
+8cce8a; %7D %5Ball_sections
 _pct 
-%3C 8
+= 
 0%5D %7B
@@ -3106,15 +3106,16 @@
 l: #
-F0F00
+1a985
 0;
+ 
 %7D %7D'
@@ -3372,22 +3372,15 @@
 f('#
-municipalities
+rolling
 _bla
@@ -3384,20 +3384,16 @@
 blackout
-_pct
  %7B %5B %25s_

Update cartodb table name and default cartocss