/*
 *********************************
 *********************************
 Non modificare il seguente codice
 *********************************
 *********************************
 *Do not modify
 *********************************
 * @extension       Marlev Templates
 * @author          Lev Milicenco
 * @link            http://www.marlev.it
 * @copyright       Copyright 2017 marlev.it - Itroom srls All Rights Reserved
 */
var deviceDetector = function () {
    var b = navigator.userAgent.toLowerCase(), a = function (a) {
        void 0 !== a && (b = a.toLowerCase());
        return/(ipad|tablet|(android(?!.*mobile))|(windows(?!.*phone)(.*touch))|kindle|playbook|silk|(puffin(?!.*(IP|AP|WP))))/.test(b) ? "tablet" : /(mobi|ipod|phone|blackberry|opera mini|fennec|minimo|symbian|psp|nintendo ds|archos|skyfire|puffin|blazer|bolt|gobrowser|iris|maemo|semc|teashark|uzard)/.test(b) ? "phone" : "desktop"
    };
    return{device: a(), detect: a, isMobile: "desktop" != a() ? !0 : !1, userAgent: b}
}();

jQuery.accept_cookie = function accept_cookie() {
    function cookie_click() {
        jQuery(".button-cookie-accept").click(function (event) {
            event.preventDefault();
            jQuery.ajax({

                url: location,
                dataType: "html",
                type: 'POST',
                cache: false,
                data: {accettocookie: "cookie-consentiti"}
            });

            if (jQuery(".cookie-block").hasClass("bottom")) {
                jQuery(".cookie-block").addClass("animated slideOutDown");


            } else {
                jQuery(".cookie-block").addClass("animated slideOutUp");
            }
            setTimeout(function () {
                jQuery(".cookie-block").remove();
            }, 900);

        });

    }
    function cookie_show() {
        var ch = jQuery(".cookie-block").height();
        jQuery(".cookie-block").hide();
        if (jQuery(".cookie-block").hasClass("bottom")) {
            jQuery(".cookie-block").css({"bottom": -ch}).show().promise().done(function () {
                jQuery(".cookie-block").animate({"bottom": 0}, 500);
            });
        } else {
            jQuery(".cookie-block").css({"top": -ch}).show().promise().done(function () {
                jQuery(".cookie-block").animate({"top": 0}, 500);
            });

        }
    }


    cookie_click();
    cookie_show();

};
jQuery.marlev_img_resize = function img_resize() {
    var mobile = jQuery(window).width();

    if (mobile <= 750) {
        jQuery("img").each(function () {
            var img_w = jQuery(this).width();
            var adapt = mobile - 50;
            if (img_w >= adapt) {
                jQuery(this).css({"width": "100%"});
            } else {
                jQuery(this).css({"width": "auto"});
            }
        });
    }
};
//Accordion menu
jQuery.marlev_accordion_menu = function AccordionMenu(moduletable_accordion, deeper) {

    jQuery(moduletable_accordion).find(deeper).each(function () {
        var ul = jQuery(this).children(".nav-child");

        var clone = jQuery(this).children("a").clone();
        clone.prependTo(ul).wrap('<li class="moved-item"></li>');

        jQuery(this).children("a").attr("href", "#").addClass("yr-accordion");
        jQuery(this).children("span").addClass("yr-accordion");
        jQuery(this).addClass("yr-parent-accordion");
        if(jQuery(this).hasClass("active")){
            jQuery(this).children("ul").slideToggle();
        }
    });

    jQuery(moduletable_accordion).find("li").click(function (event) {
        event.stopPropagation();
        if (jQuery(this).hasClass("yr-parent-accordion")) {
            event.preventDefault();
            jQuery(this).children("ul").slideToggle();
        }

    });

};
//fine accordion

//Slide menu


jQuery.marlevSlideMenu = function marlevSlideMenu(ml_slidelogo_params) {
//ml_slidelogo_params - come from template_inc.php on run this function
//define logo end menu width in slide menu
    function logo_menuwidth(ml_slidelogo_params) {
        var return_data = {
            "logo": "",
            "menu_cols": "col-md-12 col-lg-12 col-xl-12 col-xxl-12"
        };
        var logo_insert;
        if (ml_slidelogo_params['show_logo_slide'] === "show") {
            var define_menu_cols = 12 - ml_slidelogo_params['slidelogoheight'];
            return_data['menu_cols'] = 'p-0  col-lg-' + define_menu_cols+' col-xl-' + define_menu_cols + ' col-xxl-' + define_menu_cols;;
            logo_insert = '<div class="p-0  col-lg-' + ml_slidelogo_params['slidelogoheight'] + ' col-xl-' + ml_slidelogo_params['slidelogoheight'] + ' col-xxl-' + ml_slidelogo_params['slidelogoheight'] +'">';
            var slide_effect = '';
            if (ml_slidelogo_params['slidelogoeffect'] != "null") {
                slide_effect = ml_slidelogo_params['slidelogoeffect'] + ' animated';
            }
            logo_insert = logo_insert + '<div class="append-logo-slide ' + slide_effect + '">';
            logo_insert = logo_insert + '<img src="' + ml_slidelogo_params['slidelogo'] + '"height="' + ml_slidelogo_params['slidelogoheightimg'] + '" width="' + ml_slidelogo_params['slidelogowidthimg'] + '" style="margin-left:' + ml_slidelogo_params['slidelogomarginleft'] + 'px; margin-top:' + ml_slidelogo_params['slidelogomargintop'] + 'px;" /></div></div>';
            return_data['logo'] = logo_insert;
        }

        return return_data;
    }




    //run main function
    if (jQuery('#logo_menu_subsection').find('#menu').length > 0) {
        var menu_element = '#menu';

        var menuposition = jQuery(menu_element).offset().top;
        var wh = jQuery(window).scrollTop();

        if (wh > menuposition && jQuery(".ml-menu-fixed").length === 0) {
            var remain = jQuery(document).height() - jQuery(window).height();
            var menu_height = jQuery(menu_element).outerHeight();
            if (remain > 200 && wh > menu_height) {

                // var menu_cols = 'col-md-12 col-lg-12';
                var logo_menu_data = logo_menuwidth(ml_slidelogo_params);
                var clone_menu = jQuery("#ml-navigation-menu").find(".mod-menu").clone();
                jQuery(menu_element).before('<div id="ml-navigation-menu-slide" class="d-none d-lg-block"><div class="'+ml_slidelogo_params['slidelogocontainer']+'"><div class="row">' + logo_menu_data['logo'] + '<div class="' + logo_menu_data['menu_cols'] + ' append-menu-slide"></div></div></div></div>');
                jQuery(clone_menu).appendTo(".append-menu-slide");
                jQuery("#ml-navigation-menu-slide").addClass("fadeInDown animated");
                jQuery("#ml-navigation-menu-slide").addClass('ml-menu-fixed');

            }
        } else if (jQuery(".ml-menu-fixed").length) {
            var replaced_menu = jQuery('#menu').offset().top + 50;

            if (wh <= replaced_menu) {
                jQuery(".ml-menu-fixed").remove();
            }
        }

    }

};


//end slide menu

//activate animation
jQuery.marlev_run_animation = function marlev_run_animation() {

    function element_animation(element, when, device) {
        var window_height = jQuery(window).height();
        var scroll = jQuery(window).scrollTop() + window_height;
        var element_height = jQuery(element).height();

        if (element_height >= window_height - 100) {
            var element_position = jQuery(element).offset().top + 100;
        } else {
            element_height = 20;
            var element_position = element_height + jQuery(element).offset().top;
        }
        if (scroll >= element_position) {

            var effects = jQuery(element).data("mleffect");
            var delay_animation = 0;
            switch (device) {
                case "phone":
                    delay_animation = jQuery(element).data("delaymobile") * 1000;
                    break;
                case "tablet":
                    delay_animation = jQuery(element).data("delaytablet") * 1000;
                    break;
                case "desktop":
                    delay_animation = jQuery(element).data("delaypc") * 1000;
                    break;
            }
            setTimeout(function () {
                jQuery(element).removeClass("ml-effect-true");
            }, delay_animation);

            jQuery(element).addClass(effects).one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function () {

                jQuery(element).off("scroll", jQuery(element));
            });
        }

    }

    function call_element_scroll(element, device) {
        jQuery(window).scroll(function () {
            element_animation(element, "scroll", device);
        });
    }

    var device = deviceDetector.device;
    jQuery(".ml-effect-true").each(function () {

        element_animation(jQuery(this), "onload", device);
        jQuery(this).on("scroll", call_element_scroll(jQuery(this), device));

    });


};
//end activate animation

//Mobile menu
jQuery.marlev_mobile_menu = function mobilemenu() {

    //open menu
    jQuery("#ml-mobile-menu-button").click(function () {
        var mobile = jQuery(document).height();
        var mobilen = jQuery(window).height();
        var mwidth = jQuery(window).width();
        var nav_h = jQuery(".menumobile-block").find(".mod-menu").height();//menu

        jQuery(".menumobile-block").css({"height": nav_h + 70});
        jQuery("#ml-mobile-menu-box").css({"height": mobilen, "width": mwidth});
        jQuery("#ml-mobile-menu-box").addClass("fadeInLeft animated opened-mobile-menu").show();
        //jQuery("body").addClass("body_mobile_fixed");
    });
    //close menu
    jQuery("#close-marlev-mobile").click(function () {
        var box_width = jQuery("#ml-mobile-menu-box").width();
        jQuery("#ml-mobile-menu-box").animate({"left": -box_width}).promise().done(function () {
            jQuery(this).removeClass("fadeInLeft animated opened-mobile-menu").removeAttr("style");
            jQuery(".menumobile-block").removeAttr("style");
        });
    });

};

jQuery.mobile_menu_adapter_resize = function mobile_menu_adapter_resize() {
    jQuery(window).resize(function () {
        if (jQuery("#ml-mobile-menu-box").hasClass("opened-mobile-menu")) {
            var mobile = jQuery(document).height();
            var mobilen = jQuery(window).height();
            var mwidth = jQuery(window).width();
            var nav_h = jQuery(".menumobile-block").find(".mod-menu").height();//menu

            jQuery(".menumobile-block").css({"height": nav_h + 70});
            jQuery("#ml-mobile-menu-box").css({"height": mobilen, "width": mwidth});
        }
    });
};

jQuery.article_info = function article_info(btn) {
    var window_width = jQuery(window).height();
    if(window_width < 1024) {
        //Edit article, module add button
        jQuery(".icon-edit").parent("a").addClass("btn  " + btn);
        //articles info
        jQuery('.article-info-term').each(function () {
            var text = jQuery(this).text();
            var new_text = '<a href="#" class="btn ' + btn + ' show-art-info">' + text + '<i class="fa fa-expand"></i></a></a>';
            jQuery(this).html(new_text);

        });
        jQuery('.show-art-info').click(function (e) {
            e.preventDefault();
            var fa = jQuery(this).find(".fa");
            if (jQuery(fa).hasClass('fa-expand')) {
                jQuery(fa).removeClass('fa-expand').addClass("fa-compress");
            } else {
                jQuery(fa).removeClass('fa-compress').addClass("fa-expand");
            }
            jQuery(this).parents(".article-info").children("dd").each(function () {
                jQuery(this).slideToggle();
            });
        });
    }
};
jQuery.marlev_toup = function marlev_toup() {
    function show_toup(element) {
        if (jQuery(element).hasClass("toup-closed")) {
            jQuery(element).animate({"right": "0"}, 200);
            jQuery(element).removeClass("toup-closed");
        }
    }
    function hide_toup(element) {
        if (!jQuery(element).hasClass("toup-closed")) {
            jQuery(element).animate({"right": "-70px"}, 200);
            jQuery(element).addClass("toup-closed");
        }

    }
    ;
    function run() {
        var scroll_top = jQuery(window).scrollTop();
        if (scroll_top >= 160) {
            show_toup("#toup");
        } else if (scroll_top < 150) {
            hide_toup("#toup");
        }
    }
    jQuery(window).scroll(function () {
        run();
    });
    jQuery("#toup").click(function () {
        jQuery("html, body").animate({scrollTop: 0}, 300);
    });
    run();
};


jQuery.row_striped_height = function row_striped() {//fix height example category contats view
    if (jQuery(".row-striped").length) {
        jQuery(".row-striped").each(function () {
            jQuery(this).find(".row-fluid").each(function () {
                jQuery(this).append('<div class="clearfix" />');
            });
        });

    }
};
jQuery.table_responsive = function table_responsive() {
    if (jQuery("table.table").length) {
        jQuery("table.table").each(function () {
            jQuery(this).wrap('<div class="table-responsive"/>');
        });
    }
};

jQuery.fix_checkbox_sendmail_tome = function fix_checkbox_sendmail_tome(element) {

    if (jQuery(element).length) {
        jQuery(element).prependTo(jQuery(element).parents(".control-group").find(".control-label"));
        jQuery(element).parent().addClass("move-toleft");
    }



};
jQuery.fix_collapse = function fix_collapse(array) {

    function run(element, id) {
        if (jQuery(id).hasClass("in")) {
            jQuery(id).animate({"height": 0}, 700);
            setTimeout(function () {
                jQuery(id).removeClass("in")
            }, 700);

        } else {
            var h = jQuery(id).data("height");
            jQuery(id).css({"height": 0}).addClass("in");
            jQuery(id).animate({"height": h}, 700);

        }
    }

    jQuery(array).each(function (key, value) {
        if (jQuery(value).length) {
            jQuery(value).find("a").each(function () {
                var data_toogle = jQuery(this).data("toggle");
                if (data_toogle === "collapse") {
                    jQuery(document).off("click.collapse.data-api");
                    var id = jQuery(this).attr("href");
                    jQuery(id).data("height", jQuery(id).height());
                    jQuery(this).on("click", jQuery(this), function (e) {
                        e.preventDefault();
                        run(this, id);
                    });
                }
            });

        }
    });
};

jQuery.socialfixed_right = function socialfixed_right(array) {
    function get_height_width(element) {
        var returned = false;
        if (jQuery(element).find("i").length) {
            returned = {
                height: jQuery(element).find("i").height(),
                width: jQuery(element).find("i").width()
            };
        } else if (jQuery(element).find("img").length) {
            returned = {
                height: jQuery(element).find("img").height(),
                width: jQuery(element).find("img").width()
            };
        }
        return returned;
    }

    function define_max_w_h(position) {
        var ch = jQuery(position).children().length;
        var w = 0;
        var h = 0;
        var count = 1;
        jQuery(position).children().each(function () {
            if (count === 1) {
                jQuery(this).addClass("socialborder_first");
            }
            if (count === ch) {
                jQuery(this).addClass("socialborder_last");
            }
            count = count + 1;
            var height_width = get_height_width(this);
            jQuery(this).children().data("height", height_width['height']).data("width", height_width['width']);
            if (height_width['width'] > w) {
                w = height_width['width'];
            }


            if (height_width['height'] > h) {
                h = height_width['height'];
            }
        });
        h = h + 10;
        w = w + 10;
        jQuery(position).children().each(function () {
            var pad_top = (h - jQuery(this).children().data("height")) / 2;
            var options = {};
            options["width"] = w;
            options["height"] = h;
            options["padding-top"] = pad_top;

            jQuery(this).css(options).data("height", h).data("width", w);
        });
        var obj = {"height": h, "width": w, "counts": ch};
        return obj;
    }

    function on_hover(element, width_height) {
        var w = jQuery(element).data(width_height) + 5;
        var options = {};
        options[width_height] = w;
        jQuery(element).animate(options, 200);
        setTimeout(function () {
            jQuery(element).addClass("social_border");
        }, 50);
    }
    function off_hover(element, width_height) {
        var w = jQuery(element).data(width_height);
        var options = {};
        options[width_height] = w;
        jQuery(element).animate(options, 200);
        setTimeout(function () {
            jQuery(element).removeClass("social_border");
        }, 150);
    }


    function apear_social(element, timer, direction) {

        setTimeout(function () {
            var options = {};
            options[direction] = 0;
            jQuery(element).animate(options);
        }, timer);
    }

    jQuery(array).each(function (key, value) {
        if (jQuery(value).length) {
            var w_h_c = define_max_w_h(value);
            var w_w = jQuery(window).height();
            var position_top = (w_w - (w_h_c["height"] * w_h_c["counts"])) / 2;
            var count = 1;
            var timer = 0;

            jQuery(value).children().each(function () {
                if (count > 1) {
                    position_top = position_top + w_h_c["height"];
                }
                if (value === ".socialbottom") {

                } else {
                    jQuery(this).css({"top": position_top});
                }
                count = count + 1;
                var sp = value.split("social");
                apear_social(this, timer, sp[1]);


                timer = timer + 100;


                jQuery(this).off("hover");
                jQuery(this).hover(function () {
                    on_hover(this, "width");
                }, function () {
                    off_hover(this, "width");
                });

            });


        }

    });

};

jQuery.rewrite_btn_style = function rewrite_btn_style(newbtnclass) {
    jQuery(".btn-primary").each(function () {
        if (!jQuery(this).hasClass("exclude_ovveride")) {
            jQuery(this).removeClass("btn-primary").addClass(newbtnclass);
        }
    });
};

jQuery(document).ready(function () {
    jQuery.marlev_run_animation();
    jQuery.marlev_mobile_menu();
    jQuery.marlev_img_resize();
    jQuery.marlev_accordion_menu(".moduletable_accordion", ".deeper");
    jQuery.accept_cookie();
    //  jQuery.advanced_search();
    jQuery.marlev_toup();
    jQuery.row_striped_height();
    jQuery.table_responsive();
    jQuery.fix_checkbox_sendmail_tome("#jform_contact_email_copy");
    jQuery.fix_collapse(['#slide-contact', '#finder-search']);
    setTimeout(function(){
        jQuery.socialfixed_right(['.socialright', '.socialleft']);
    },1000);
    jQuery.mobile_menu_adapter_resize();

});

