var oneDayInMs = 24 * 60 * 60 * 1000;

var main = function() {
    var datasetKey = getDatasetKeyFromURL();
    downloadChart(datasetKey,30);
};

var removeTimeFromDate = function(date) {
    return date.setUTCHours(0,0,0,0); // Floor date to midnight and return as time integer
};

var formatAsISODate = function(time) {
    var date = new Date(time);
    return date.toISOString().substring(0,10); // Return first 10 characters (yyyy-mm-dd) of full ISO date
};

var downloadChart = function(datasetKey, dayRange) {
    var today = new Date();
    var startDay = removeTimeFromDate(today); // Set at midnight
        startDay = startDay - ((dayRange - 1) * oneDayInMs); // Substract dayRange in milliseconds
    var days = [];
    var downloads = [];
    for (var i = 0; i < dayRange; i++) {
        days[i] = formatAsISODate(startDay + i * oneDayInMs); // Populate array with all dates in ISO8601
        downloads[i] = null; // Populate array with null values (for empty chart)
    }

    // Update title
    var anchor = d3.select('#content .results .content');
    var currentTitle = anchor.select('h2');
    currentTitle.text(currentTitle.text() + ' in total');

    // Add title and div for chart to DOM
    anchor.insert('div', ':first-child')
        .attr('class','fullwidth')
        .attr('id','downloadChart');
    anchor.insert('div', ':first-child')
        .attr('class', 'header')
        .append('div')
            .attr('class', 'left')
            .append('h2')
                .text('Download activity over the last 30 days');

    var downloadChart = c3.generate({
        bindto: '#downloadChart',
        data: {
            x: 'days',
            columns: [
                ['days'].concat(days),
                ['downloads'].concat(downloads)
            ],
            type: 'bar'
        },
        axis: {
            x: {
                type: 'timeseries',
                tick: {
                    format: '%d/%m'
                }
            },
            y: {
                tick: {
                    outer: false
                }
            }
        },
        padding: {
            left: 30 // To make room for y-axis labels on data loading
        },
        bar: {
            width: {
                ratio: 0.9
            }
        },
        legend: {
            show: false
        }
    });

    loadDownloadData(datasetKey, 1200, startDay, downloads, downloadChart);
};

var loadDownloadData = function(datasetKey, pageLimit, startDay, downloads, downloadChart) {
    /*  Note: this function does only one call to the GBIF API, so if dayRange 
        is high and pageLimit low, it might not retrieve all downloads. */
    
    var url = 'http://api.gbif.org/v1/occurrence/download/dataset/' + datasetKey + '?limit=' + pageLimit;
    d3.json(url,function(error, result) {
        if (error) return console.warn(error);

        result.results.every(function(result) {
            var downloadDay = removeTimeFromDate(new Date(result.download.created));
            // console.log(downloadDay);
            
            if (downloadDay >= startDay) {
                if (result.download.status == 'SUCCEEDED') {
                    var i = (downloadDay - startDay) / oneDayInMs; // Day index
                    downloads[i] += 1;
                }
                return true; // Continue looping
            } else {
                return false; // Passed beyond startDay, stop looping. Assumes API returns downloads in reversed chronology.
            }
        });

        downloadChart.load({
            columns: [
                ['downloads'].concat(downloads)
            ]
        });
    });
};

main();

@@ -2380,10 +2380,12 @@
 y, 1
-2
 0
+0, 
 0, s
@@ -2419,16 +2419,98 @@
 dChart);
+ // set pageLimit and offset here. A lower pageLimit will result in more API calls
 %0A%7D;%0A%0Avar
@@ -2560,16 +2560,24 @@
 geLimit,
+ offset,
  startDa
@@ -2871,16 +2871,38 @@
 ageLimit
+ + '&offset=' + offset
 ;%0A    d3
@@ -2981,24 +2981,48 @@
 rn(error);%0A%0A
+        var minI = 30;%0A%0A
         resu
@@ -3156,17 +3156,16 @@
       //
- 
 console.
@@ -3379,16 +3379,108 @@
 y index%0A
+                    if (i %3C minI) %7B%0A                        minI = i;%0A                    %7D%0A
         
@@ -3746,24 +3746,400 @@
        %7D);%0A%0A
+        //console.log(minI);%0A        // Downloads needed until the earliest download event's created date matches the startdate. If this is not the case, launch the same function again%0A        // with an increased offset parameter.%0A        if (minI %3E 0) %7B%0A            loadDownloadData(datasetKey, pageLimit, offset + pageLimit, startDay, downloads, downloadChart);%0A        %7D%0A%0A
         down

fix #10