/*
 *************************************
 JMUERY SCRIPT FOR MARLEV TEMPLATE MANAGER - LAYOUT ADMIN PANEL JOOMLA TEMPLATE
 *************************************
 Copyright http://marlev.it
 *************************************
 GPL License
 *************************************
 */

jQuery.get_single_config = function get_configs(items, edit) {
    var json_items = JSON.stringify(items);
    jQuery.ajax({
        url: jQuery(location).attr('href'),
        dataType: "html",
        type: 'POST',
        cache: false,
         async:false,
        data: {posconfig: 1, tmconfig: json_items},
        success: function (data) {
            jQuery.elabora_risposta_post(data);
        },
        complete: function (data) {
        setTimeout(function(){
            jQuery.open_slider_from_func(edit);
            jQuery.enable_select_position_on_edit();
            jQuery(edit).html('<i class="icon-apply"></i>');
        },500);

        }
    });
};

jQuery.get_configs = function get_configs(items,percents = false) {
    var counter = 1;
    var sender = {};
    jQuery.each(items, function (index) {
        sender[index] = items[index];
        delete items[index];
        counter = counter + 1;
        if (counter == 20) {
            return false;
        }
    });
    var json_items = JSON.stringify(sender);
    jQuery.ajax({
        url: jQuery(location).attr('href'),
        dataType: "html",
        type: 'POST',
        cache: false,
        async:false,
        data: {posconfig: 1, tmconfig: json_items},
        success: function (data) {
            jQuery.elabora_risposta_post(data);
        },
        complete: function () {
            var rest = Object.keys(items).length;
            if (rest > 0) {
                if(percents != false)
                {
                    jQuery.run_status_bar(percents);
                }
                jQuery.get_configs(items,percents);
            }
            else {
                jQuery.enable_select_position_on_edit();
                if(percents != false)
                {
                    jQuery.run_status_bar(percents);
                    setTimeout(function(){
                        jQuery("#waitbox").remove();
                    },600);
                }

                setTimeout(function () {
                    jQuery.install_newinstallation();
                }, 1600);
            }
        }
    });
};
jQuery.install_ml_template = function install_ml_template() {


    var items = {};
    jQuery(".options-block").each(function () {
        var subpos = jQuery(this).data("subpos");
        var sender = {};
        sender["parentid"] = jQuery(this).data("parentid");
        sender["subpos"] = subpos;
        sender["selected"] = jQuery(this).data("selected");
        sender["subposdata"] = jQuery(this).data("subposdata");
        items[subpos] = sender;
    });
    var conter = Object.keys(items).length / 20;
    // if(!Number.isInteger(conter)){
    //     conter = parseInt(conter) + 1;
    // }
   var percents = Math.round(100 / conter);
    jQuery.get_configs(items,percents);
    jQuery.template_layout();


    //salva la configurazione esistente subito dopo installazione dle template

};


jQuery.template_layout = function template_layout() {
    jQuery(".edit-options").click(function (e) {
        var data_funcs = jQuery(this).attr("data-funcs");
        if (data_funcs == "nofuncs") {
       // if (!jQuery(this).hasClass("func-assigned")) {
            jQuery.load_position_config_on_request(jQuery(this));

        }
    });

};

jQuery(document).ready(function () {
    jQuery.coldelete();
    jQuery.addposition();
    jQuery.suspend_positions();
    jQuery.width_large();
    //jQuery.config_layout_slider(".edit-options", ".close-slider");
    jQuery.lift_scroll();
    jQuery.submit_form();
    jQuery.show_alert_message();
    //jQuery.append_tabscript(data);
    jQuery.suspend_positions_onload();
    jQuery.enable_tooltip_in_actions();
    jQuery.show_suspend_section();
    jQuery.device_on_layout_click();
    //jQuery.enable_position_on_selects();
    //jQuery("#waitbox").remove();
    //jQuery("#waitbox").remove();
    jQuery.enable_position_on_selects();
    jQuery.enable_select_position_on_edit();
    jQuery.layout_slider_onload();
    jQuery.layout_life_run();


});
