jQuery(document).ready(function ($) {
    /**
     * Customizer Option Auto focus
     */
    jQuery('h3.accordion-section-title').on('click', function () {
        var id = $(this).parent().attr('id');
        try {
            var is_panel = id.includes("panel");
            var is_section = id.includes("section");

            if (is_panel) {
                focus_item = id.replace('accordion-panel-', '');
                console.log(focus_item);
                history.pushState({}, null, '?autofocus[panel]=' + focus_item);
            }
            if (is_section) {
                focus_item = id.replace('accordion-section-', '');
                history.pushState({}, null, '?autofocus[section]=' + focus_item);
            }
        } catch (e) {

        }
    });

    //FontAwesome Icon Control JS
    $('body').on('click', '.kingcabs-icon-list li', function () {
        var icon_class = $(this).find('i').attr('class');
        $(this).addClass('icon-active').siblings().removeClass('icon-active');
        $(this).parent('.kingcabs-icon-list').prev('.kingcabs-selected-icon').children('i').attr('class', '').addClass(icon_class);
        $(this).parent('.kingcabs-icon-list').next('input').val(icon_class).trigger('change');
    });

    $('body').on('click', '.kingcabs-selected-icon', function () {
        $(this).next().slideToggle();
    });



    // Gallery Control
    $('.upload_gallery_button').click(function (event) {
        var current_gallery = $(this).closest('label');

        if (event.currentTarget.id === 'clear-gallery') {
            //remove value from input
            current_gallery.find('.gallery_values').val('').trigger('change');

            //remove preview images
            current_gallery.find('.gallery-screenshot').html('');
            return;
        }

        // Make sure the media gallery API exists
        if (typeof wp === 'undefined' || !wp.media || !wp.media.gallery) {
            return;
        }
        event.preventDefault();

        // Activate the media editor
        var val = current_gallery.find('.gallery_values').val();
        var final;

        if (!val) {
            final = '[gallery ids="0"]';
        } else {
            final = '[gallery ids="' + val + '"]';
        }
        var frame = wp.media.gallery.edit(final);

        frame.state('gallery-edit').on(
            'update', function (selection) {

                //clear screenshot div so we can append new selected images
                current_gallery.find('.gallery-screenshot').html('');

                var element, preview_html = '', preview_img;
                var ids = selection.models.map(
                    function (e) {
                        element = e.toJSON();
                        preview_img = typeof element.sizes.thumbnail !== 'undefined' ? element.sizes.thumbnail.url : element.url;
                        preview_html = "<div class='screen-thumb'><img src='" + preview_img + "'/></div>";
                        current_gallery.find('.gallery-screenshot').append(preview_html);
                        return e.id;
                    }
                );

                current_gallery.find('.gallery_values').val(ids.join(',')).trigger('change');
            }
        );
        return false;
    });

    //MultiCheck box Control JS
    $('.customize-control-checkbox-multiple input[type="checkbox"]').on('change', function () {

        var checkbox_values = $(this).parents('.customize-control').find('input[type="checkbox"]:checked').map(
            function () {
                return $(this).val();
            }
        ).get().join(',');

        $(this).parents('.customize-control').find('input[type="hidden"]').val(checkbox_values).trigger('change');

    }
    );

    //Chosen JS
    $(".hs-chosen-select").chosen({
        width: "100%"
    });

});

