(function($) {
    Drupal.behaviors.seed_police_menu = {
        attach: function (context, settings) {
            jQuery('body').once('body-processed', function() {
                jQuery('.content-father > .menu > .menu-item.first > .menu > li.expanded').click(function(){

                    jQuery(this).toggleClass('active');
                    jQuery(this).siblings('.expanded').removeClass('active');
                    // Width window
                    var WidthWindow = jQuery(window).width();
                
                    // Side size page
                    var WidthSide = (WidthWindow - 1200) / 2;

                    // Width element Menu
                    var WidthContent = jQuery('.content-father > .menu > .menu-item.first').width();

                    // Position left content
                    var ResultPosition = WidthSide + WidthContent;

                    // Width content
                    var WidthResult = WidthWindow - ResultPosition;

                    // Movil Height
                    var WindowHeight = jQuery(window).height();
                
                    // Position item menu
                    var PositionItemMovil = jQuery(this).offset().top;

                    //Position menu expanded
                    var ResultHeight = WindowHeight - PositionItemMovil;
                
                    //Position menu expanded tablet 
                    var PositionItemLeft = jQuery(this).offset().left;

                    var PositionLeftExpaned = (WidthWindow - PositionItemLeft) * -1;
                
                    // Height Body
                    var BodyHeight = jQuery('body').height();

                    if (jQuery('.content-father > .menu > .menu-item.first li.expanded').hasClass('active') ){
                    
                        jQuery('body').addClass('active-menu-two-expanded');
                        jQuery('.active').children('.menu').removeClass('element-invisible');
                    
                        // Desk
                        if(WidthWindow > 1024) {
                            jQuery('.active').children('.menu').css({
                                left: ResultPosition,
                                "height": "100%", 
                            }, 0);
                        
                            jQuery('.active').children('.menu').animate({
                                "padding": "21px",
                                "border-left": "1px solid #717171",
                                width: WidthResult
                            }, 0);  
                        }

                        // Table
                        if (WidthWindow < 1024 && WidthWindow > 767) {

                            jQuery('.active').children('.menu').css({
                                width: WidthWindow,
                                left: 0,
                                padding: 0,
                                height: ResultHeight,
                            }, 0);

                            if ( PositionItemLeft > 50) {
                                var PositionLeftExpaned = (WidthWindow - PositionItemLeft) * -1;

                                jQuery('.active').children('.menu').css({
                                left: PositionLeftExpaned,
                                "margin-left": 0,
                                }, 0);
                            }
                        }

                        // Movile
                        if (WidthWindow < 767) {
                            jQuery('.active').children('.menu').css({
                                width: WidthWindow,
                            }, 0);

                            jQuery('.active').children('.menu').animate({
                                height: ResultHeight,
                            }, 0);
                        }
                    }

                    //Menu Expanded Null
                    if(jQuery(this).hasClass('active') === false){
                        jQuery('.active').children('.menu').addClass('element-invisible');
                
                        if (WidthWindow > 1024){
                            jQuery(this).children('.menu').animate({
                                "padding": 0,
                                "border": 0,
                                width: 0,
                            }, 0);
                        }

                        if (WidthWindow < 1024){        
                            jQuery('body').removeClass('active-menu-two-expanded');
                        
                            jQuery(this).children('.menu').animate({
                                height: 0,   
                            }, 0);
                        }
                    }

                    if (WidthWindow < 1024){
                        jQuery('.active').siblings('.expanded').children('.menu').animate({
                            height: 0,
                        }, 0);
                    }

                    if (WidthWindow > 1024){
                        jQuery('.active').siblings('.expanded').children('.menu').animate({
                            "padding": 0,
                            "border": 0,
                            width: 0,
                        }, 0);
                    }
                });
                
                jQuery(window).bind('resize', function() {
                    if (jQuery('.content-father > .menu > .menu-item.first li.expanded').hasClass('active')){

                        // Width window
                        var WidthWindow = jQuery(window).width();
                
                        // Side size page
                        var WidthSide = (WidthWindow - 1200) / 2;

                        // Width element Menu
                        var WidthContent = jQuery('.content-father > .menu > .menu-item.first').width();

                        // Position left content
                        var ResultPosition = WidthSide + WidthContent;

                        // Width content
                        var WidthResult = WidthWindow - ResultPosition;

                        // Movil Height
                        var WindowHeight = jQuery(window).height();
                
                        // Position item menu
                        var PositionItemMovil = jQuery('.content-father > .menu > .menu-item.first li.expanded.active').offset().top;

                        //Position menu expanded
                        var ResultHeight = WindowHeight - PositionItemMovil;
                        //Position menu expanded tablet 
                        var PositionItemLeft = jQuery('.content-father > .menu > .menu-item.first li.expanded.active').offset().left;


                        // Desk
                        if(WidthWindow > 1024) {
                            jQuery('.active').children('.menu').css({
                                left: ResultPosition,
                                "height": "100%",
                            }, 0);
                        
                            jQuery('.active').children('.menu').animate({
                                "padding": "21px",
                                "border-left": "1px solid #717171",
                                width: WidthResult
                            }, 0);  
                        }

                        // Table
                        if (WidthWindow < 1024 && WidthWindow > 767) {

                            jQuery('.active').children('.menu').css({
                                width: WidthWindow,
                                left: 0,
                                padding: 0,
                                height: ResultHeight,
                            }, 0);

                            if ( PositionItemLeft > 50) {
                                var PositionLeftExpaned = (WidthWindow - PositionItemLeft) * -1;

                                jQuery('.active').children('.menu').css({
                                    left: PositionLeftExpaned,
                                    "margin-left": 0,
                                }, 0);
                            }
                        }

                        // Movile
                        if (WidthWindow < 767) {
                            jQuery('.active').children('.menu').css({
                                width: WidthWindow,
                                padding: 0,
                                left: 0,
                                height: ResultHeight,
                            }, 0);
                        }
                    }

                    if (WidthWindow < 1024){
                        jQuery('.active').siblings('.expanded').children('.menu').animate({
                            height: 0,
                            "padding": 0,
                        }, 0);
                    }

                    if (WidthWindow > 1024){
                        jQuery('.active').siblings('.expanded').children('.menu').animate({
                            "padding": 0,
                            "border": 0,
                            width: 0,
                        }, 0);
                    }
                });

                const queryString = window.location.search;
                const urlParams = new URLSearchParams(queryString);
                const open = urlParams.get('open')
                jQuery('.title-acordeon', context).each(function() {
                    if (jQuery(this).text().toLowerCase() == open) {
                        $([document.documentElement, document.body]).animate({
                            scrollTop: jQuery(this).parent('a').parent().parent().offset().top
                        }, 2000);
                        jQuery(this).parent('a').trigger('click');
                    }
                });
            });
        }
    }
})(jQuery);
