(function($) {
    'use strict';

    $(document).ready(function() {
        // For color selection form field
        if ($('.gridpress-color-field').length > 0) {
            $('.gridpress-color-field').wpColorPicker();
        }

        if ($('.gridpress-page-selecter').length > 0) {
            $('.gridpress-page-selecter').select2({
                width: '100%',
                ajax: {
                    type: "POST",
                    url: gridpress_adminscript_str.ajax_url,
                    dataType: 'json',
                    data: function (params) {
                        var query = {
                            q: params.term,
                            gridpress_nonce: gridpress_adminscript_str.nonce,
                            action: 'gridpress_ajax_get_page_results'
                        }
                        return query;
                    },
                    processResults: function(data) {
                        var nonce_err = $.gridpress_ajax_security_err_reload({result: data});
                        if (nonce_err == 'failed') {
                            return false;
                        }
                        return {
                            results: data.results
                        };
                    }
                },
                minimumInputLength: 1,
                language: {
                    inputTooShort: function () {
                        return gridpress_func_str.type_to_search;
                    }
                }
            });
        }

        if ($('.gridpress-postitms-selectr').length > 0) {
            $('.gridpress-postitms-selectr').select2({
                width: '100%',
                placeholder: gridpress_adminscript_str.srch_post_itms,
                ajax: {
                    type: "POST",
                    url: gridpress_adminscript_str.ajax_url,
                    dataType: 'json',
                    data: function (params) {
                        var query = {
                            q: params.term,
                            pstype: $(this).attr('data-ptype'),
                            gridpress_nonce: gridpress_adminscript_str.nonce,
                            action: 'gridpress_ajax_get_posts_results'
                        }
                        return query;
                    },
                    processResults: function(data) {
                        var nonce_err = $.gridpress_ajax_security_err_reload({result: data});
                        if (nonce_err == 'failed') {
                            return false;
                        }
                        return {
                            results: data.results
                        };
                    }
                },
                minimumInputLength: 1,
                language: {
                    inputTooShort: function () {
                        return gridpress_func_str.type_to_search;
                    }
                }
            });
        }
    });

    $(window).on("resize", function () {
        //
    });

    // For image upload form field
    $('body').on( 'click', '.gridpress-form-imgfield .gridpress-upload-imgbtn, .gridpress-form-imgfield .gridpress-field-inputcon input', function() {
        var button = $(this);
        var main_parent = button.parents('.gridpress-form-imgfield');
        var custom_uploader = wp.media({
            title: gridpress_adminscript_str.insert_img_lbl,
            library : {
                type : 'image'
            },
            button: {
                text: gridpress_adminscript_str.use_img_lbl // button label text
            },
            multiple: false
        }).on('select', function() {
            var attachment = custom_uploader.state().get('selection').first().toJSON();
            main_parent.find('img').attr('src', attachment.url);
            main_parent.find('.gridpress-field-inputcon input[type="text"]').val(attachment.url);
            main_parent.find('.gridpress-field-inputcon input[type="hidden"]').val(attachment.id);
            main_parent.find('.gridpress-field-imgcon').removeClass('gridpress-hide');
            main_parent.find('.gridpress-field-btnscon .gridpress-remvupload-imgbtn').removeClass('gridpress-hide');
        }).open();
    });

    // For removing uploaded image form field
    $('body').on('click', '.gridpress-form-imgfield .gridpress-remvupload-imgbtn', function() {
        var button = $(this);
        var main_parent = button.parents('.gridpress-form-imgfield');
        main_parent.find('.gridpress-field-inputcon input').val('');
        main_parent.find('.gridpress-field-imgcon').addClass('gridpress-hide');
        main_parent.find('.gridpress-field-btnscon .gridpress-remvupload-imgbtn').addClass('gridpress-hide');
    });

    $('.gridpress-setupwiz-nextbtn').on('click', function() {
        var button = $(this);
        var stplinks_con = button.closest('.setupwiz-stepslnks-con');
        var main_parent = button.closest('.setupwiz-maincontnt-con');
        var menu_itms_con = main_parent.find('.setupwiz-stps-menu');
        var menu_progress = menu_itms_con.find('.setupwiz-step-progress');
        var menu_cntnt_con = main_parent.find('.setupwiz-sfieldshldr-con .setupwiz-allsteps-con');
        var total_steps = menu_itms_con.find('>div').length;
        var curr_step = parseInt(stplinks_con.attr('data-sid'));
        if (curr_step < total_steps) {
            stplinks_con.find('.gridpress-setupwiz-backbtn').removeAttr('style');
            var nxt_stp = curr_step+1;
            var progres_height = 6.75 + (9 * (nxt_stp - 1));
            menu_progress.css({height: progres_height + 'vh'});
            stplinks_con.attr('data-sid', nxt_stp);
            menu_itms_con.find('div[data-sid="' + nxt_stp + '"]').addClass('setupwiz-activmenu');
            menu_cntnt_con.find('.setupwiz-stepitm-con').removeClass('setupwiz-activstep');
            menu_cntnt_con.find('.setupwiz-stepitm-' + nxt_stp).addClass('setupwiz-activstep');
            if (nxt_stp == total_steps) {
                button.hide();
                stplinks_con.find('.gridpress-setupwiz-finshbtn').removeAttr('style');
            }
        }
    });

    $('.gridpress-setupwiz-backbtn').on('click', function() {
        var button = $(this);
        var stplinks_con = button.closest('.setupwiz-stepslnks-con');
        var main_parent = button.closest('.setupwiz-maincontnt-con');
        var menu_itms_con = main_parent.find('.setupwiz-stps-menu');
        var menu_progress = menu_itms_con.find('.setupwiz-step-progress');
        var menu_cntnt_con = main_parent.find('.setupwiz-sfieldshldr-con .setupwiz-allsteps-con');
        var curr_step = parseInt(stplinks_con.attr('data-sid'));
        if (curr_step > 1) {
            stplinks_con.find('.gridpress-setupwiz-finshbtn').hide();
            stplinks_con.find('.gridpress-setupwiz-nextbtn').removeAttr('style');
            //
            var prev_stp = curr_step-1;
            var progres_height = 6.75 + (9 * (prev_stp - 1));
            menu_progress.css({height: progres_height + 'vh'});
            stplinks_con.attr('data-sid', prev_stp);
            menu_itms_con.find('div[data-sid="' + curr_step + '"]').removeClass('setupwiz-activmenu');
            menu_cntnt_con.find('.setupwiz-stepitm-con').removeClass('setupwiz-activstep');
            menu_cntnt_con.find('.setupwiz-stepitm-' + prev_stp).addClass('setupwiz-activstep');

            if (prev_stp == 1) {
                button.hide();
            }
        }
    });

    $('.setupwiz-wel-btns').on('click', '.gridpress-welcm-startbtn', function() {
        var button = $(this);
        button.closest('.setupwiz-sfieldshldr-con').find('.setupwiz-stepslnks-con .gridpress-setupwiz-nextbtn').trigger('click');
    });

    $('.setupwiz-webtypes-con').on('click', '.setupwiz-option-wrapper', function() {
        var button = $(this);
        button.closest('.setupwiz-webtypes-con').find('.setupwiz-option-wrapper').removeClass('setupwiz-step-active');
        button.addClass('setupwiz-step-active');
    });

})(jQuery);