﻿var dateFilterChanged;
var currentLeft;
var currentRight;
var changedFunction;

function initSlider(onChangedFunction) {
    changedFunction = onChangedFunction;
    dateFilterChanged = false;
    var today = new Date();
    var timestampLeft = today.getTime();
    timestampLeft = timestampLeft - 365 * 24 * 3600000;
    var timestampRight = today.getTime() + 24 * 3600000;
    currentRight = timestampRight;
    currentLeft = timestampLeft;

    var date1, date2, date1Formatted, date2Formatted;

    $("#slider-range").slider({
        range: true,
        min: timestampLeft,
        max: timestampRight,
        orientation: 'horizontal',
        values: [currentLeft, currentRight],

        slide: function(event, ui) {
            date1 = new Date(ui.values[0]);
            date2 = new Date(ui.values[1]);
            currentLeft = date1.getTime();
            currentRight = date2.getTime();
            date1Formatted = dateFormat(date1, "dS mmmm, yyyy");
            date2Formatted = dateFormat(date2, "dS mmmm, yyyy");
            $("#date_from").html(date1Formatted);
            $("#date_to").html(date2Formatted);
            dateFilterChanged = true;
        }
    });
    updateSliderDates(timestampLeft, timestampRight);
    $("#date_from").click(function() {
        selectDate("#date_from", currentLeft, function(dateText, inst) {
            currentLeft = new Date(inst.selectedYear, inst.selectedMonth, inst.selectedDay).getTime();
            updateSliderDates(currentLeft, currentRight);
            changedFunction(currentLeft, currentRight);
        });
    });
    $("#date_to").click(function() {
        selectDate("#date_to", currentRight, function(dateText, inst) {
            currentRight = new Date(inst.selectedYear, inst.selectedMonth, inst.selectedDay).getTime();
            updateSliderDates(currentLeft, currentRight);
            changedFunction(currentLeft, currentRight);
        });
    });
}

var lastSelectDate = '';
var selectDate = function(idSelect, defDate, callback) {
    if (lastSelectDate == idSelect) { return; }
    lastSelectDate = idSelect;
    var oldHtml = $(idSelect).html();
    $(idSelect).html('<input type="text" id="selectDate" class="input_text" style="width:100px; margin-top:-3px; margin-left:-7px;"  value="' + $(idSelect).html() + '" />');
    $("#selectDate").datepicker({
            'defaultDate': new Date(defDate),
        //        'onSelect': function(p1, p2) { lastSelectDate = ''; callback(p1, p2) },
            'onClose': function(p1, p2) { lastSelectDate = ''; $(idSelect).html(oldHtml); callback(p1, p2) }
        });
    $("#selectDate").datepicker('show');
};


var updateSliderDates = function(timestampLeft, timestampRight) {
    date1 = new Date(timestampLeft);
    date2 = new Date(timestampRight);
    date1Formatted = dateFormat(date1, "dS mmmm, yyyy");
    date2Formatted = dateFormat(date2, "dS mmmm, yyyy");
    $("#date_from").html(date1Formatted);
    $("#date_to").html(date2Formatted);
    $('#date_filter_clk').click(onDateChanged);
};

function getFromDate() {
    return currentLeft;
}
function getToDate() {
    return currentRight;
}
function onDateChanged() {
    $('#filter').toggle();
    if ($('#filter').css("display") == "none") {
        if (dateFilterChanged) {
            dateFilterChanged = false;
            changedFunction(currentLeft, currentRight);
        }
    }
}
