jQuery.active_default_device_width = function active_default_device_width(configid) {
    var checkbox = configid.replace("configid", "checkbox");
    var active = jQuery("#devices-view").attr("data-active");
    //jQuery(checkbox + "-pc, "+checkbox + "-tablet, "+ checkbox + "-mobile").prop("checked",false);
    switch (active) {
        case "view-desktop":
            checkbox = checkbox + "-pc";
            break;
        case "view-tablet" :
            checkbox = checkbox + "-tablet";
            break;
        case "view-mobile" :
            checkbox = checkbox + "-mobile";
    }

    jQuery(checkbox).click();
}

jQuery.open_slider = function open_slider(slider_width) {
    var w_width = jQuery(window).width();
    var loadlayout_width = w_width - slider_width - 10;
    if(jQuery("#layout-life-mode #mainframe").length > 0){
        var spzio_left = w_width - jQuery(" #layout-life-mode").width();
        var blk = " #layout-life-mode";
    }
    else {
        var spzio_left = w_width - jQuery("#loadlayout").width();
        var blk = "#loadlayout";
    }

    // var offset_left = jQuery("#loadlayout").offset().left;
    // var load_width = jQuery("#loadlayout").width();
    //    jQuery("#loadlayout").css({'position': 'absolute', left: offset_left, "width": load_width});
    var run_minimize_loadlayout = setInterval(function () {
        var layoutsilder_offset_left = jQuery("#layout-slider-block").offset().left;
        var rimanenza = spzio_left - slider_width - 30;
        if (rimanenza <= layoutsilder_offset_left) {
            jQuery(blk).animate({
                "width": loadlayout_width,
                "right": 0
            }, 350).css({'position': 'absolute'});
            if(blk == " #layout-life-mode"){
                setTimeout(function(){
                    jQuery("#loadlayout").css({"width":loadlayout_width,"right":0,'position': 'absolute'});
                    jQuery.mainframe_position();
                },400);

            }
            //    jQuery("#loadlayout").css({"position": 'absolute', "left": slider_width, "width": loadlayout_width});
            clearInterval(run_minimize_loadlayout);
        }
    }, 1);
    jQuery("#layout-slider-block").show("slide", {direction: "left"}, 600).data("state", "open");
    //      jQuery("#layout-slider-block").animate({"marginLeft": 0}, 500).data("state", "open");

}

jQuery.close_slider = function close_slider(content_modified, parentid) {
    if(jQuery("#layout-life-mode #mainframe").length > 0){
       // jQuery("#layout-life-mode").removeAttr("style");
        jQuery("#layout-life-mode").animate({"width":"100%","margin-left":"-0.75rem","left":0},200);
        setTimeout(function(){
            jQuery.mainframe_position();
        },300);
    }
    jQuery("#loadlayout").animate({
        "width": jQuery(".tm-home").width() - 15,
        "marginRight": 15
    }, 400).promise().done(function () {
        //   jQuery("#loadlayout").animate({"marginRight": 15}, 350).promise().done(function(){
        jQuery("#loadlayout").removeAttr("style");
        // });
    });
    setTimeout(function () {
        jQuery.run_fixed_menu();
    }, 700);
    jQuery("#layout-slider-block").toggle("slide", {direction: "left"}, 700).promise().done(function () {
        jQuery(content_modified).hide();
        jQuery(content_modified).appendTo(parentid);
    });

    // jQuery("#loadlayout").css({"position": 'relative', "left": 'auto', "width": "auto"});
    jQuery("#layout-slider-block").data("state", "close");

}

jQuery.load_content_inslider = function load_content_inslider(configid) {
    var state = jQuery("#layout-slider-block").data("state");
    if (state == "close") {
        //jQuery(configid).fadeIn(1200);
        jQuery(configid).appendTo("#load-slider-content").promise().done(function () {
            jQuery(configid).show();
        });
    } else {
        jQuery(configid).appendTo("#load-slider-content");
        jQuery(configid).fadeIn(1200);
    }
    jQuery("#avvaiable-positions-block").appendTo("#load-slider-content .positions-posto");
    jQuery("#duplicates_template_select").jchosen({"width": "200px"});
    jQuery("#duplicates_template").appendTo("#load-slider-content .export-posto");
    jQuery(configid).find(".font").find("select").jchosen();
}

jQuery.return_content_alposto = function return_content_alposto(configid) {

    if (jQuery("#load-slider-content").find(".ml-configs").length) {

        var content_modified = jQuery("#load-slider-content").find(".ml-configs");

        var parentid = content_modified.data("parent");
        var get_parent_from_click = jQuery(configid).data("parent");

        if (get_parent_from_click == parentid) {
            jQuery.close_slider(content_modified, parentid);

        } else {
            jQuery("#avvaiable_positions").appendTo("#avvaiable-positions-content");
            jQuery("#avvaiable_positions_multi").val("").trigger("chosen:updated");
            jQuery(content_modified).hide();
            jQuery(content_modified).appendTo(parentid).promise().done(function () {
                jQuery.load_content_inslider(configid);
                jQuery.active_default_device_width(configid);
            });


        }
    }

}

jQuery.open_slider_from_func = function open_slider_from_func(edit_options){
    var configid = jQuery(edit_options).data("configid");

    var state_slider = jQuery("#layout-slider-block").data("state");
    var slider_width = jQuery("#layout-slider-block").data("width");
    if (state_slider == "close") {
        jQuery.load_content_inslider(configid);
        jQuery.active_default_device_width(configid);
        jQuery.open_slider(slider_width);

    } else if (state_slider == "open") {

        jQuery.return_content_alposto(configid, slider_width);

    }
    //jQuery(".edit-options").stop();
    jQuery(".editable_positions").val("#" + jQuery(edit_options).attr("id")).trigger("chosen:updated");
}


jQuery.config_layout_slider = function config_layout_slider(openslider, closeslider) {


    jQuery(closeslider).click(function () {
        var slider_width = jQuery("#layout-slider-block").data("width");
        var configid = "#" + jQuery("#layout-slider-block").find(".ml-configs").attr("id");

        jQuery.return_content_alposto(configid, slider_width);
        jQuery(".editable_positions").val("editposition").trigger("chosen:updated");
    });

    jQuery(openslider).click(function () {
        var configid = jQuery(this).data("configid");
        var state_slider = jQuery("#layout-slider-block").data("state");
        var slider_width = jQuery("#layout-slider-block").data("width");
        if (state_slider == "close") {
            jQuery.load_content_inslider(configid);
            jQuery.active_default_device_width(configid);
            jQuery.open_slider(slider_width);

        } else if (state_slider == "open") {

            jQuery.return_content_alposto(configid, slider_width);

        }
        jQuery(".edit-options").stop();
        jQuery(".editable_positions").val("#" + jQuery(this).attr("id")).trigger("chosen:updated");
    });
};

jQuery.layout_slider_onload = function open_config() {
    function height_of_slider() {
        var h = jQuery(window).height();
        jQuery("#layout-slider-block").css({"height": h});
        jQuery("#load-slider-content").css({"min-height": "110%"});
    }
    function width_of_slider() {
        if (jQuery.ifmobile() == true) {
            var w = jQuery(window).width();
        } else {
            var w = jQuery("#layout-slider-block").width();
        }

        jQuery("#layout-slider-block").css({"width": w}).data("width", w);
    }

    height_of_slider();
    width_of_slider()
    jQuery(window).resize(function () {
        height_of_slider();
        width_of_slider();

    });
};