jQuery.enable_life_edit_oncontents = function(){
    var style;
    jQuery(".editable_positions").find("option").each(function(){
        var openoptions = jQuery(this).val();
        var element = openoptions.replace("#openoptions-","");
        switch (element){
            case "ml_body":style='position:absolute;top:0;left:0;color:#0d6efd;cursor:pointer;font-size: 16px;text-align: center;z-index:999';break;
            case "header_section":style='position:absolute;top:0;right:0;color:#0d6efd;cursor:pointer;font-size: 16px;text-align: center;z-index:999';break;
            default: style='position:relative;color:#0d6efd;cursor:pointer;font-size: 16px;width:100%;text-align: center;';
        }
        jQuery("#mainframe").contents().find("#"+element).prepend('<div  style="'+style+'" class="edit-options d-flex justify-content-center" data-element="'+openoptions+'"><div style="background-color: #ccc;width: 200px;"><span class="icon-apply"></span><span style="color:#000;"> '+element.replace("_section"," section").replace("_subsection"," section").replace("ml_","")+'</span></div></div>');
        jQuery("#mainframe").contents().find("#"+element).find(".edit-options").click(function(){
            var data_funcs = jQuery(openoptions).attr("data-funcs");
            if (data_funcs == "nofuncs") {
                jQuery.load_position_config_on_request(openoptions);
            } else {
                jQuery.open_slider_from_func(openoptions);
            }
        });
    });
    var active = jQuery(".editable_positions").val();
    if(active != "editposition"){
        var active_id = active.replace("openoptions-","");
        if(jQuery("#mainframe").contents().find(active_id).length > 0){
            var tops = jQuery("#mainframe").contents().find(active_id).position().top;
            if(tops > jQuery(window).height() - 100) {
                jQuery("#mainframe").contents().scrollTop(tops - 70);
            }
        }
    }
};

jQuery.iframeload = function iframeload() {
        jQuery("#mainframe").animate({opacity: 1}, 300);
        jQuery.enable_life_edit_oncontents();

};
jQuery.loadframe = function loadframe() {
    var juri = jQuery("#ml-uri").val().replace("/administrator","");
    jQuery("#layout-life-mode").html('<iframe onload="jQuery.iframeload()"   id="mainframe"  src="'+juri+'" ></iframe>');
};
jQuery.mainframe_position = function () {
    var h = jQuery(window).height();
    jQuery("#mainframe").css({"height": h});
    var state_slider = jQuery("#layout-slider-block").data("state");
    var device_active = jQuery("#devices-view").attr("data-active");
    jQuery("#layout-life-mode").removeAttr("style");
    if (state_slider == "close") {
        jQuery("#layout-life-mode").css({"width":"100%","margin-left":"-0.75rem"})
        if(device_active == "view-desktop"){
            jQuery("#mainframe").css({"width":"100%"});
        }
        else if(device_active == "view-tablet"){
            jQuery("#mainframe").css({"width":"800px"});
        }
        else{
            jQuery("#mainframe").css({"width":"340px"});
        }
    }
    else {
        var ww = jQuery(window).width();
        var layout_slider_w = jQuery("#layout-slider-block").width();
        jQuery("#layout-life-mode").css({"width":ww-layout_slider_w +10,"left":layout_slider_w+50})
        if(device_active == "view-desktop"){
            jQuery("#mainframe").css({"width":ww-layout_slider_w + 10});
        }
        else if(device_active == "view-tablet"){
            jQuery("#mainframe").css({"width":"800px"});
        }
        else{
            jQuery("#mainframe").css({"width":"340px"});
        }
    }
}
jQuery.layout_life_run = function () {
    jQuery("#layout-life").click(function () {
        if (jQuery(this).prop('checked')) {
            if (!jQuery(".menu-action").hasClass("closed")) {
                jQuery.close_menu();
                jQuery(".menu-action").addClass("closed-by-sistem");
            }
            jQuery.loadframe();
            jQuery.mainframe_position();
            jQuery("#tm-layout, .layout-lift-section, .layout-lift-block").hide();
        } else {
            jQuery("#mainframe").remove();
            jQuery("#tm-layout, .layout-lift-section, .layout-lift-block").show();
            if (jQuery(".menu-action").hasClass("closed-by-sistem")) {
                jQuery(".menu-action").removeClass("closed-by-sistem");
                jQuery(".menu-action").click();
            }
        }
    });
    jQuery.update_life = function() {
        if (jQuery("#layout-life").prop("checked")) {
            var out = jQuery.get_serialized("template.updatelife");
            var token = jQuery('#hidden-token').find("input").attr("name");
            var juri = jQuery("#ml-uri").val().replace("/administrator");
            jQuery.ajax({
                url: jQuery(location).attr('href') + '&' + token + '=1',
                dataType: "html",
                type: 'POST',
                cache: false,
                data: {task: "template.updatelife", postdata: out, hashdata: token},
                success: function (data) {
                    var current = document.getElementById("mainframe").contentWindow.location.href;
                    if (current.indexOf("?") >= 0) {
                        var query = "&tm-life=" + token;
                    } else {
                        var query = "?tm-life=" + token;
                    }
                    if (current.indexOf("tm-life=") < 0) {
                        current = current + query;
                    }
                    jQuery("#mainframe").css({"opacity": "0.3"});
                    jQuery("#mainframe").attr("src", current);

                }

            });
        }
    }

}