/*
 *************************************
 JjQueryUERY SCRIPT FOR jQueryARLEV TEjQueryPLATE jQueryANAGER - LAYOUT ADjQueryIN PANEL JOOjQueryLA TEjQueryPLATE
 *************************************
 Copyright http://marlev.it
 *************************************
 GPL License
 *************************************
 */


//funzione serve per vedere quanti checkbox sono stati selezionati, se piu di 1 pilsate carica si disabilita
jQuery.loading_wait = function loading_wait() {
    jQuery("body").append('<div id="waitbox" class="modal-backdrop fade in"><div class="waitbox"><div class="wait"><span class="wait-load">Loading</span><br/><i style="margin-top:20px;"class="fa fa-spinner fa-pulse fa-3x fa-fw"></i></div></div><div>');

};

jQuery.presetsChecked = function presetsChecked(element) {


    var checked = jQuery(".tm-preset-action:checked").length;
    if (checked == 1) {
        jQuery(".load-preset, .delete-presets").removeClass("disabled");

    } else if (checked > 1) {
        jQuery(".load-preset").addClass("disabled");
    } else {
        jQuery(".load-preset, .delete-presets").addClass("disabled");
    }

};


jQuery.show_alert_message = function show_alert_message() {


    // setTimeout(function () {
    var timer = 500;

    jQuery("#alert-box").find(".alert").each(function () {
        if (!jQuery(this).hasClass("alert-show")) {

            var element = jQuery(this);
            var this_height = jQuery(element).innerHeight() + 10;
            jQuery(this).addClass("alert-show").data("height", this_height);
            jQuery(element).data("height", this_height);
            //  setTimeout(function () {
//             if (jQuery(element).hasClass("install-layout")) {
//                jQuery(element).show();
//                setTimeout(function () {
//                    jQuery.hide_alert_message(jQuery(element));
//                }, 2500);
//            } else {
            var setint = setInterval(function () {
                var get_height = jQuery("#alert-box").data("height");
                if (get_height == 0) {
                    clearInterval(setint);
                    get_height = this_height + get_height;
                    jQuery("#alert-box").animate({"height": get_height}, 500);
                    jQuery(element).show("slide", {direction: "up"}, 500);
                    jQuery("#alert-box").data("height", get_height);
                    jQuery(jQuery(element).find(".close")).one("click", function () {
                        jQuery.hide_alert_message(jQuery(element));
                    });

                    if (jQuery(element).hasClass("alert-success")) {

                        setTimeout(function () {
                            jQuery.hide_alert_message(jQuery(element));
                        }, 2500);
                    } else {
                        setTimeout(function () {
                            jQuery.hide_alert_message(jQuery(element));
                        }, 7000);
                    }
                }
            }, 800);

            //}, timer);
            // timer = timer + 1500;

        }
    });

    // }, 500);
};
jQuery.hide_alert_message = function hide_alert_message(element) {

    var ths_height = jQuery(element).data("height");
    var ths_whidth = jQuery(element).width();
    var box_height = jQuery("#alert-box").data("height");
    var new_height = box_height - ths_height;
    var new_height_adapt = new_height;


    jQuery(element).css({"z-index": 1}).animate({"marginTop": -ths_height, "opacity": 0, "opacity": 0}, 300).promise().done(function () {
        jQuery(element).remove();
    });
    jQuery("#alert-box").animate({"height": new_height_adapt}, 300).promise().done(function () {
        if (new_height == 25) {
            new_height = 0;

        }
        jQuery("#alert-box").data("height", new_height);

    });


}
jQuery.width_tms = function width_tms() {
    var home_width = jQuery(".tm-home").width();
    jQuery("#alert-box").css({"width": home_width});
    jQuery(".marlev-ulmenu").find("li").each(function () {
        var get_class = jQuery(this).attr("data");
        jQuery("." + get_class).css({"width": home_width}).data("width", home_width);
    });
};

jQuery.create_tabs_script = function create_tabs_script(data) {
    var ul = jQuery("#myTab-minifierTabs");
    jQuery(data).find(".tab-pane").each(function () {
        var params = jQuery(this).find(".params-block");
        var type = params.data("type");
        var active = params.data("active");
        var id = jQuery(this).attr("id");
        jQuery(ul).append(jQuery("<li class=\"" + active + "\"><a href=\"#" + id + "\" data-toggle=\"tab\">" + type + "<\/a><\/li>"));
    });
};

jQuery.bootstrap_button_after_ajax = function bootstrap_button_after_ajax(element) {
    var attr_for = jQuery(element).attr("for");

    if (!jQuery("#" + attr_for).is(":checked")) {
        jQuery(element).addClass("btn active btn-success working");
        jQuery("#" + attr_for).attr("checked", true);
        jQuery(element).parents(".btn-group").find("label").each(function () {
            if (!jQuery(this).hasClass("working")) {
                jQuery(this).removeClass("btn active btn-success").addClass("btn");
                var this_for = jQuery(this).attr("for");
                jQuery("#" + this_for).attr("checked", false);
            }
        });
        jQuery(element).removeClass("working");
    }

};
jQuery.marlev_show_buttons = function show_buttons(element) {
    if (jQuery(element).hasClass("btns-closed")) {
        jQuery(element).toggle("slide", {direction: "right"}, 300);
        jQuery(element).removeClass("btns-closed");

    }
};
jQuery.marlev_hide_buttons = function hide_buttons(element) {

    if (!jQuery(element).hasClass("btns-closed")) {
        jQuery(element).toggle("slide", {direction: "right"}, 150);
    }
    jQuery(element).addClass("btns-closed");
};

jQuery.button_save_fixed = function button_save_fixed() {

    function run() {
        var scroll_top = jQuery(window).scrollTop();

        if (scroll_top > 160) {
            jQuery.marlev_show_buttons("#ml-save-fixed");
        } else if (scroll_top < 140) {
            jQuery.marlev_hide_buttons("#ml-save-fixed");
        }
    }
    jQuery(window).scroll(function () {
        run();
    });
    run();

    //ml-save-fixed
};
jQuery.run_fixed_menu = function run_fixed_menu(){
      var ws = jQuery(window).scrollTop();
        var active = jQuery(".marlev-ulmenu").find(".m-active").attr("data");
        var active_h = jQuery("."+active).outerHeight() + jQuery("."+active).offset().top;
       
        var header_h = jQuery(".header_mmenu").data("height");
        if (ws >= 53 && active_h > jQuery(window).height()) {
            jQuery(".header_mmenu").css({"height":header_h+37});
            jQuery(".marlev_menu").css({"position":"fixed","top":"31px","height":1200}).addClass("positioned");
        } 
        else if(active_h <= jQuery(window).height()){
            jQuery(".header_mmenu").css({"height":header_h});
             jQuery(".marlev_menu").removeAttr("style").css({"position":"relative","width":jQuery(".marlev_menu").data("width"),"height":jQuery(window).height()- 80});
        }
        
        else if (ws <= 52 && jQuery(".marlev_menu").hasClass("positioned")) {
            jQuery(".header_mmenu").css({"height":header_h});
            jQuery(".marlev_menu").removeAttr("style").css({"position":"relative","height":1200,"width":jQuery(".marlev_menu").data("width")}).removeClass("positioned");
           // jQuery(".marlev_menu").removeClass("header_mmenu_fixed");

        }
};
jQuery.fixed_menu = function fixed_menu() {
    var h = jQuery(document).height();
    var wh = jQuery(window).height() - 97;
    var menu_width = jQuery(".marlev_menu").width();
    var tms = jQuery(window).width() - menu_width;
    jQuery("#tms-container").css({"width": tms - 50, "margin-left": menu_width});

    jQuery(".menu-container").css({"width": menu_width});
     
    jQuery(".header_mmenu").css({"height": wh, "width": menu_width}).data("height", wh).data("width", menu_width);
    var ul_height = jQuery(".header_mmenu").find("ul").height() + 50
    if (ul_height > wh) {
        jQuery(".header_mmenu").css({"overflow-y": "scroll"});
    }
    jQuery(".marlev_menu").css({"width":jQuery(".header_mmenu").find(".marlev-ulmenu").width()}).data("width",jQuery(".header_mmenu").find(".marlev-ulmenu").width());

    function run() {
        //var qa = 30 + jQuery("#ml-head").height();
      
    }
    jQuery.run_fixed_menu();
    jQuery(window).scroll(function () {

        jQuery.run_fixed_menu();
    });


};
jQuery.rgb_colors = function rgb_colors() {
    setTimeout(function () {
        jQuery(".mlrgb-color").each(function () {
            jQuery(this).minicolors({theme: "bootstrap", opacity: true, control: 'hue', format: 'rgb'});
        });
    }, 1000);

};
jQuery(document).ready(function () {
    jQuery.fixed_menu();
    jQuery.loading_wait();
    jQuery(window).resize(function () {
        jQuery.fixed_menu();
    });
    //jQuery.width_tms();

    jQuery(".tm-preset-action").on("click", function () {
        jQuery.presetsChecked(jQuery(this));
    });
    jQuery.button_save_fixed();
    jQuery.rgb_colors();

});