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

var JM = jQuery.noConflict();
JM(document).ready(function () {
    JM(".header, .subhead").remove();


//    function fix_menu() {
//        var menu_width = JM(".menu-container").width();
//        JM(".marlev_menu").css({"width": menu_width});
//   
//        
//    }
//  fix_menu();
    // jQuery(window).resize(function () {
    //  fix_menu();
    // });


    //JM("#params_SetFont").trigger("liszt:updated");


    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");
        if (curent === "tm-layout") {
            jQuery.marlev_show_buttons(".layout-lift");
        } else {
            jQuery.marlev_hide_buttons(".layout-lift");
        }

        hideBlocks();
        var showblock = JM(".marlev_manager").find("." + curent);
        // var hblock = showblock.css("height", showblock.height());

        showblock.fadeIn().promise().done(function(){
              jQuery.run_fixed_menu();
        });

        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();
        //  console.log(type)
        var action = false;
        jQuery(val).each(function (key, value) {
            if (jQuery.isArray(type)) {
               jQuery(type).each(function (a, v) {
                    if (v == value) {
                        action = true;
                    }
                });

            } else {
                if (type === value) {
                    action = true;
                }
            }
        });
        if (action === true) {
            jQuery(class_action).show();//.fadeOut();//.show("slide", {direction: "up"}, 700);
        } else {
            jQuery(class_action).hide();//.fadeOut();//.hide("slide", {direction: "up"}, 700);
        }
        JM(id_change).on('change', function () {
            callChange(id_change, val, class_action);
        });
    }
    callChange('#params_show_logo_slide', ['show'], ".slidelogo");
    callChange('#params_socialtype', ['font'], ".socialfontsize");
    callChange('#params_offline_counter', ['1'], ".offline_whensiteonline");
    callChange('#params_socialpositions', ['socialleft', 'socialright'], ".socialsfondo");
 


    function slide_menu() {
        function show_hide_li() {
            var type = JM('#params_pcmenulayout').val();
            if (type === "slide") {
                jQuery(".for_slide_menu_only").each(function () {
                    jQuery(this).show("slide", {direction: "left"}, 300);
                });

            } else {
                jQuery(".for_slide_menu_only").each(function () {
                    jQuery(this).hide("slide", {direction: "left"}, 300);
                });

            }
        }

        var ul = JM(".tm-menu").find("#myTab-menuTabs");
        var c = 1;
        JM(ul).find("li").each(function () {

            if (c == 2 || c == 3) {
                jQuery(this).addClass("for_slide_menu_only");
            }
            c = c + 1;
        });
        ;
        show_hide_li();
        JM('#params_pcmenulayout').on('change', function () {
            show_hide_li();
        });

    }
    setTimeout(function () {
        slide_menu();
    }, 500);



    function logo_menu_slide_width() {
        var _parent = jQuery("#params_slidelogowidth").parents(".slidelogo");
        var slider_width = jQuery(".tm-menu").width() - _parent.find(".control-label").width() - 100;
        jQuery("#width-slide-logo-example").css({"width": slider_width});

        function run_width() {
            var block_w = jQuery("#width-slide-logo-example").width();
            var get_width = jQuery("#params_slidelogowidth").val() * 10;
            var logo_width = block_w * get_width / 100;
            var menu_width = block_w - logo_width;
            jQuery("#width-slide-logo").animate({"width": logo_width}, {duration: 300, queue: false});
            jQuery("#width-slide-logo-menu").animate({"width": menu_width, "marginLeft": logo_width + 6}, {duration: 300, queue: false});

        }
        run_width();
        jQuery("#params_slidelogowidth").on('change', function () {
            run_width();
        });

    }
    logo_menu_slide_width();

    function allineamento_logo() {
        function run() {
            var value = jQuery("#params_logoslideralign").val();
            var align;
            switch (value) {
                case "center":
                    align = "center";
                    break;
                case "left":
                    var align = "left";
                    break;
                case "right":
                    var align = "right";
                    break;
            }
            jQuery("#width-slide-logo").css({"textAlign": align});
        }
        run();
        jQuery("#params_logoslideralign").on('change', function () {
            run();
        });
    }
    allineamento_logo();

    function logo_slide_effect_demo() {
        jQuery("#params_slidelogoeffect").on("change", function () {
            var val = jQuery(this).val();
            jQuery("#logoslide-example-effect").removeClass().addClass(val + ' animated').one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function () {
                jQuery(this).removeClass();
            });
        });

    }
    function logo_slide_effect_speed() {
        //slidelogospeed
        jQuery("#params_slidelogospeed").on("change", function () {
            var val = jQuery(this).val();
            jQuery("#logoslide-example-effect").attr("style", "animation-duration:" + val + "s;", "-webkit-animation-duration:" + val + "s;");
            var effect = jQuery("#params_slidelogoeffect").val();
            jQuery("#logoslide-example-effect").removeClass().addClass(effect + ' animated').one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function () {
                jQuery(this).removeClass();
            });
        });
    }
    logo_slide_effect_speed();
    logo_slide_effect_demo();

    jQuery(".layout-lift").find("select").each(function () {
        jQuery(this).chosen({"search": true});
    });


function update_curent_time(){
    jQuery(".udpate-date").click(function(e){
        e.preventDefault();
         jQuery.ajax({
        url: jQuery(location).attr('href'),
        dataType: "html",
        type: 'POST',
        cache: false,
        data: {updatetime: 1},
        success: function (data) {
            //  var response = data;

            var responseval = jQuery(data).filter('#update-time');

            jQuery(".curent-server-time").html(responseval).promise().done(function () {
               setTimeout(function(){
                    jQuery(".curent-server-time").find("span").animate({"color":"#000"});
               },700);

            });

        }
    });
        
        
    });
}

update_curent_time();

});