var braapf_convert_numeric_to_date,
braapf_init_datepicker,
braapf_datepicker_same,
braapf_convert_date_to_numeric,
braapf_grab_single_datepicker;
(function ($){
    braapf_convert_numeric_to_date = function(numeric, format) {
        numeric = numeric+"";
        var year = numeric.substring(0, 4);
        var year2 = numeric.substring(2, 4);
        var month = numeric.substring(4, 6);
        var day = numeric.substring(6, 8);
        var result_date = format+"";
        result_date = result_date.replace('yy', year);
        result_date = result_date.replace('y', year2);
        result_date = result_date.replace('mm', month);
        result_date = result_date.replace('dd', day);
        return result_date;
    }
    function braapf_jqui_getDate( element, format ) {
        var date;
        try {
            date = $.datepicker.parseDate( format, element.value );
        } catch( error ) {
            date = null;
        }
        return date;
    }
    braapf_init_datepicker = function () {
        $(".bapf_datepick:not(.bapf_datepick_ready)").each(function() {
            if( ! jQuery('.bapfdpapcss').length ) {
                jQuery('body').append('<div class="bapfdpapcss '+$(this).data('template')+'"></div>');
            }
            var taxonomy = $(this).data('taxonomy');
            var $mainElement = $(this).find('.bapf_date_all');
            var dateFormat = $mainElement.data('dateformat');
            var minDate = braapf_convert_numeric_to_date($mainElement.data('min'), dateFormat);
            var maxDate = braapf_convert_numeric_to_date($mainElement.data('max'), dateFormat);
            var startDate = braapf_convert_numeric_to_date($mainElement.data('start'), dateFormat);
            var endDate = braapf_convert_numeric_to_date($mainElement.data('end'), dateFormat);
            var datepicker_data = {
                dateFormat: dateFormat,
                minDate: minDate,
                maxDate: maxDate,
                changeMonth: $mainElement.data('changemonth'),
                changeYear: $mainElement.data('changeyear'),
                beforeShow: function(textbox, instance){
                    $('.bapfdpapcss').append($('#ui-datepicker-div'));
                }
            };
            $(this).find('.bapf_date_from input').val(startDate);
            datepicker_data.minDate = minDate;
            datepicker_data.maxDate = endDate;
            datepicker_data = berocket_apply_filters('jqui_datepicker_data', datepicker_data, 'from', $(this).find('.bapf_date_from input'));
            $(this).find('.bapf_date_from input').datepicker(datepicker_data).on( "change", function() {
                $('.bapf_datepick.bapf_datepick_ready[data-taxonomy="'+taxonomy+'"]').find('.bapf_date_to input').datepicker( "option", "minDate", braapf_jqui_getDate( this, dateFormat ) );
                braapf_datepicker_same(taxonomy, [$mainElement.find('.bapf_date_from input').val(), $mainElement.find('.bapf_date_to input').val()]);
                var filter_changed_element = {
                    element:'#'+$(this).closest('.bapf_sfilter').attr('id'),
                    parent: 0,
                    find: '.bapf_body'
                };
                berocket_apply_filters('filter_changed_element', filter_changed_element, $(this));
                berocket_do_action('update_products', 'filter', $(this));
            });
            $(this).find('.bapf_date_to input').val(endDate);
            datepicker_data.minDate = startDate;
            datepicker_data.maxDate = maxDate;
            datepicker_data = berocket_apply_filters('jqui_datepicker_data', datepicker_data, 'to', $(this).find('.bapf_date_to input'));
            $(this).find('.bapf_date_to input').datepicker(datepicker_data).on( "change", function() {
                $('.bapf_datepick.bapf_datepick_ready[data-taxonomy="'+taxonomy+'"]').find('.bapf_date_from input').datepicker( "option", "maxDate", braapf_jqui_getDate( this, dateFormat ) );
                braapf_datepicker_same(taxonomy, [$mainElement.find('.bapf_date_from input').val(), $mainElement.find('.bapf_date_to input').val()]);
                berocket_do_action('update_products', 'filter', $(this));
            });
            $(this).addClass('bapf_datepick_ready');
        });
    }
    braapf_datepicker_same = function (taxonomy, values) {
        $('.bapf_datepick.bapf_datepick_ready[data-taxonomy="'+taxonomy+'"]').each(function() {
            $(this).find('.bapf_date_from input').val(values[0]);
            $(this).find('.bapf_date_to input').val(values[1]);
        });
    }
    braapf_convert_date_to_numeric = function (date) {
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        month = month + "";
        if( month.length == 1 ) {
            month = "0"+month;
        }
        var day = date.getDate()+"";
        if( day.length == 1 ) {
            day = "0"+day;
        }
        var numeric = year+""+month+""+day;
        return numeric;
    }
    braapf_grab_single_datepicker = function(single_data, element) {
        if( element.is('.bapf_datepick.bapf_datepick_ready') && single_data != false ) {
            var $mainElement = element.find('.bapf_date_all');
            var minDate = $mainElement.data('min');
            var maxDate = $mainElement.data('max');
            var from = element.find('.bapf_date_from input').datepicker( "getDate" );
            from = braapf_convert_date_to_numeric(from);
            var from_text = element.find('.bapf_date_from input').val();
            var to = element.find('.bapf_date_to input').datepicker( "getDate" );
            to = braapf_convert_date_to_numeric(to);
            var to_text = element.find('.bapf_date_to input').val();
            if( from != minDate || to != maxDate ) {
                single_data.values = [{value:from+'_'+to, html: from_text+' - '+to_text}];
            }
        }
        return single_data;
    }
    $(document).on('braapf_unselect braapf_unselect_all', '.bapf_datepick', function(event) {
        var $mainElement = $(this).find('.bapf_date_all');
        var dateFormat = $mainElement.data('dateformat');
        var minDate = braapf_convert_numeric_to_date($mainElement.data('min'), dateFormat);
        var maxDate = braapf_convert_numeric_to_date($mainElement.data('max'), dateFormat);
        $(this).find('.bapf_date_from input').val(minDate);
        $(this).find('.bapf_date_to input').val(maxDate);
    });
    berocket_add_filter('braapf_init', braapf_init_datepicker);
    berocket_add_filter('grab_single_filter_default', braapf_grab_single_datepicker);
})(jQuery);
