(function ($) {
    'use strict';

    $.gridpress_alert_box = function (params) {

        if ($('#gridpress-alert-box').length) {
            return false;
        }

        var markup = [
            '<div id="gridpress-alert-popup" class="gridpress-modal-popup gridpress-popup-visible gridpress-forceclse-popup gridpress-alertmsg-modal">',
            '<div class="gridpress-modal-container">',
            '<div class="gridpress-modal-inner-area">',
            '<div class="gridpress-modal-content-area">',
            '<div class="gridpress-modalinit-con gridpress-confirmsg-con">',
            '<div class="alert-msg-txt">', params.message, '</div>',
            '<div class="confirm-popup-btns">',
            '<a class="gridpress-btn gridpress-alert-close">' + gridpress_func_str.conf_ok_lbl + '</a>',
            '</div></div></div></div></div></div>'
        ].join('');

        $(markup).appendTo('body');
        $.gridpress_modal_on_open_body({
            popup_type: 'alertmsg'
        });

        var alert_close_button = $('#gridpress-alert-popup .gridpress-alert-close');

        alert_close_button.click(function () {

            $.gridpress_alert_box.hide();
            return false;
        });
    }

    $.gridpress_alert_box.hide = function () {
        $.gridpress_modal_popup_close();
        $('#gridpress-alert-popup').remove();
    }

    $.gridpress_confirm_box = function (params) {

        if ($('#gridpress-confirm-popup').length > 0) {
            $('#gridpress-confirm-popup').remove();
        }

        var conf_btns_html = '';
        $.each(params.buttons, function (name, obj) {

            conf_btns_html += '<button class="confirm-button ' + obj['class'] + '">' + obj['title'] + '</button>';

            if (!obj.action) {
                obj.action = function () { };
            }
        });

        var markup = [
            '<div id="gridpress-confirm-popup" class="gridpress-modal-popup gridpress-popup-visible gridpress-forceclse-popup gridpress-confirmsg-modal">',
            '<div class="gridpress-modal-container">',
            '<div class="gridpress-modal-inner-area">',
            '<div class="gridpress-modal-content-area">',
            '<div class="gridpress-modalinit-con gridpress-confirmsg-con">',
            '<span class="confrm-msg-txt">', params.message, '</span>',
            '<div class="confirm-popup-btns">',
            conf_btns_html,
            '</div></div></div></div></div></div>'
        ].join('');

        $(markup).appendTo('body');
        $.gridpress_modal_on_open_body({
            popup_type: 'confirmsg'
        });

        var buttons = $('#gridpress-confirm-popup .confirm-button'),
            i = 0;

        $.each(params.buttons, function (name, obj) {
            buttons.eq(i++).click(function () {

                obj.action();
                return false;
            });
        });
    }

    $.gridpress_confirm_box.hide = function () {
        $.gridpress_modal_popup_close();
        $('#gridpress-confirm-popup').remove();
    }

    $(document).on('mousedown', '.gridpress-modal-popup', function (e) {
        //
        if (!$(this).hasClass('gridpress-forceclse-popup')) {

            var is_close = true;
            var this_dom = e.target;
            var thisdom_obj = $(this_dom);

            if (thisdom_obj.hasClass('gridpress-modal-content-area') || thisdom_obj.parents('.gridpress-modal-content-area').length > 0) {
                is_close = false;
            }
            if (is_close === true) {
                $.gridpress_modal_popup_close();
            }
        }
    });

    $(document).on('click', '.gridpress-popup-closebtn', function (ev) {

        ev.preventDefault();
        $.gridpress_modal_popup_close();
    });

    $(document).on('keypress keyup blur', '.gridpress-only-number', function (event) {
        var _this = $(this);
        $(this).val(_this.val().replace(/[^\d,]+/g, ""));
        if ((event.which < 48 || event.which > 57)) {
            event.preventDefault();
        }
    });

    $(document).on('submit', '.gridpress-user-form', function (ev) {

        ev.preventDefault();

        var this_form = $(this);
        var from_element = this_form[0];
        var form_data = new FormData(from_element);

        if (!this_form.hasClass('ajax-processing')) {
            this_form.append($.gridpress_delay_loader_html());

            $.ajax({
                type: "POST",
                dataType: "json",
                url: gridpress_func_str.ajax_url,
                processData: false,
                contentType: false,
                data: form_data,
                success: function (data) {
                    var nonce_err = $.gridpress_ajax_security_err_reload({result: data});
                    if (nonce_err == 'failed') {
                        return false;
                    }

                    // after success callback for other actions
                    $.gridpress_ajax_success_callback({
                        req_data: form_data,
                        result: data,
                        parent_con: this_form
                    });
                    if (data.error == '0') {
                        $.gridpress_submit_msg_alert({
                            msg: data.msg,
                            class: 'gridpress-alert-success'
                        });
                    } else if (data.error == '2') {
                        $.gridpress_submit_msg_alert({
                            msg: data.msg
                        });
                    } else {
                        $.gridpress_submit_msg_alert({
                            msg: data.msg,
                            class: 'gridpress-alert-danger'
                        });
                    }
                    if (typeof data.redirect !== 'undefined' && data.redirect !== undefined && data.redirect != '') {
                        if (data.redirect == 'same') {
                            window.location.reload();
                        } else {
                            window.location.replace(data.redirect);
                        }
                        return false;
                    }
                    this_form.removeClass('ajax-processing');
                    $.gridpress_remove_delay_loader({
                        target: this_form
                    });
                },
                error: function () {
                    this_form.removeClass('ajax-processing');
                    $.gridpress_remove_delay_loader({
                        target: this_form
                    });
                }
            });
        }

        this_form.addClass('ajax-processing');
    });

    // For switch button in form field
    $('body').on('click', '.gridpress-form-switch input[type="checkbox"]', function() {
        if ($(this).is(":checked")) {
            $(this).parents('.gridpress-form-switch').find('input[type="hidden"]').val('on');
        } else {
            $(this).parents('.gridpress-form-switch').find('input[type="hidden"]').val('off');
        }
    });

    // For buttons in form field
    $('body').on('change', '.gridpress-form-optbtn input[type="checkbox"], .gridpress-form-optbtn input[type="radio"]', function() {
        var this_btn = $(this);
        var btns_con = this_btn.closest('.gridpress-form-optbtns');
        btns_con.find('input[type="checkbox"],input[type="radio"]').each(function() {
            if ($(this).is(":checked")) {
                if (!$(this).closest('.gridpress-form-optbtn').hasClass('gridpress-chked-optbtn')) {
                    $(this).closest('.gridpress-form-optbtn').addClass('gridpress-chked-optbtn');
                }
            } else {
                $(this).closest('.gridpress-form-optbtn').removeClass('gridpress-chked-optbtn');
            }
        });
    });

    // After ajax success callback
    $.gridpress_ajax_success_callback = function (params) {
        let req_data = params.req_data;
        let result = params.result;
        let parent_con = params.parent_con || null;

        if (typeof result.callback !== 'undefined') {
            var callback_function = result.callback;
            $[callback_function]({
                req_data: req_data,
                result: result,
                parent_con: parent_con
            });
        }
    }

    $.gridpress_submit_msg_alert = function (params) {
        let msg = params.msg;
        let alert_class = params.class || 'gridpress-alert-info';
        var id = Math.floor(Math.random() * 1000000) + 1;
        $('body').find('.gridpress-alert-msg').remove();
        $('body').append('<div id="alert-' + id + '" class="gridpress-alert-msg ' + alert_class + '">' + msg + '</div>');
        setTimeout(function () { $('#alert-' + id).remove(); }, 4000);
    }

    $.gridpress_ajax_security_err_reload = function (params) {
        let result = params.result;
        if (result.error !== undefined && typeof result.error !== 'undefined' && result.error == 'security_failed') {
            window.location.reload();
            return 'failed';
        }
    }

    $.gridpress_delay_loader_html = function () {
        let loader_html = '<div class="gridpress-loder-con"><div class="gridpress-loder-iner"><div class="gridpress-loader"></div></div></div>';

        return loader_html;
    }

    $.gridpress_remove_delay_loader = function (params) {
        let target_con = params.target;
        target_con.find('.gridpress-loder-con').remove();
    }

    $.gridpress_modal_on_open_body = function (params = {}) {
        let popup_type = params.popup_type || 'common';

        const $body = $('body');
        const scroll_width = $body.width();

        $body.addClass('gridpress-popup-active');

        // Calculate the scrollbar width by comparing new width with the initial width
        const new_scroll_width = $body.width();
        const scrollbar_width = new_scroll_width - scroll_width;

        $body.css('padding-right', `${scrollbar_width}px`);
    }

    $.gridpress_modal_popup_open = function (params) {
        let target = params.target;
        let popup_class = params.class || 'gridpress-popup-maincon';
        $('#' + target).attr('class', 'gridpress-modal-popup gridpress-popup-hide ' + popup_class);
        $('#' + target).removeClass('gridpress-popup-hide').addClass('gridpress-popup-visible');
        $.gridpress_modal_on_open_body();
    }

    $.gridpress_modal_popup_close = function () {
        $('.gridpress-modal-popup').attr('class', 'gridpress-modal-popup gridpress-popup-hide');
        $('body').removeClass('gridpress-popup-active');
        $('body').css('padding-right', '');
    }

    $.gridpress_select2_common = function (params = {}) {
        let target = params.target;
        let enable_search = params.search || false;

        let select_options = {
            width: '100%'
        };
        if (enable_search === false) {
            select_options.minimumResultsForSearch = 'Infinity';
        }
        if (typeof target.attr('placeholder') !== 'undefined') {
            select_options.placeholder = target.attr('placeholder');
        }
        target.select2(select_options);
    }

    /*
    * On document ready
    */
    $(document).ready(function() {
        // select2
        if ($('.gridpress-select2-dropdown').length > 0) {
            $('.gridpress-select2-dropdown').each(function() {
                if ($(this).hasClass('gridpress-select2-wsearch')) {
                    $.gridpress_select2_common({
                        target: $(this),
                        search: true
                    });
                } else {
                    $.gridpress_select2_common({
                        target: $(this)
                    });
                }
            });
        }

        //
        $('.gridpress-items-paginatcon').each(function() {
            var pagicon = $(this);
            if (pagicon.find('.gridpress-cardsinfint-lodcon').length) {
                $.gridpress_load_carditms_on_scroll(pagicon.find('.gridpress-cardsinfint-lodcon'));
            }
        });
    });

    $(window).on("scroll", function () {
        $('.gridpress-items-paginatcon').each(function() {
            var pagicon = $(this);
            if (pagicon.find('.gridpress-cardsinfint-lodcon').length) {
                $.gridpress_load_carditms_on_scroll(pagicon.find('.gridpress-cardsinfint-lodcon'));
            }
        });
    });

    $('.gridpress-dynm-topterms-filtr').on('click', 'li a', function() {
        var this_itm = $(this);
        var iterm_id = this_itm.attr('data-id');
        var filters_con = this_itm.closest('.gridpress-dynm-topterms-filtr');
        var cards_con = this_itm.closest('.gridpress-dynm-cardscon').find('.gridpress-dynm-cards-holdrcon');
        filters_con.find('li').removeClass('active');
        this_itm.parent('li').addClass('active');
        if (filters_con.hasClass('gridpress-dynm-slected-postfiltrs')) {
            if (iterm_id == 'all') {
                cards_con.find('>div').removeAttr('style');
            } else {
                cards_con.find('>div').hide();
                cards_con.find('.gridpress-term-' + iterm_id).removeAttr('style');
            }
        } else {
            var cards_section = this_itm.closest('.gridpress-dynm-cards-section');
            if (!cards_section.find('.gridpress-loder-con').length) {
                cards_section.append($.gridpress_delay_loader_html());
            }

            $.ajax({
                type: "POST",
                dataType: "json",
                url: gridpress_func_str.ajax_url,
                data: {
                    gid: cards_section.attr('data-id'),
                    term_id: this_itm.attr('data-id'),
                    gridpress_nonce: gridpress_func_str.front_nonce,
                    action: 'gridpress_cards_top_terms_filter_call'
                },
                success: function (data) {
                    var nonce_err = $.gridpress_ajax_security_err_reload({result: data});
                    if (nonce_err == 'failed') {
                        return false;
                    }

                    if (data.cards != '') {
                        cards_section.find('.gridpress-dynm-cards-holdrcon').html(data.cards);
                    }
                    if (data.pagination != '') {
                        cards_section.find('.gridpress-items-paginatcon').html(data.pagination);
                        cards_section.find('.gridpress-items-paginatcon').removeAttr('style');
                    } else {
                        cards_section.find('.gridpress-items-paginatcon').hide();
                    }

                    $.gridpress_remove_delay_loader({
                        target: cards_section
                    });
                },
                error: function () {
                    $.gridpress_remove_delay_loader({
                        target: cards_section
                    });
                }
            });
        }
    });

    /*
    * For pagination
    */
    $('.gridpress-items-paginatcon').on('click', '.gridpress-carditms-pagelinks a', function(e) {
        e.preventDefault();
        var this_itm = $(this);
        var cards_section = this_itm.closest('.gridpress-dynm-cards-section');

        var filtr_term_id = '';
        if (cards_section.find('.gridpress-dynm-topterms-filtr').length) {
            filtr_term_id = cards_section.find('.gridpress-dynm-topterms-filtr').find('li.active').find('a').attr('data-id');
        }

        if (!cards_section.find('.gridpress-loder-con').length) {
            cards_section.append($.gridpress_delay_loader_html());
        }

        $.ajax({
            type: "POST",
            dataType: "json",
            url: gridpress_func_str.ajax_url,
            data: {
                gid: cards_section.attr('data-id'),
                page_num: this_itm.attr('data-pagenum'),
                term_id: filtr_term_id,
                gridpress_nonce: gridpress_func_str.front_nonce,
                action: 'gridpress_cards_pagination_pagenum_call'
            },
            success: function (data) {
                var nonce_err = $.gridpress_ajax_security_err_reload({result: data});
                if (nonce_err == 'failed') {
                    return false;
                }

                if (data.cards != '') {
                    cards_section.find('.gridpress-dynm-cards-holdrcon').html(data.cards);
                    cards_section.find('.gridpress-items-paginatcon').html(data.pagination);
                }

                $.gridpress_remove_delay_loader({
                    target: cards_section
                });
            },
            error: function () {
                $.gridpress_remove_delay_loader({
                    target: cards_section
                });
            }
        });
    });

    /*
    * For load more items
    */
    $('.gridpress-items-paginatcon').on('click', '.gridpress-cardslodmore-btncon a', function(e) {
        e.preventDefault();
        var this_itm = $(this);
        var cards_section = this_itm.closest('.gridpress-dynm-cards-section');

        var paginatcon = cards_section.find('.gridpress-items-paginatcon');

        if (typeof paginatcon.attr('style') !== 'undefined') {
            return;
        }

        var filtr_term_id = '';
        if (cards_section.find('.gridpress-dynm-topterms-filtr').length) {
            filtr_term_id = cards_section.find('.gridpress-dynm-topterms-filtr').find('li.active').find('a').attr('data-id');
        }

        if (!paginatcon.find('.gridpress-loder-con').length) {
            paginatcon.append($.gridpress_delay_loader_html());
        }

        let pagenum = parseInt(this_itm.attr('data-pagenum'));
        let t_pages = parseInt(this_itm.attr('data-pages'));

        if (!this_itm.hasClass('ajax-processing')) {
            $.ajax({
                type: "POST",
                dataType: "json",
                url: gridpress_func_str.ajax_url,
                data: {
                    gid: cards_section.attr('data-id'),
                    page_num: pagenum,
                    term_id: filtr_term_id,
                    gridpress_nonce: gridpress_func_str.front_nonce,
                    action: 'gridpress_cards_pagination_pagenum_call'
                },
                success: function (data) {
                    var nonce_err = $.gridpress_ajax_security_err_reload({result: data});
                    if (nonce_err == 'failed') {
                        return false;
                    }
                    if ((pagenum+1) > t_pages) {
                        paginatcon.hide();
                    } else {
                        this_itm.attr('data-pagenum', (pagenum+1));
                    }

                    if (data.cards != '') {
                        cards_section.find('.gridpress-dynm-cards-holdrcon').append(data.cards);
                    }

                    $.gridpress_remove_delay_loader({
                        target: paginatcon
                    });
                    this_itm.removeClass('ajax-processing');
                },
                error: function () {
                    $.gridpress_remove_delay_loader({
                        target: paginatcon
                    });
                    this_itm.removeClass('ajax-processing');
                }
            });
        }
        this_itm.addClass('ajax-processing');
    });

    $.gridpress_is_element_in_viewport = function ($el) {
        var scroll_top = $(window).scrollTop();
        var scroll_bottom = scroll_top + $(window).height();

        var el_top = $el.offset().top;
        var el_bottom = el_top + $el.outerHeight();

        return (el_bottom >= scroll_top) && (el_top <= scroll_bottom);
    }

    $.gridpress_load_carditms_on_scroll = function (target) {
        if ($.gridpress_is_element_in_viewport(target)) {
            target.find('a').trigger('click');
        }
    }

    /*
    * For video popup
    */
    $('.gridpress-videocard-itmcon').on('click', 'a', function(ev) {
        ev.preventDefault();
        var card_id = $(this).closest('.gridpress-videocard-itmcon').attr('data-id');
        let popup_id = 'gridpress-common-popup';
        $.gridpress_modal_popup_open({
            target: popup_id,
            class: 'gridpress-videocard-modal'
        });
        let content_con = $('#' + popup_id).find('.gridpress-modalinit-con');
        content_con.html($.gridpress_delay_loader_html());
        $.ajax({
            url: gridpress_func_str.ajax_url,
            method: "POST",
            data: {
                card_id: card_id,
                gid: $(this).closest('.gridpress-dynm-cards-section').attr('data-id'),
                gridpress_nonce: gridpress_func_str.front_nonce,
                action: 'gridpress_addin_card_video'
            },
            dataType: "json",
            success: function (response) {
                $.gridpress_remove_delay_loader({
                    target: content_con
                });
                content_con.html(response.video_html);
            },
            error: function() {
                $.gridpress_remove_delay_loader({
                    target: content_con
                });
            }
        });
    });

    /*
    * For adding popover
    */
    $('body').on('mouseover', '[data-gridpress-popover]', function() {
        var element = $(this);
        element.addClass('gridpress-popover-domelem');

        // Get the computed style of the element
        var element_style = element.css('position');

        if (element_style === 'static') {
            element.css('position', 'relative');
        }

        var element_rect = this.getBoundingClientRect();
        var element_space_above = element_rect.top;

        // Create popover element
        var popover_con = $('<span>', {
            class: 'gridpress-popover-holder',
            html: '<span class="gridpress-popover-content">' + element.attr('data-gridpress-popover') + '</span><span class="gridpress-popover-arrow"></span>'
        });

        element.append(popover_con);
        var popover_con_elem = element.find('.gridpress-popover-holder');
        var popover_height = popover_con_elem.outerHeight();

        if (element_space_above > popover_height) {
            popover_con_elem.css('top', '-' + popover_height + 'px');
        } else {
            popover_con_elem.addClass('gridpress-bottom-popover');
            popover_con_elem.css('bottom', '-' + popover_height + 'px');
        }

        element.addClass('gridpress-popover-hovered');
    });

    // Delegate the mouseout event to remove the popover
    $('body').on('mouseout', '[data-gridpress-popover]', function() {
        var element = $(this);
        element.removeClass('gridpress-popover-hovered');
        element.find('.gridpress-popover-holder').remove();
    });

    /*
    * Input Calendar Popup
    */
    $('body').on('focus', '.gridpress-date-input', function() {
        var this_input = $(this);
        if (this_input.closest('.gridpress-calendarinput-wrap').length == 0) {
            this_input.wrap('<div class="gridpress-calendarinput-wrap"></div>');
            this_input.closest('.gridpress-calendarinput-wrap').append('<div class="gridpress-calendar-popup"></div>');
        }

        $.gridpress_hide_open_calendars();

        const calendar_input_wrap = this_input.closest('.gridpress-calendarinput-wrap');
        const calendar_holder = calendar_input_wrap.find('.gridpress-calendar-popup');

        if (typeof this_input.attr('data-miny') !== 'undefined') {
            calendar_holder.attr('data-miny', this_input.attr('data-miny'));
            this_input.removeAttr('data-miny');
        }
        if (typeof this_input.attr('data-maxy') !== 'undefined') {
            calendar_holder.attr('data-maxy', this_input.attr('data-maxy'));
            this_input.removeAttr('data-maxy');
        }

        let today = new Date();

        let max_year = today.getFullYear();
        let min_year = 1920;
        if (typeof calendar_holder.attr('data-miny') !== 'undefined') {
            min_year = parseInt(calendar_holder.attr('data-miny'));
        }
        if (typeof calendar_holder.attr('data-maxy') !== 'undefined') {
            max_year = parseInt(calendar_holder.attr('data-maxy'));
        }

        let selected_date = this_input.val();

        // Initialize with today's date
        let selected_day = today.getDate();
        let selected_month = today.getMonth(); // Months are zero-indexed
        let selected_year = today.getFullYear();

        const date_pattern = /^(\d{2})\/(\d{2})\/(\d{4})$/;
        if (selected_date != '' && date_pattern.test(selected_date)) {
            const [, day, month, year] = selected_date.match(date_pattern);

            // Convert the day, month, and year to integers
            selected_day = parseInt(day, 10);
            selected_month = parseInt(month, 10) - 1;
            selected_year = parseInt(year);
        }

        let def_day = selected_day;
        let def_month = selected_month;
        let def_year = selected_year;

        if (typeof calendar_holder.attr('data-day') === 'undefined') {
            calendar_holder.attr('data-day', selected_day);
            calendar_holder.attr('data-month', selected_month);
            calendar_holder.attr('data-year', selected_year);
        }

        $.gridpress_generate_calendar({
            calendar: calendar_holder,
            date_input: this_input,
            day: def_day,
            month: def_month,
            year: def_year,
            min_year: min_year,
            max_year: max_year
        });
    });

    $.gridpress_hide_open_calendars = function() {
        $('.gridpress-date-input').each(function() {
            var this_input = $(this);
            var calendar_input_wrap = this_input.closest('.gridpress-calendarinput-wrap');
            var calendar_holder = calendar_input_wrap.find('.gridpress-calendar-popup');
            calendar_holder.hide();
        });
    }

    $.gridpress_generate_year_options = function (min_year, max_year) {
        const $yearSelect = $('<select></select>');
        for (let year = min_year; year <= max_year; year++) {
            const $option = $('<option></option>').val(year).text(year);
            $yearSelect.append($option);
        }
        return $yearSelect;
    }

    $.gridpress_update_calendar = function (calendar_holder, dateinput_field, month, year, selected_day, selected_month, selected_year) {
        const month_name = new Date(selected_year, selected_month).toLocaleString('default', { month: 'long' });
        calendar_holder.find('.gridpress-month-name').text(month_name);

        const $days = calendar_holder.find('.gridpress-calendar-days').empty();

        const first_day = new Date(selected_year, selected_month, 1).getDay();
        const last_date = new Date(selected_year, selected_month + 1, 0).getDate();

        for (let i = 0; i < first_day; i++) {
            $days.append($('<div class="gridpress-calendar-day gridpress-disabled"></div>'));
        }

        for (let i = 1; i <= last_date; i++) {
            const $day = $('<div class="gridpress-calendar-day"></div>').text(i);

            // Check if this is the selected date
            if (i === selected_day && month === selected_month && year === selected_year) {
                $day.addClass('gridpress-selected');
            }

            $day.on('click', function() {

                calendar_holder.attr('data-day', i);
                calendar_holder.attr('data-month', selected_month);
                calendar_holder.attr('data-year', selected_year);

                // Display the selected date in the input
                dateinput_field.val(`${(i < 10 ? '0' : '') + i}/${(selected_month < 9 ? '0' : '') + (selected_month + 1)}/${selected_year}`);
                calendar_holder.hide(); // Hide the calendar after selection
            });

            $days.append($day);
        }
    }

    $.gridpress_generate_calendar = function (params) {
        let today = new Date();

        let calendar_holder = params.calendar;
        let dateinput_field = params.date_input;
        let day = params.day || today.getDate();
        let month = params.month || today.getMonth();
        let year = params.year || today.getFullYear();
        let min_year = params.min_year || 1920;
        let max_year = params.max_year || today.getFullYear();

        let selected_day = calendar_holder.attr('data-day');
        let selected_month = calendar_holder.attr('data-month');
        let selected_year = calendar_holder.attr('data-year');

        selected_day = parseInt(selected_day, 10);
        selected_month = parseInt(selected_month, 10);
        selected_year = parseInt(selected_year);

        calendar_holder.empty();

        const $header = $('<div class="gridpress-calendar-header"></div>');

        const $prevButton = $('<a class="gridpress-monthchnger-btn"><i class="gridpress-icon-chevron-left"></i></a>');
        $prevButton.on('click', function() {
            let year_dropdown = calendar_holder.find('.gridpress-calendar-header select');
            if (selected_month === 0) {
                selected_month = 11;
                selected_year--;
            } else {
                selected_month--;
            }
            if (selected_year < min_year) {
                selected_year = min_year;
            }
            year_dropdown.val(selected_year);
            $.gridpress_update_calendar(calendar_holder, dateinput_field, month, year, selected_day, selected_month, selected_year);
        });

        const $nextButton = $('<a class="gridpress-monthchnger-btn"><i class="gridpress-icon-chevron-right"></i></a>');
        $nextButton.on('click', function() {
            let year_dropdown = calendar_holder.find('.gridpress-calendar-header select');
            if (selected_month === 11) {
                selected_month = 0;
                selected_year++;
            } else {
                selected_month++;
            }
            if (selected_year > max_year) {
                selected_year = max_year;
            }
            year_dropdown.val(selected_year);
            $.gridpress_update_calendar(calendar_holder, dateinput_field, month, year, selected_day, selected_month, selected_year);
        });

        const $monthYear = $('<span class="gridpress-month-name"></span>');

        const $yearSelect = $.gridpress_generate_year_options(min_year, max_year);
        $yearSelect.val(selected_year);
        $yearSelect.on('change', function() {
            selected_year = parseInt($(this).val());
            $.gridpress_update_calendar(calendar_holder, dateinput_field, month, year, selected_day, selected_month, selected_year);
        });

        $header.append($prevButton, $monthYear, $yearSelect, $nextButton);
        calendar_holder.append($header);

        const $days = $('<div class="gridpress-calendar-days"></div>');
        calendar_holder.append($days);

        $.gridpress_update_calendar(calendar_holder, dateinput_field, month, year, selected_day, selected_month, selected_year);

        calendar_holder.removeAttr('style');
    }

    /*
    * For click on dom events
    */
    $(document).on('click', function(event) {
        // For hide date calendars
        if ($('.gridpress-date-input').length > 0) {
            $('.gridpress-date-input').each(function() {
                var this_input = $(this);
                var calendar_input_wrap = this_input.closest('.gridpress-calendarinput-wrap');
                var calendar_holder = calendar_input_wrap.find('.gridpress-calendar-popup');
                if (!$(event.target).closest('.gridpress-calendarinput-wrap').length) {
                    calendar_holder.hide();
                }
            });
        }
    });

})(jQuery);
