$(function() {

    var start = moment({hour: 0});
    var end = moment();
    var s_start = start.utc().format('YYYY-MM-DDTHH:mm:ss[Z]');
    var s_end = end.utc().format('YYYY-MM-DDTHH:mm:ss[Z]');

    //console.log("XXX: "+start.format());

    function change_property(new_property) {
        if (new_property != property) {
            property = new_property;
            
            if (history.pushState) {
                var newurl = window.location.href.replace(/property=[a-z]+/,'property='+new_property);
                window.history.pushState({path:newurl},'',newurl);
            }
        }
    }

    function cb(start, end) {
        $('#reportrange span').html(start.locale('de').format('D. MMM YYYY') + ' - ' + end.locale('de').format('D. MMM YYYY'));
        //console.log("A new date selection was made: " + start.format() + ' to ' + end.format());
        s_start = start.utc().format('YYYY-MM-DDTHH:mm:ss[Z]');
        s_end = end.utc().format('YYYY-MM-DDTHH:mm:ss[Z]');
        //console.log("Start: " + s_start + ' End: ' + s_end + ' Property: '+ property);
        drawGraph(deveui, alias, property, s_start,s_end, minmax, false);
    }

    $('#btn_w').on("click", function() {
      change_property('w');
      drawGraph(deveui, alias, property, s_start, s_end, minmax, false);
    });

    $('#btn_t').on("click", function() {
      change_property('t');
      drawGraph(deveui, alias, property, s_start, s_end, minmax, false);
    });

    $('#btn_h').on("click", function() {
      change_property('h');
      drawGraph(deveui, alias, property, s_start, s_end, minmax, false);
    });

    $('#btn_p').on("click", function() {
      change_property('p');
      drawGraph(deveui, alias, property, s_start, s_end, minmax, false);
    });

    $('#btn_vp').on("click", function() {
      change_property('vp');
      drawGraph(deveui, alias, property, s_start, s_end, minmax, false);
    });

    $('#reportrange').daterangepicker({
        startDate: start.local(),
        endDate: end.local(),
        regional: [ "de" ],
        "locale": {
            format: 'DD.MM.YYYY',
            "separator": " - ",
            "applyLabel": "Anwenden",
            "cancelLabel": "Abbrechen",
            "fromLabel": "Von",
            "toLabel": "Bis",
            customRangeLabel: 'Benutzerdefiniert',
            "weekLabel": "W",
            "daysOfWeek": [
                "So",
                "Mo",
                "Di",
                "Mi",
                "Do",
                "Fr",
                "Sa"
            ],
            "monthNames": [
                "Jan",
                "Feb",
                "M&auml;r",
                "Apr",
                "Mai",
                "Jun",
                "Jul",
                "Aug",
                "Sep",
                "Okt",
                "Nov",
                "Dez"
            ],
            "firstDay": 1
        },
        ranges: {
           'Heute': [moment(), moment()],
           'Gestern': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
           'Letzte 7 Tage': [moment().subtract(6, 'days'), moment()],
           'Letzte 30 Tage': [moment().subtract(29, 'days'), moment()],
           'Diesen Monat': [moment().startOf('month'), moment().endOf('month')],
           'Letzten Monat': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
        }
    }, cb);

    //cb(start, end);

});
