/*
 *************************************
 JMUERY SCRIPT FOR MARLEV TEMPLATE MANAGER - LAYOUT ADMIN PANEL JOOMLA TEMPLATE
 *************************************
 Copyright http://marlev.it
 *************************************
 GPL License
 *************************************
 */
jQuery.run_status_bar = function run_status_bar(aggiungi) {
    var actual = jQuery("#waitbox").find(".progress-bar").data("percent");
    var new_percent = actual + aggiungi;

    jQuery("#waitbox").find(".progress-bar").data("percent", new_percent);
    jQuery("#waitbox").find(".progress-bar").attr("aria-valuenow", new_percent).css({"width": new_percent + "%"});
}
jQuery.load_position_config_on_request = function load_position_config_on_request(element) {
    var uri = jQuery("#ml-uri").val() + "components/com_tmarlevv2/images/spinner.gif";
    var load_image = '<img  src="' + uri + '" />';
    jQuery(element).html('<i class="fa fa-spinner fa-pulse"></i>');
    var edit = jQuery(element);
    var options_block = jQuery(element).parents(".options-block");
    var items = {};
    var subpos = jQuery(options_block).data("subpos");
    var sender = {};
    sender["parentid"] = jQuery(options_block).data("parentid");
    sender["subpos"] = subpos;
    sender["selected"] = jQuery(options_block).data("selected");
    sender["subposdata"] = jQuery(options_block).data("subposdata");
    items[subpos] = sender;
    jQuery.get_single_config(items, edit);
}
jQuery.enable_position_on_selects = function () {
    jQuery("#tm-layout").find("#avvaiable_positions_multi").jchosen({"search": true});
    jQuery("#tm-layout").find("#layout-lift-editposition").insertBefore("#layout-lift-life");
    jQuery("#layout-lift-life").removeAttr("style");


}
//edit position from select choise, is rebuild to read new config from ajax
jQuery.enable_select_position_on_edit = function enable_select_position_on_edit() {
    var sselect_width = jQuery(".layout-lift-section").find(".chosen-container").width();
    var value = jQuery("#layout-lift-editposition").find("select").val();
    var clone_editable_positions = jQuery("#layout-lift-editposition").find("select").clone();
    jQuery("#layout-lift-editposition").html("");
    var sequenzial = parseInt(jQuery(clone_editable_positions).data("sequenzial")) + 1;
    var newid = "editable_positions_" + sequenzial;
    jQuery(clone_editable_positions).attr("data-sequenzial", sequenzial).attr("id", newid).val(value);
    jQuery(clone_editable_positions).appendTo("#layout-lift-editposition");

    jQuery("#" + newid).css({"width": sselect_width}).jchosen({"search": true});
    jQuery("#" + newid).on("change", function () {
        var editconfig = jQuery(this).val();
        if (editconfig == "editposition") {

        } else {
            var data_funcs = jQuery(editconfig).attr("data-funcs");
            if (data_funcs == "nofuncs") {
                jQuery.load_position_config_on_request(editconfig);
            } else {
                jQuery.open_slider_from_func(editconfig);
            }
        }

    });
}
jQuery.device_on_layout_click = function () {
    jQuery(".adapt-viewer").click(function () {
        if (jQuery(this).hasClass("active-view")) {
            return true;
        }
        jQuery.device_on_layout_click_run(jQuery(this).attr("id"));

    });
    jQuery.device_on_layout_click_run("view-desktop");
}

jQuery.run_adaptive_for_device = function (active_view, element) {
    switch (active_view) {
        case "view-desktop" :
            var type = "pc";
            var pos = "col-lg-$ col-xl-$ col-xxl-$";
            break;
        case "view-tablet" :
            var type = "tablet";
            var pos = "col-md-$";
            break;
        case "view-mobile" :
            var type = "mobile";
            var pos = "col-sm-$";
    }
    for (var i = 0; i <= 12; i++) {
        jQuery(element).removeClass('col-md-' + i + ' col-lg-' + i + ' col-sm-' + i + ' col-' + i + ' col-xl-' + i + ' col-xxl-' + i);
        var getdata = jQuery(element).attr("data-" + type);
        var prepare_cl = pos.replaceAll("$", getdata);
        jQuery(element).addClass(prepare_cl);
        jQuery.each(["order", "pull", "offset"], function (index, value) {
            jQuery(element).removeClass(' ' + value + "-" + i + ' ' + value + "-sm-" + i + ' ' + value + "-md-" + i + ' ' + value + "-lg-" + i + ' ' + value + "-xl-" + i + ' ' + value + "-xxl-" + i);
        });
    }
    jQuery(element).removeClass("d-md-none d-lg-none d-sm-none d-md-block d-lg-block d-none d-sm-block");
    var position = jQuery(element).data("position");
    jQuery("#editconfig-" + position + "-" + type).find(".colmove").each(function () {
        jQuery(element).addClass(jQuery(this).val());
    });
    jQuery("#editconfig-" + position + "-" + type).find(".colhide").each(function () {
        jQuery(element).addClass(jQuery(this).val());
    });

    if (jQuery(element).next().hasClass("clearfix")) {
        jQuery("#clear-" + type + "-" + position).remove();
        jQuery.addclear_after_position(jQuery("#editconfig-" + position + "-" + type).find(".addclear"));
    }
}


jQuery.device_on_layout_click_run = function (active_view) {
    jQuery(".active-view").removeClass("active-view");
    jQuery("#" + active_view).addClass("active-view")
    jQuery("#devices-view").attr("data-active", jQuery("#" + active_view).attr("id"));

    jQuery(".innerposition, .subposition").each(function () {
        jQuery.run_adaptive_for_device(active_view, jQuery(this));
    });
    jQuery("#loadlayout").removeClass("view-desktop view-tablet view-mobile").addClass(jQuery("#" + active_view).attr("id"))
    if(jQuery("#layout-life-mode #mainframe").length > 0){
        jQuery.mainframe_position();
    }
}
jQuery.show_suspend_section = function () {

    function run_suspend(element) {
        var id = jQuery(element).data("id");
        if (jQuery(element).prop('checked')) {
            jQuery(id).slideUp();
        } else {
            jQuery(id).slideDown();
            var goto = jQuery(id).offset().top - 75;
            jQuery("html, body").animate({scrollTop: goto}, 500);
        }
    }

    jQuery(".suspend-section").each(function () {
        run_suspend(this);
    });

    jQuery(".suspend-section").click(function () {
        run_suspend(this);
        jQuery(this).parent().tooltip('hide');
    });
}
jQuery.enable_tooltip_in_actions = function enable_tooltip_in_actions() {
    jQuery(".add-position-block, .content-pos-name, #devices-view").find(".hasTooltip").each(function () {

        jQuery(this).tooltip({"html": true, "trigger": "hover focus", "position": "top"});
    });
    jQuery(".edit-modules,.edit-options,.icon-cancel").each(function () {
        jQuery(this).tooltip({"html": true, "trigger": "hover focus", "position": "top"});
        jQuery(this).click(function (e) {
            jQuery(this).tooltip("hide");
        });
    });
}
jQuery.assign_functions_on_edit_click = function assign_functions_on_edit_click(element) {
    var data_funcs = jQuery(element).attr("data-funcs");
    if (data_funcs == "nofuncs") {
        jQuery(element).attr("data-funcs", "funcs");
        var id = jQuery(element).data("configid");

        //assegnazione al cambio col
        jQuery(jQuery(id).find(".colchange")).on("change", function () {
            jQuery.colchange(jQuery(this));

        });
        //assegnazione di hide
        jQuery(jQuery(id).find(".colhide")).on("change", function () {
            jQuery.colhide(jQuery(this));

        });
        //assegnazione di colmove
        jQuery(jQuery(id).find(".colmove")).on("change", function () {
            jQuery.colmove(jQuery(this));

        });
        jQuery(jQuery(id).find(".child_padding")).on("change", function () {
            jQuery.child_disable_padding(jQuery(this));

        });

        //assegnazione di addclear after position
        jQuery(jQuery(id).find(".addclear")).on("change", function () {
            jQuery.addclear_after_position(jQuery(this));
            jQuery.update_life();

        });
        //seleziona background

        jQuery.choise_background(jQuery(id).find(".background-type"));
        jQuery(jQuery(id).find(".background-type")).on("change", function () {
            jQuery.choise_background(jQuery(this));

        });

        jQuery(jQuery(id).find(".change-config-view")).on("change", function () {
            jQuery.changeDeviceConfig_checkbox(jQuery(this));

        });


        jQuery(id).find(".minicolors").each(function () {
            if (jQuery(this).hasClass("rgb-color")) {
                jQuery(this).minicolors({theme: "bootstrap", opacity: true, control: 'hue', format: 'rgb'});
            } else {
                jQuery(this).minicolors({theme: "bootstrap"});
            }
        });

        ///funzioni per copia di configurazioni
        jQuery(id).find('.copy-actions .btn').on("click", function (e) {
            e.preventDefault();
            jQuery.show_actions_copy_tool(jQuery(this));

        });
        jQuery(id).find(".transfer-config").on("change", function () {
            jQuery.select_device_tocopy_checkbox(jQuery(this));
        });

        jQuery(id).find('.copy-config-now').on("click", function (e) {
            e.preventDefault();
            jQuery.copy_config_now(jQuery(this));
        });
        jQuery(id).find('.export-config-now').on("click", function (e) {
            e.preventDefault();
            jQuery.export_config_now(jQuery(this));
        });

        jQuery(jQuery(id).find(".copy-params-device")).on("click", function () {
            jQuery.copydevicesconfig_checkbox(jQuery(this));
        });
        //fine per la copia di configurazioni

        jQuery(id).find(".hasPopover").each(function () {

            jQuery(this).tooltip({
                "html": true,
                "trigger": "hover focus",
                "container": "#layout-slider-block",
                "position": "top"
            });
        });

        //  customElements.define('marlev-field-media', JoomlaFieldMedia);
        jQuery(jQuery(id).find('.field-media-wrapper .button-select')).on("click", function () {
            jQuery.fix_ovverflow_image_select(jQuery(this));
           // jQuery.update_life();
        });
        //per demo degli effetti
        jQuery(id).find(".effect, .efspeed, .efdelay").on("change", function () {
            jQuery.run_effect_demo(jQuery(this));
        });
        //jQuery.run_effect_demo
        jQuery(id).find("select").each(function(){
            jQuery(this).on("change", function () {
                jQuery.update_life();
            });
        });
        jQuery(id).find("input").each(function(){
            jQuery(this).on("input", function () {
                jQuery.update_life();
            });

        });
    }
};
jQuery.fix_ovverflow_image_select = function fix_ovverflow_image_select(element) {
    jQuery(element).parents("#layout-slider-block").css({"overflow": "visible"});
    setTimeout(function () {
        var call_modal = setInterval(function () {
            if (!jQuery(element).parents(".field-media-wrapper").find(".modal").hasClass("show")) {
                jQuery(element).parents("#layout-slider-block").css({"overflow": "auto"});
                setTimeout(function(){
                    jQuery.update_life();
                },300);

                clearInterval(call_modal);
            }
        }, 500);

    }, 800);

};

jQuery.append_tabscript = function append_tabscript(data) {
    if (data.trim()) {
    var get_script = /<script type="application\/json" class="joomla-script-options [\s\S]*?">([\s\S]*?)<\/script>/gm;
    var match = get_script.exec(data);
    var json = jQuery.parseJSON("[" + match[1] + "]");
    jQuery(json).each(function (i, val) {
        jQuery.each(val, function (k, v) {
            if (k == "bootstrap.tabs") {
                jQuery.each(v, function (l, i) {

                    Joomla.initialiseTabs(l, i)
                });
            }
        });
    });
    }
};


jQuery.recount_exist_positions = function recount_exist_pos(parent, element) {

    var parentid = jQuery(parent).attr("id");

    var find_defcollayout_input = "#" + parentid + '-defcollayout';
    var find_tabcollayout_input = "#" + parentid + '-tabcollayout';
    var find_mobcollayout_input = "#" + parentid + '-mobcollayout';
    var find_positions_input = "#" + parentid + '-positions';
    jQuery(find_defcollayout_input).val("");
    jQuery(find_tabcollayout_input).val("");
    jQuery(find_mobcollayout_input).val("");
    jQuery(find_positions_input).val("");


    jQuery(parent).find(element).each(function () {

        var defcollayout = jQuery(find_defcollayout_input).val();
        var tabcollayout = jQuery(find_tabcollayout_input).val();
        var mobcollayout = jQuery(find_mobcollayout_input).val();
        var getpos = jQuery(find_positions_input).val();
        var pc = jQuery(this).attr("data-pc");
        var mobile = jQuery(this).attr("data-mobile");
        var tablet = jQuery(this).attr("data-tablet");
        var position = jQuery(this).attr("data-position");
        if (defcollayout == '') {
            jQuery(find_defcollayout_input).val(pc);
            jQuery(find_tabcollayout_input).val(tablet);
            jQuery(find_mobcollayout_input).val(mobile);
            jQuery(find_positions_input).val(position);
        } else {
            jQuery(find_defcollayout_input).val(defcollayout + "," + pc);
            jQuery(find_tabcollayout_input).val(tabcollayout + "," + tablet);
            jQuery(find_mobcollayout_input).val(mobcollayout + "," + mobile);
            jQuery(find_positions_input).val(getpos + "," + position);
        }
    });
};

jQuery.colchange = function colchange(element) {
    function remove_classes(type, parent_class) {
        for (var i = 1; i <= 12; i++) {
            if (type == "pc") {
                jQuery(parent_class).removeClass('col-xl-' + i + ' col-lg-' + i + ' col-xxl-' + i);
            } else if (type == "tablet") {

                jQuery(parent_class).removeClass('col-md-' + i);
            } else if (type == "mobile") {
                jQuery(parent_class).removeClass('col-sm-' + i);
            }
        }
    }


    function run_colsfunction(element, position_class, parent_class) {
        var thisval = jQuery(element).val();
        var type = jQuery(element).data("type");
        var parentid = '#' + jQuery(element).data("id");

        if (type == "pc") {
            jQuery(parent_class).attr("data-pc", thisval);
            remove_classes(type, parent_class);
            jQuery(parent_class).addClass(' col-lg-' + thisval + ' col-xl-' + thisval + ' col-xxl-' + thisval);
            jQuery.recount_exist_positions(parentid, position_class);
            //var count = jQuery.colscount(parentid, position_class);
        } else if (type == "tablet") {
            jQuery(parent_class).attr("data-tablet", thisval);
            remove_classes(type, parent_class);
            jQuery(parent_class).addClass('col-md-' + thisval);
            jQuery.recount_exist_positions(parentid, position_class);


        } else if (type == "mobile") {
            jQuery(parent_class).attr("data-mobile", thisval);
            remove_classes(type, parent_class);
            jQuery(parent_class).addClass('col-sm' + thisval);
            jQuery.recount_exist_positions(parentid, position_class);
        }

    }


    function switch_col(element) {
        var parent_class = '.pos_' + jQuery(element).data('position');

        if (jQuery(parent_class).hasClass('subposition')) {

            var position_class = ".subposition";
            run_colsfunction(element, position_class, parent_class);
        } else {

            var position_class = ".innerposition";
            run_colsfunction(element, position_class, parent_class);

        }

    }

    switch_col(element);
};

jQuery.coldelete = function coldelete() {
    function remove_option_from_select_avvaiable_positions(position_class) {
        var position = jQuery(position_class).data("position");
        var remove = '#editconfig-' + position;
        jQuery("#editable_positions").find("option").each(function () {
            if (this.value == remove) {
                jQuery(this).remove();
            }
        });
        jQuery("#avvaiable_positions_multi").find("option").each(function () {
            if (this.value == remove) {
                jQuery(this).remove();
            }
        });
        jQuery("#params_socialpositions").find("option").each(function () {
            if (this.value == position) {
                jQuery(this).remove();
            }
        });
        jQuery("#editable_positions").trigger("chosen:updated");
        jQuery("#avvaiable_positions_multi").trigger("chosen:updated");
        jQuery("#params_socialpositions").trigger("chosen:updated");

    }

    function close_slider_if_deleted_option_in(position_class) {
        var deletedit = '#configid-' + jQuery(position_class).data("position");
        if (jQuery("#layout-slider-block").find(deletedit).length) {
            jQuery(deletedit).find(".close-slider").click();

        }
    }

    function run_delete(element, position_class) {
        jQuery(element).tooltip('hide');
        var position = jQuery(element).parents(position_class).data("position");
        jQuery("#edit-" + position).find(".addclear").each(function () {
            jQuery(this).val(0);
            jQuery.addclear_after_position(jQuery(this));
        });
        jQuery(element).parents(position_class).remove();
        jQuery.colchange(element);
    }

    function delete_col(position_class) {
        jQuery(position_class).find(".icon-cancel").click(function () {
            close_slider_if_deleted_option_in(jQuery(this).parents(position_class));
            var parentid = '#' + jQuery(this).parents(".position").attr("id");
            run_delete(this, position_class);
            remove_option_from_select_avvaiable_positions(jQuery(this).parents(position_class));
            jQuery.recount_exist_positions(parentid, position_class);
        });
    }

    function delete_col_subposition(position_class) {
        jQuery(position_class).find(".icon-cancel").click(function () {
            if (!jQuery(this).closest(".innerposition").length) {
                close_slider_if_deleted_option_in(jQuery(this).parents(position_class));
                var parentid = '#' + jQuery(this).parents(".position").attr("id");
                run_delete(this, position_class);
                remove_option_from_select_avvaiable_positions(jQuery(this).parents(position_class));
                jQuery.recount_exist_positions(parentid, position_class);
            }
        });
    }

    delete_col_subposition('.subposition');
    delete_col('.innerposition');
};

jQuery.addposition = function addposition() {
    function find_prev_numeric_position(parent, get_positions, get_cols) {
        for (var i = 1; i <= get_cols; i++) {

            if (!jQuery(parent).find('.pos_' + get_positions + '-' + i).length) {
                var make_prev = i - 1;
                if (make_prev == 0) {
                    return ['prima', get_positions + '-' + i];

                } else {
                    return ['.pos_' + get_positions + '-' + make_prev, get_positions + '-' + i];
                }
            }
        }
        return false;
    }

    function find_prev_text_position(parent, positions) {
        var prev_position = false;
        jQuery.each(positions, function (index, value) {

            if (!jQuery(parent).find('.pos_' + value).length) {

                if (index == 0) {

                    prev_position = ['prima', value];
                } else {
                    var make_prev = index - 1;
                    prev_position = ['.pos_' + positions[make_prev], value];
                }
                return prev_position;
            }
        });
        return prev_position;
    }

    function check_if_clear_enabled_for_insert_position(responseval, prev_position) {
        if (jQuery(prev_position).next().hasClass("end-position")) {
            responseval.insertAfter(prev_position);
        } else {
            var nn = jQuery(prev_position).nextAll();
            jQuery(nn).each(function () {
                if (jQuery(this).hasClass("w-100") && jQuery(this).hasClass("end-position")) {
                    responseval.insertBefore(jQuery(this));
                    return false;
                } else if (!jQuery(this).hasClass("w-100")) {
                    responseval.insertBefore(jQuery(this));
                    return false;
                }

            });

        }


    }

    function add_as_option_to_select_avvaiable_positions(position) {
        jQuery(".editable_positions").prepend(jQuery('<option>', {
            value: '#openoptions-' + position,
            text: position
        }));
        jQuery.enable_select_position_on_edit();
        jQuery("#avvaiable_positions_multi").prepend(jQuery('<option>', {
            value: '#editconfig-' + position,
            text: position
        })).trigger("chosen:updated");

        jQuery("#params_socialpositions").prepend(jQuery('<option>', {
            value: position,
            text: position
        })).trigger("chosen:updated");

    }

    function load_position(element, sendposition, sendclass, sendparentid, divclass, prev_this_class, parentid) {
        jQuery.ajax({
            url: jQuery(location).attr('href'),
            dataType: "html",
            type: 'GET',
            cache: false,
            data: {
                loadposition: 1,
                position: sendposition,
                loadclass: sendclass,
                parentid: sendparentid,
                blockclass: divclass
            },
            success: function (data) {
                //  var response = data;

                var responseval = jQuery(data).filter('#load_' + sendposition);

                //  jQuery(parentid).html(responseval);
                var inafter = jQuery(parentid).children(".row");
                if (prev_this_class[0] == "prima") {
                    responseval.prependTo(inafter);
                } else {

                    if (jQuery(prev_this_class[0]).length) {
                        check_if_clear_enabled_for_insert_position(responseval, prev_this_class[0])

                    } else {
                        responseval.insertAfter(inafter);
                    }
                }
                jQuery('#load_' + sendposition).find(".show-position").addClass("pos-just-added");
                setTimeout(function () {
                    jQuery('#load_' + sendposition).find(".show-position").addClass("pos-just-added-remove");
                }, 700);
                setTimeout(function () {
                    jQuery('#load_' + sendposition).find(".show-position").removeClass("pos-just-added-remove pos-just-added");
                }, 1700);

                jQuery('#load_' + sendposition).find(".edit-options").on("mouseenter", function () {
                    jQuery.assign_functions_on_edit_click(jQuery(this));
                });


                jQuery.coldelete();

                jQuery.config_layout_slider(jQuery('#load_' + sendposition).find(".edit-options"), jQuery('#load_' + sendposition).find(".close-slider"));
                //end new


                jQuery.append_tabscript(data);
                jQuery.recount_exist_positions(parentid, '.' + divclass);
                var active_view = jQuery("#devices-view").attr("data-active");
                jQuery.run_adaptive_for_device(active_view, '#load_' + sendposition);
                jQuery(element).removeClass("workinng");
                jQuery('#load_' + sendposition).find(".edit-modules,.edit-options,.icon-cancel").each(function () {
                    jQuery(this).tooltip({"html": true, "trigger": "hover focus", "position": "top"});
                    jQuery(this).click(function (e) {
                        jQuery(this).tooltip("hide");
                    });
                });

                add_as_option_to_select_avvaiable_positions(sendposition);


            }
        });
    }

    jQuery(".add-position").click(function () {
        if (jQuery(this).hasClass("workinng")) {
            return;
        } else {
            jQuery(this).addClass("workinng");
        }
        var get_positions = jQuery(this).data('positions');
        var parent = jQuery(this).data('id')
        var parentid = "#" + parent;
        var get_maxcols = jQuery(this).data('maxcols');


        var get_class = jQuery(this).data('class');
//        var response = false;
        var prev_this_class = false
        if (get_maxcols > 1) {
            var prev_this_class = find_prev_numeric_position(parentid, get_positions, get_maxcols);

        } else {
            var positions = get_positions.toString().split(',');
            var maxcols = get_maxcols.toString().split(',');
            var prev_this_class = find_prev_text_position(parentid, positions);
        }

        if (prev_this_class == false) {
            var message = jQuery("#all-positions_added").data("message");

            alert(message);
            jQuery(this).removeClass("workinng");
            return;
        }
        var fix_parent = parent.replace("pos_", "");
        var loadposition = load_position(this, prev_this_class[1], 'pos_' + prev_this_class[1], fix_parent, get_class, prev_this_class, parentid);

    });
};
jQuery.suspend_positions = function suspend_positions() {
    jQuery(".suspend-position").click(function () {
        var parent = '#' + jQuery(this).data('id');
        if (jQuery(this).prop('checked')) {
            jQuery(parent).addClass("pos_suspended");
            jQuery.suspend_positions_in_visible(parent, "suspend");
        } else {
            jQuery(parent).removeClass("pos_suspended");
            jQuery.suspend_positions_in_visible(parent, "unsuspend");
        }

        jQuery(this).parent().tooltip('hide');
    });
};
jQuery.suspend_positions_onload = function suspend_positions_onload() {
    jQuery(".pos_suspended").each(function () {
        jQuery.suspend_positions_in_visible(jQuery(this), "suspend");
    });
}
jQuery.suspend_positions_in_visible = function suspend_positions_in_visible(parent, action) {
    jQuery(parent).find("div").each(function () {
        var cl = jQuery(this).attr("class");
        var pattern = /col-(lg|md|xs|sm)-[0-9]{0,2}/i;
        if (pattern.test(cl)) {
            if (action == "suspend") {
                jQuery(this).slideUp();
            } else {
                jQuery(this).slideDown();
            }
        }
    });
}
jQuery.width_large = function width_large() {
    jQuery(".inlarge-click").click(function () {
        var parentid = '#' + jQuery(this).data('id');
        var parentinput = '#' + jQuery(this).data('inputid');
        var actual = jQuery(this).data('actual');
        if (actual == "container") {
            var value = "container-fluid";
        } else {
            var value = "container";
        }
        jQuery(parentid).removeClass(actual).addClass(value);
        jQuery(this).data('actual', value);
        jQuery(parentinput).val(value);

    });
};
jQuery.colhide = function colhide(element) {
    function remove_move_classes(type, thisclass) {

        jQuery(thisclass).removeClass('d-md-none d-sm-none d-md-block d-none d-sm-block d-lg-none d-lg-block');
    }

    function run_move(element) {
        var type = jQuery(element).data("type");
        var position = ".pos_" + jQuery(element).data("position");
        var value = jQuery(element).val();
        remove_move_classes(type, position);
        if (value != "") {
            jQuery(position).addClass(value);
        }

    }

    run_move(element);
};
jQuery.child_disable_padding = function child_disable_padding(element){

        var type = jQuery(element).data("type");
        var position = "#pos_" + jQuery(element).data("position");
        var value = jQuery(element).val();
        console.log(position +" "+type+" "+value)
        if (value == "") {
            if(type == "pc"){
                jQuery(position).removeClass('pc-child-nopadding');
            }
            else if(type == "tablet"){
                jQuery(position).removeClass('tablet-child-mopadding');
            }
            else if(type == "mobile"){
                jQuery(position).removeClass('mobile-child-nopadding');
            }
        }
        else {
            if(type == "pc"){
                jQuery(position).addClass('pc-child-nopadding');
            }
            else if(type == "tablet"){
                jQuery(position).addClass('tablet-child-nopadding');
            }
            else if(type == "mobile"){
                jQuery(position).addClass('mobile-child-nopadding');
            }
        }



}
jQuery.colmove = function colmove(element) {
    function remove_classes(movemode, type, thisclass) {
        //  var parent = jQuery(element).parents(position_class);
        for (var i = 0; i <= 12; i++) {
            if (type == "pc") {
                jQuery(thisclass).removeClass(movemode + '-lg-' + i + ' ' + movemode + '-xl-' + i + ' ' + movemode + '-xxl-' + i);
            } else if (type == "tablet") {

                jQuery(thisclass).removeClass(movemode + '-md-' + i);
            } else if (type == "mobile") {
                jQuery(thisclass).removeClass(movemode + '-sm-' + i);
            }
        }
    }

    function run_move(element) {
        var type = jQuery(element).data("type");
        var position = ".pos_" + jQuery(element).data("position");
        var movemode = jQuery(element).data("move");
        var value = jQuery(element).val();
        remove_classes(movemode, type, position);
        if (value != "") {
            jQuery(position).addClass(value);
        }

    }
    run_move(element);

};
jQuery.choise_background = function choise_backgrounds(element) {

    var value = jQuery(element).val();

    var parent = jQuery(element).parents(".params-block");

    if (value == "") {
        jQuery(parent).find(".colors-box, .image-box").hide();
    } else if (value == "color") {
        jQuery(parent).find(".colors-box").show();
        jQuery(parent).find(".image-box").hide();
    } else if (value == "image") {
        jQuery(parent).find(".colors-box").hide();
        jQuery(parent).find(".image-box").show();
    }
};

jQuery.run_effect_demo = function (element) {
    var device = jQuery(element).data("type");
    var position = jQuery(element).data("position");
    var parent = "#editconfig-" + position + "-" + device;
    var effect = jQuery(parent).find(".effect").val().replace(/tablet|mobile/gi, "pc");
    if (effect == "") {
        return true;
    }
    var efspeed = jQuery(parent).find(".efspeed").val();
    var efdelay = jQuery(parent).find(".efdelay").val();
    jQuery(".pos_" + position).find(".show-position").css({
        "background": "#68a901",
        "animation-duration": efspeed + "s",
        "animation-delay": efdelay + "s"
    }).addClass(effect)
    setTimeout(function () {
        jQuery(".pos_" + position).find(".show-position").removeAttr("style").removeClass(effect);
    }, efspeed * 1000 + efdelay * 1000 + 500);
}


jQuery.addclear_after_position = function addclear_after_position(element) {
//todo addclear
    var device = jQuery(element).data("device");
    var position = jQuery(element).data("position");
    if (jQuery(element).val()== "1") {
        // jQuery(element).prop('checked', true);
        var el_class = '.pos_' + position;
        switch (device) {
            case "pc":
                var clear = 'gy-2 d-none d-lg-block clear-pc';
                break;
            case "tablet":
                var clear = 'w-100 gy-2 d-none d-md-block d-lg-none clear-tablet';
                break;
            case "mobile":
                var clear = 'w-100 gy-2 d-block d-sm-block d-md-none clear-mobile';
                break;

        }
        jQuery(el_class).after('<div id="clear-' + device + '-' + position + '" class="w-100 ' + clear + '"></div>');
    } else {
        var el_id = '#clear-' + device + '-' + position
        jQuery(el_id).remove();
    }
    //   });

};


//functon va richiamata dal metodo onchange in checkbox pc, tablet, mobile //segue devicetab
jQuery.changeDeviceConfig_checkbox = function changeDeviceConfig_checkbox(element) {
    var this_config = jQuery(element).data("configid");
    if (jQuery(element).prop('checked', true) && !jQuery(element).hasClass('checbox-checked')) {

        jQuery(element).addClass("working checbox-checked");

        jQuery(element).parents(".select-edit-device").find("input").each(function () {
            var get_config = "#" + jQuery(this).data("configid");
            if (!jQuery(this).hasClass("working")) {
                jQuery(this).prop('checked', false).removeClass("checbox-checked");
                jQuery(get_config).hide();
            } else {
                // jQuery(get_config).show();
                jQuery(get_config).show("fade", 500);
                jQuery.device_on_layout_click_run(jQuery(this).attr("data-view"));
            }
        });
        jQuery(element).removeClass("working");
    } else {

        jQuery(element).prop('checked', true);

    }
};

jQuery.show_actions_copy_tool = function show_actions_copy_tool(element) {
    function show_element(block, showit) {

        //var h = jQuery(block).find(showit).innerHeight();
        //jQuery(block).find(showit).css({"height": "auto"});


        // jQuery(block).css({"height": 0, "display": "block"}).animate({"height": h}, 500);
        jQuery(block).css({"display": "block", "position": "relative"})
        jQuery(block).find(showit).slideDown();
        // setTimeout(function () {
        //     jQuery(block).find(showit).addClass("slideInDown animated").show();
        //     jQuery(block).find(showit).slideDown();
        // },250);
        //jQuery(block).find(showit).addClass("slideInDown animated").show();
        setTimeout(function () {
            //jQuery(block).find(showit).removeClass("slideInDown animated");
            jQuery(block).find(showit).css({"height": "auto"});
            jQuery(block).css({"height": "auto"});
        }, 600);

    }

    function hide_elements(block) {
        jQuery(block).animate({"height": 0}, 500).promise().done(function () {
            jQuery(block).removeAttr("style").hide();
        });
        jQuery(block).find(".for-positions, .for-all-device,.for-export").slideUp();
        //jQuery(block).find(".for-positions, .for-all-device").addClass("slideOutDown animated");

        // setTimeout(function () {
        //     jQuery(block).find(".for-positions, .for-all-device").hide().removeClass("slideOutDown animated");
        // }, 700);
    }

    if (!jQuery(element).hasClass("working")) {
        jQuery(element).addClass("working");
        var block = jQuery(element).parents("#load-slider-content").find(".copy-actions-block");
        var type = jQuery(element).data("type");
        var block_state = jQuery(block).data("state");
        if (type === "copy") {
            var showit = ".for-all-device";
            var hideit = ".for-positions,.for-export";
        } else if (type == "actions") {
            var showit = ".for-positions";
            var hideit = ".for-all-device,.for-export";
        } else {
            var showit = ".for-export";
            var hideit = ".for-all-device,.for-positions";
        }

        if (block_state === "close") {
            show_element(block, showit);
            jQuery(block).data("opened", type).data("state", "open");

        } else if (block_state === "open") {
            var opened = jQuery(block).data("opened");
            if (opened === type) {
                hide_elements(block);
                setTimeout(function () {
                    jQuery(block).data("state", "close");
                }, 700);

            } else {
                jQuery(block).find(hideit).slideUp(200);
                jQuery(block).animate({"height": 0}, 200);
                setTimeout(function () {

                    var h = jQuery(block).find(showit).outerHeight();
                    jQuery(block).animate({"height": h}, 700);
                    jQuery(block).find(showit).slideDown();
                    //jQuery(block).find(showit).addClass("slideInDown animated").show();
                    setTimeout(function () {
                        //jQuery(block).find(showit).removeClass("slideInDown animated");
                        jQuery(block).css({"height": "auto"});
                    }, 1000);
                    jQuery(block).data("opened", type);
                }, 300);
            }
        }
        setTimeout(function () {
            jQuery(element).removeClass("working");
        }, 1000)
    }
    // setTimeout(function () {
    //     jQuery(element).stop().off().on("click", function (e) {
    //         e.preventDefault();
    //         jQuery.show_actions_copy_tool(jQuery(element));
    //     });
    // }, 300);
};
//funzione per copia di configurazioni da una posizione ad altra
jQuery.select_device_tocopy_checkbox = function select_device_tocopy_checkbox(element) {
    var elpar = jQuery(element).parents(".select-device-tocopy");
    var type = jQuery(element).data("copy");

    if (type == "all") {
        jQuery(element).addClass("working");
        jQuery(elpar).find(".transfer-config").each(function () {
            if (!jQuery(this).hasClass("working")) {
                jQuery(this).prop('checked', false);
            }
        });
        jQuery(element).removeClass("working");
    } else {
        jQuery(elpar).find(".copy-all").prop('checked', false);
    }
};
jQuery.copy_config_now_helper = function copy_config_now_helper(element, tocopy, positions) {
    var ifall = 'all' in tocopy;
    if (ifall) {
        var tocopy = {pc: true, tablet: true, mobile: true};
    }
    var copyconfig = jQuery(element).data("copyconfig");
    jQuery.each(tocopy, function (key) {
        jQuery.each(positions, function (thisnull, transfer) {
            jQuery.copy_configuration("#" + copyconfig + "-" + key, transfer + "-" + key);
        });

    });
    var eseguito = jQuery(element).data("eseguito");
    jQuery(element).parent().find(".element-message").remove();
    jQuery("<div class='element-message' >" + eseguito + "</div>").insertAfter(element);
    setTimeout(function () {
        jQuery(element).parent().find(".element-message").remove();
    }, 2000);
}

jQuery.export_config_now = function copy_config_now(element) {
    jQuery(element).parent().find(".element-message").remove();
    var import_to = jQuery(element).parents(".for-export").find("#duplicates_template_select").val();
    if (import_to == "" || import_to === null || !parseInt(import_to)) {
        jQuery("<div class='element-message'>" + jQuery(element).attr("data-selecttemplatefirst") + "</div>").insertAfter(element);
        return true;
    }
    var edit_position = jQuery(element).parents(".ml-configs").attr("data-parent").replace("#edit", "#posconfig");
    var Xcollayout = ['defcollayout', 'tabcollayout', 'mobcollayout'];
    var sender = {};
    sender['import_to'] = import_to;
    sender['export_type'] = jQuery(edit_position).attr("data-export-type");//tipo blocco section|position
    sender['posname'] = jQuery(edit_position).attr("data-subpos");  //nome posizione
    var parent_block = jQuery(edit_position).attr("data-parentid");
    sender['parent'] = parent_block.replace("pos_", "");//nome blocco parent *serve solo per position
    sender['position_onblock'] = 0;//posizione della position nel blocco *serve solo per position
    sender['cols'] = 0;   //numero di positions nel blocco *serve solo per position

    var counter = 0
    if (sender['export_type'] == "position") {
        jQuery("#" + parent_block).find(".subposition").each(function () {
            if (jQuery(this).attr("data-position") == sender['posname']) {
                sender['position_onblock'] = counter;

            } else {
                counter = counter + 1;
            }
        });
        jQuery.each(Xcollayout, function (key, val) {
            var splitter = jQuery("#" + parent_block + "-" + val).val().split(",");
            sender['cols'] = splitter.length;
            sender[val] = splitter[sender['position_onblock']];
        });
    } else {
        jQuery.each(Xcollayout, function (key, val) {
            sender[val] = 0;
        });
    }
    var json_send = JSON.stringify(sender);
    jQuery(element).css({"width":jQuery(element).outerWidth()});
    jQuery(element).html('<i class="fa fa-spinner fa-pulse"></i>');
    var out = jQuery.get_serialized("template.exportposconfig");
    var token = jQuery('#hidden-token').find("input").attr("name");
    jQuery.ajax({
        url: jQuery(location).attr('href') + '&' + token + '=1',
        dataType: "html",
        type: 'POST',
        cache: false,
        data: {task: "template.exportposconfig", postdata: out,jsondata:json_send},
        success: function (data) {
            var responseval = jQuery(data).filter('#export-done');

            jQuery(element).html(jQuery(element).attr("data-text"));
            if(jQuery(responseval).attr("data-result") == "fail"){
                jQuery("<div class='element-message'>Failed... Please contact support</div>").insertAfter(element);
            }
            else {
                jQuery("<div class='element-message'>" + jQuery(element).attr("data-eseguito") + "</div>").insertAfter(element);
            }
            setTimeout(function(){
                jQuery(element).parent().find(".element-message").remove();
            },2000);
        }

    });
};
///END

jQuery.copy_config_now = function copy_config_now(element) {
    jQuery(element).parent().find(".element-message").remove();
    var elpar = jQuery(element).parents(".copy-actions-block");
    var tocopy = {};
    jQuery(elpar).find(".transfer-config").each(function () {
        if (jQuery(this).is(':checked')) {
            tocopy[jQuery(this).data("copy")] = true;
        }
    });
    if (jQuery.isEmptyObject(tocopy)) {
        var select_device_first = jQuery(element).data("selectdevicefrst");
        jQuery("<div class='element-message'>" + select_device_first + "</div>").insertAfter(element);
        return false;
    }
    var positions = jQuery("#avvaiable_positions_multi").val();
    if (positions === null) {
        var selectdestination_first = jQuery(element).data("selectdestination");
        jQuery("<div class='element-message'>" + selectdestination_first + "</div>").insertAfter(element);
        return false;
    }
    var items = {};
    jQuery.each(positions, function (thisnull, editconfig) {
        editconfig = editconfig.replace("editconfig", "posconfig");
        var data_funcs = jQuery(editconfig).data("funcs");
        if (data_funcs == "nofuncs") {
            jQuery(editconfig).data("funcs", "func-assigned");
            var subpos = jQuery(editconfig).data("subpos");
            var sender = {};
            sender["parentid"] = jQuery(editconfig).data("parentid");
            sender["subpos"] = subpos;
            sender["selected"] = jQuery(editconfig).data("selected");
            sender["subposdata"] = jQuery(editconfig).data("subposdata");
            items[subpos] = sender;
        }

    });

    var rest = Object.keys(items).length;
    if (rest > 0) {
        Promise.all(jQuery.get_configs(items)).then(jQuery.copy_config_now_helper(element, tocopy, positions));
    } else {
        jQuery.copy_config_now_helper(element, tocopy, positions);
    }


};
//funzione che esegue la copia stessa, usata in due funzioni
jQuery.copy_configuration = function copy_configuration(source, destination) {
    jQuery(source).find("input").each(function () {
        var identifier = "." + jQuery(this).parents(".controls").data("identifier");

        var value = jQuery(this).val();
        var element = jQuery(this);
        jQuery(destination).find(identifier).each(function () {
            var inp = jQuery(this).find("input");//minicolors
            //if checkbox
            if (jQuery(inp).parent().hasClass("checkbox")) {
                var checked = false;
                if (jQuery(element).prop("checked")) {
                    checked = true;
                }
                jQuery(inp).attr("checked", checked);

            } else {//altri input di tipo color, input normali
                jQuery(inp).val(value);
                if (jQuery(inp).hasClass("minicolors-input")) {
                    jQuery(inp).parents(".minicolors").find(".minicolors-swatch-color").css({"background-color": value});
                }
                if (jQuery(inp).hasClass("addclear")) {
                    jQuery.addclear_after_position(jQuery(inp));
                }
            }


        });
    });
    jQuery(source).find("select").each(function () {
        var identifier = "." + jQuery(this).parents(".controls").data("identifier");
        var getvalue = jQuery(this).val();

        jQuery(destination).find(identifier).each(function () {
            var inp = jQuery(this).find("select");//minicolors
            var exists_option = false;
            jQuery(inp).find("option").each(function () {
                if (this.value == getvalue) {
                    exists_option = true;
                }
            });
            if (exists_option === true) {
                jQuery(inp).val(getvalue);
                jQuery(inp).find("option").each(function () {
                    if (this.value == getvalue) {
                        jQuery(this).attr("selected", "selected");
                    } else {
                        jQuery(this).removeAttr("selected");
                    }
                });
                jQuery(inp).trigger("chosen:updated");
                if (jQuery(inp).hasClass("background-type")) {
                    jQuery.choise_background(inp);
                } else if (jQuery(inp).hasClass("colchange")) {
                    jQuery.colchange(jQuery(inp));
                } else if (jQuery(inp).hasClass("colmove")) {
                    jQuery.colmove(jQuery(inp));
                } else if (jQuery(inp).hasClass("colhide")) {
                    jQuery.colhide(jQuery(inp));
                }

            }

        });
    });
};

//funzione per copiare le configurazione di diveces, da quello selezionato ad altri
jQuery.copydevicesconfig_checkbox = function copydevicesconfig_checkbox(element) {
    function close_opration(element) {
        var label = jQuery(element).parent().find("label");
        var defaultlang = jQuery(label).data("default");
        var eseguito = jQuery(label).data("eseguito");
        var wlabel = jQuery(label).width();
        jQuery(label).css({"width": wlabel}).html(eseguito);
        setTimeout(function () {
            jQuery(label).removeAttr("style").html(defaultlang);
            jQuery(element).removeClass("working");
            jQuery(element).prop('checked', false);
        }, 1500);
    }

    function copy_data(element) {
        var content = jQuery(element).parents("#load-slider-content");
        var checkbox_block = jQuery(content).find(".select-edit-device");
        var selected;
        jQuery(checkbox_block).find("input").each(function () {

            if (jQuery(this).prop('checked')) {
                selected = jQuery(this).data("configid");

            }
        });
        jQuery.copy_configuration("#" + selected, "#load-slider-content");

    }

    if (jQuery(element).prop('checked') && !jQuery(element).hasClass('working')) {
        jQuery(element).addClass("working");
        copy_data(element);
        close_opration(element);
    }
};

jQuery.lift_scroll = function layout_lift_change() {

    function run(element) {
        jQuery(element).on("change", function () {
            var getid = "#" + jQuery(this).val();
            var goto = jQuery(getid).offset().top - 35;
            // alert(goto)
            jQuery("html, body").animate({scrollTop: goto}, 500);

        });
    }

    run(".layout-fast-navigate-section");
    run(".layout-fast-navigate-blocks");
};
//elabora la risposta di post per config caricati con ajax
jQuery.elabora_risposta_post = function elabora_risposta_post(data) {
    var responseval = jQuery(data).filter('#posconfig-loader');
    jQuery(responseval).find(".config-block").each(function () {
        var options_block = jQuery(this).data("posconfig");
        var loaded_edit_id = "#" + jQuery(this).attr("id");
        jQuery(this).insertAfter(options_block);
        // jQuery(options_block).find(".edit-options").on("mouseenter", function () {
        jQuery.assign_functions_on_edit_click(jQuery(options_block).find(".edit-options"));
        //  });
        jQuery.config_layout_slider(jQuery(options_block).find(".edit-options"), jQuery(loaded_edit_id).find(".close-slider"));
        jQuery(options_block).find(".edit-options").each(function () {
            jQuery(this).tooltip({"html": true, "trigger": "hover focus", "position": "top"});
            jQuery(this).click(function (e) {
                jQuery(this).tooltip("hide");
            });
        });
        // var active_view = jQuery("#devices-view").attr("data-active");
        // jQuery.run_adaptive_for_device(active_view, loaded_edit_id);
        // jQuery('#load_' + sendposition).find(".edit-modules,.edit-options,.icon-cancel").each(function () {
        //     jQuery(this).tooltip({"html": true, "trigger": "hover focus", "position": "top"});
        //     jQuery(this).click(function (e) {
        //         jQuery(this).tooltip("hide");
        //     });
        // });

    });
    jQuery.append_tabscript(data);
}

