var csoLatLongs = {};
var csoEvents = [];

const CUMULATIVE = "cumulative"; 

function readCsoPoints() {
    $.ajax({
        url: "/data/outfall_points.csv",
        async: false,
        dataType: "text",
        success: function (csvd) {
            var csoPoints = $.csv.toObjects(csvd);
            for (var i = 0; i < csoPoints.length; i++) {
                var csoPoint = csoPoints[i];
                csoLatLongs[csoPoint.outfall_name] = csoPoint;
            }
            drawMap(CUMULATIVE);
        },
        error: function (xhr, textStatus, errorThrown) {
            alert('Failed to retrieve lat/longs: ' + textStatus);
        }
    })
}

function readCsoEvents(csoPoints) {
    $.ajax({
        url: "/data/merged_cso_with_durations.csv",
        async: false,
        dataType: "text",
        success: function (csvd) {
            csoEvents = $.csv.toObjects(csvd);
            drawMap(CUMULATIVE);
        },
        error: function (xhr, textStatus, errorThrown) {
            alert('Failed to retrieve cso events: ' + textStatus);
        }

    })
}

function drawMap(mapType) {
    if ((jQuery.isEmptyObject(csoLatLongs)) || (csoEvents.length == 0)) {
        return;
    }
    
    if (mapType == CUMULATIVE) {
        drawCumulative(csoEvents);
    }
}

// When the dates are changed, update the values
$("#slider").bind("valuesChanged", function(e, data){
    var modifiedCsoEvents = [];
    var a = Object.prototype.toString.call(data.values.min);
    var startDate = data.values.min;
    var endDate = data.values.max;
    for (var i = 0; i < csoEvents.length; i++) {
        var csoEvent = csoEvents[i];
        var csoStart = new Date(csoEvent['Open date/time']);
        var csoEnd = new Date(csoEvent['Close date/time']);
        if ((startDate <= csoStart) && (endDate >= csoEnd)) {
            modifiedCsoEvents.push(csoEvent);
        }
        
    }
    drawCumulative(modifiedCsoEvents);
});
    
    
function drawCumulative(argCsoEvents) {

    // Find cumulative for outfall points
    var cumulative = {};
    for (var i = 0; i < argCsoEvents.length; i++) {
        var csoEvent = argCsoEvents[i];
        if (csoEvent['Outfall Structure'] in csoLatLongs) {
            if (!(csoEvent['Outfall Structure'] in cumulative)) {
                cumulative[csoEvent['Outfall Structure']] = 0.0;
            }
            cumulative[csoEvent['Outfall Structure']] += parseFloat(csoEvent['Duration_mins']);
        }
    }
    
    var data = dictToSortedList(cumulative);
    
    for (var i = 0; i < data.length; i++) {

        var color = 'blue';
        var size = 200;
        if (i < 10) {
            color = 'red';
            size = 500;
        }
        
        var value = data[i];
        var point = csoLatLongs[value.outfall_name];

        var circle = L.circle([point.lat, point.lon], size, {
            color: color,
            fillColor: color,
            fillOpacity: 1
        }).addTo(mymap);
        
        circle.bindPopup(value.outfall_name + "<br>Cum. dump: " + value.value + " mins.");
        
    }
}

function dictToSortedList(dict) {
    outfalls_by_num = {}
    values = [];
    $.each( dict, function( key, value ) {
        outfalls_by_num[value] = key;
        values.push(parseFloat(value));
    });

    values.sort(function(a,b) { return b - a;});
    retVal = [];
    for (i = 0; i < values.length; i++) {
        retVal.push({outfall_name: outfalls_by_num[values[i]], value: values[i]});
    }
    return retVal;
}
@@ -34,16 +34,34 @@
 ts = %5B%5D;
+%0Avar markers = %5B%5D;
 %0A%0Aconst 
@@ -505,32 +505,43 @@
 awMap(CUMULATIVE
+, csoEvents
 );%0A        %7D,%0A  
@@ -942,16 +942,27 @@
 MULATIVE
+, csoEvents
 );%0A     
@@ -1136,16 +1136,30 @@
 (mapType
+, argCsoEvents
 ) %7B%0A    
@@ -1311,17 +1311,20 @@
 ulative(
-c
+argC
 soEvents
@@ -1951,27 +1951,32 @@
     draw
-Cumulative(
+Map(CUMULATIVE, 
 modified
@@ -2568,24 +2568,39 @@
 ative);%0A    
+clearMarkers();
 %0A    for (va
@@ -3130,16 +3130,46 @@
 ins.%22);%0A
+        markers.push(circle);%0A
         
@@ -3577,24 +3577,24 @@
 i%5D%7D);%0A    %7D%0A
-
     return r
@@ -3601,8 +3601,147 @@
 etVal;%0A%7D
+%0A%0Afunction clearMarkers() %7B%0A    for (var i = 0; i %3C markers.length; i++) %7B%0A        mymap.removeLayer(markers%5Bi%5D);%0A    %7D%0A    markers = %5B%5D;%0A%7D

Reset map when dates are changed