/*
 *************************************
 JMUERY SCRIPT FOR ENIGMA ADMIN PANEL JOOMLA TEMPLATE
 *************************************
 Copyright http://marlev.it
 *************************************
 GPL License
 *************************************
 */

var JM = jQuery.noConflict();
JM(document).ready(function () {
    JM("#params_SetFont").trigger("liszt:updated");

    function moveMenu() {
        JM(".marlev_menu").insertBefore(".container-main");
    }
    moveMenu();
    function hideBlocks() {
        JM(".marlev-ulmenu li").each(function () {
            if (!JM(this).hasClass("m-active")) {
                var hideclass = JM(this).attr("data");
                JM(".marlev_manager").find("." + hideclass).hide();
            }
        });
    }

    hideBlocks();

    JM(".marlev-ulmenu").children("li").click(function () {

        JM(this).parent().find("li").each(function () {
            if (JM(this).hasClass("m-active"))
                JM(this).removeClass("m-active");
        });

        JM(this).addClass("m-active");
        var curent = JM(this).attr("data");
        hideBlocks();
        var showblock = JM(".marlev_manager").find("." + curent);
        var hblock = showblock.css("height", showblock.height() + 100);

        showblock.fadeIn();
        JM(".tm-active-menu").val(curent);
        //JM("#system-message-container").hide();
    });

//funzione per riapertura della voce del menu dopo il salvataggio
    function openItemMenu() {
        var curent = JM(".tm-active-menu").val();

        JM(".marlev-ulmenu").find("li").each(function () {
            var menudata = JM(this).attr("data");
            if (curent == menudata)
                JM(this).click();
            return true;
        });

    }
    openItemMenu();
//disattivazione home se e' impostato di default
    var home = JM("#params_home").val();
    if (home == 1) {
        JM("#params_home").attr("disabled", true);
        if (JM.fn.chosen) {
            JM("#params_home").trigger("liszt:updated");
        }
        JM("#params_home").parent().append('<input type="hidden" name="params[home]" value="1" />')
    }

    function placeHolder() {
        JM("#params_linkcopy").attr("placeholder", "http://sitename.com");
        JM("#params_copy").attr("placeholder", "Copyright here...");
    }
    placeHolder();


//Autoclose confirm message
    if (JM(".close").length > 0) {
        setTimeout(function () {
            JM(".close").click();
        }, 3000);
    }//end

    function callChange(id_change, val, class_action) {
        var type = JM(id_change).val();
        if (type == val) {
            JM(class_action).slideDown();
        }
        else {
            JM(class_action).slideUp();
        }
        JM(id_change).on('change', function () {
            callChange(id_change, val, class_action);
        });
    }

    callChange('#params_menu_type', "slide", ".for_slide_menu_only");
    callChange('#params_headerimage', "none", ".header-color");
    callChange('#params_headerimage', "custom", ".header-custom-image");
    callChange('#params_menuposition', "in_top", ".logo-position");

    function remove_c_install_messagge() {
        var n = JM(".alert").find("p").length;
        if (n == 2) {
            JM(".alert").find("p:last").remove();
        }
    }
    remove_c_install_messagge();


//funzione serve per vedere quanti checkbox sono stati selezionati, se piu di 1 pilsate carica si disabilita
    function presetsChecked() {
        JM(".tm-preset-action").click(function () {
            var checked = JM(".tm-preset-action:checked").length;
            if (checked == 1) {
                JM(".load-preset, .delete-presets").removeClass("disabled");

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

        });
    }
    presetsChecked();

    function open_config() {
        JM(".ckick-to-open").click(function () {
            var box = JM(this).parent().find(".box-mcont");
            if (JM(box).hasClass("boxopened")) {
                JM(box).slideUp();
                JM(box).removeClass("boxopened");
            }
            else {
                JM(box).slideDown();
                JM(box).addClass("boxopened");
            }
        });
    }
    open_config();

    function first_config_isopen() {
        JM(".marlev-ulmenu").find("li").each(function () {
            var get_class = "." + JM(this).attr("data");
            JM(get_class).find(".box-mcont:first").addClass("boxopened").slideDown();
        });

    }
    first_config_isopen();
});