<?php
/**
 * Customize header styles for Divi theme responsiveness.
 */
if (!function_exists('pac_drh_header_style')):
    function pac_drh_header_style()
    {
        $style_arr = [];
        // Mobile Submenus
        if (pac_drh_get_settings_enabled('pac_drh_enable_collapse_mobile_submenus')) {
            $color = et_get_option('pac_drh_collapse_icon_color', '#2ea3f2');
            $bg_color = et_get_option('pac_drh_collapse_icon_bg_color', '#f0f3f6');
            $style_arr[] = '#et_mobile_nav_menu .et-db #et-boc .et-l ul.et_mobile_menu li.menu-item-has-children .mobile-toggle,#et_mobile_nav_menu .et-db #et-boc .et-l ul.et_mobile_menu li.page_item_has_children .mobile-toggle,#et_mobile_nav_menu ul.et_mobile_menu li.menu-item-has-children .mobile-toggle,#et_mobile_nav_menu ul.et_mobile_menu li.page_item_has_children .mobile-toggle{width:44px;height:100%;padding:0!important;max-height:44px;border:none;position:absolute;right:0;top:0;z-index:999;background-color:transparent}#et_mobile_nav_menu .et-db #et-boc .et-l ul.et_mobile_menu>li.menu-item-has-children,#et_mobile_nav_menu .et-db #et-boc .et-l ul.et_mobile_menu>li.menu-item-has-children .sub-menu li.menu-item-has-children,#et_mobile_nav_menu .et-db #et-boc .et-l ul.et_mobile_menu>li.page_item_has_children,#et_mobile_nav_menu ul.et_mobile_menu>li.menu-item-has-children,#et_mobile_nav_menu ul.et_mobile_menu>li.menu-item-has-children .sub-menu li.menu-item-has-children,#et_mobile_nav_menu ul.et_mobile_menu>li.page_item_has_children{position:relative}#et_mobile_nav_menu .et-db #et-boc .et-l .et_mobile_menu .menu-item-has-children>a,#et_mobile_nav_menu .et_mobile_menu .menu-item-has-children>a{background-color:transparent}#et_mobile_nav_menu #main-header ul.et_mobile_menu .menu-item-has-children .sub-menu,#et_mobile_nav_menu .et-db #et-boc .et-l ul.et_mobile_menu .menu-item-has-children .sub-menu,#et_mobile_nav_menu .et-db #main-header ul.et_mobile_menu .menu-item-has-children .sub-menu,#et_mobile_nav_menu ul.et_mobile_menu .menu-item-has-children .sub-menu{display:none!important;visibility:hidden!important}#main-header ul.et_mobile_menu .menu-item-has-children .sub-menu.visible,.et-db #et-boc .et-l ul.et_mobile_menu .menu-item-has-children .sub-menu.visible,.et-db #main-header ul.et_mobile_menu .menu-item-has-children .sub-menu.visible,ul.et_mobile_menu .menu-item-has-children .sub-menu.visible{display:block!important;visibility:visible!important}#et_mobile_nav_menu .et-db #et-boc .et-l ul.et_mobile_menu li.menu-item-has-children .mobile-toggle,#et_mobile_nav_menu ul.et_mobile_menu li.menu-item-has-children .mobile-toggle{text-align:center;opacity:1}#et_mobile_nav_menu .et-db #et-boc .et-l ul.et_mobile_menu li.menu-item-has-children .mobile-toggle::after,#et_mobile_nav_menu ul.et_mobile_menu li.menu-item-has-children .mobile-toggle::after{top:10px;position:relative;font-family:ETModules,serif;content:"3";color:'.esc_attr($color).';background:'.esc_attr($bg_color).';border-radius:50%;padding:3px}#et_mobile_nav_menu .et-db #et-boc .et-l ul.et_mobile_menu li.menu-item-has-children.dt-open>.mobile-toggle::after,#et_mobile_nav_menu ul.et_mobile_menu li.menu-item-has-children.dt-open>.mobile-toggle::after{content:"2"}#et_mobile_nav_menu .et_pb_menu_0.et_pb_menu .et_mobile_menu:after{position:absolute;right:5%;margin-left:-20px;top:-14px;width:0;height:0;content:"";border-left:20px solid transparent;border-right:20px solid transparent;border-bottom:20px solid #fff}#et_mobile_nav_menu .mobile_menu_bar{position:relative;display:block;line-height:0}#et_mobile_nav_menu .et_pb_menu_0.et_pb_menu .et_mobile_menu,#et_mobile_nav_menu .et_pb_menu_0.et_pb_menu .et_mobile_menu ul{background-color:#fff!important;border-radius:10px}';
        }
        // Menu Scrollbar Height
        if (pac_drh_get_settings_enabled('pac_drh_mobile_menu_scrollable')) {
            $height = pac_drh_get_option('pac_drh_mobile_menu_max_height', '80vh');
            $style_arr[] = '#et_mobile_nav_menu .et_mobile_menu {overflow-y: scroll !important;max-height: '.esc_attr($height).' !important;-overflow-scrolling: touch !important;-webkit-overflow-scrolling: touch !important}';
        }
        // Menu Hamburger Desktop
        if (pac_drh_get_settings_enabled('pac_drh_enable_hamburger_menu_desktop')) {
            $style_arr[] = '@media (min-width:980px){#top-menu{display:none}#et_mobile_nav_menu{display:flex;float:none;margin:-6px 6px;;align-items:center}}';
            $style_arr[] = '@media (min-width: 980px) {#et-top-navigation .et-cart-info {margin-top: -10px;}#et_top_search {float: left;margin: -5px 50px 0 10px;}}';
        }
        // Menu Open Icon
        if (pac_drh_get_settings_enabled('pac_drh_enable_open_mobile_icon')) {
            $style_arr[] = '#et_mobile_nav_menu .mobile_nav.opened .mobile_menu_bar:before{content:"M"}';
        }
        // Mobile Logo
        if (!empty(pac_drh_get_option('pac_drh_mobile_logo'))) {
            $style_arr[] = '#logo {display: none;}';
            $style_arr[] = '@media (min-width:300px) and (max-width:767px){.pac_drh_desktop_logo{display:none!important}.pac_drh_mobile_logo{display:inline-block!important}}';
            $style_arr[] = '@media (min-width:768px) and (max-width:980px){.pac_drh_desktop_logo{display:none!important}.pac_drh_mobile_logo{display:inline-block!important}}';
            $style_arr[] = '@media (min-width:981px){.pac_drh_desktop_logo{display:inline-block!important}.pac_drh_mobile_logo{display:none!important}}';
        }
        // Menu BreakPoints
        if (!empty(pac_drh_get_option('pac_drh_menu_breakpoint'))) {
            $breakpoint = pac_drh_get_option('pac_drh_menu_breakpoint');
            if (preg_match('/^[1-9][0-9]{0,15}$/', $breakpoint)) {
                return;
            }
            if ((int)$breakpoint > 981) {
                $style_arr[] = '@media only screen and (max-width:'.$breakpoint.'){#et_mobile_nav_menu {display: block !important;margin-bottom: 10px;margin-top: 5px}#top-menu-nav{display: none !important}#et_top_search {margin: 14px 15px 0 0;float: left;}#et-top-navigation .et-cart-info {margin: 8px 0 0 22px;}}';
            }
            if ((int)$breakpoint < 980) {
                $style_arr[] = '@media only screen and (min-width:'.$breakpoint.'){#top-menu{display:block!important}#et_mobile_nav_menu{display:none!important}}';
            }
            $style_arr[] = '@media (max-width: 767px) and (min-width: 0px){ #et_top_search{margin: 5px 15px 0 0;float: left;}}';
        }
        // Nav Bar
        if (pac_drh_get_settings_enabled('pac_drh_fixed_navigation_bar')) {
            $style_arr[] = '@media (max-width: 767px) and (min-width: 0px){.et_fixed_nav #main-header {position: fixed;}}';
            $style_arr[] = '@media (min-width: 768px) and (max-width: 980px){.et_fixed_nav #main-header {position: fixed;}}';
            $style_arr[] = '@media (max-width: 981px) { #main-header { position: fixed;}}';
        }
        // Hamburger Text
        if (pac_drh_get_settings_enabled('pac_drh_enable_hamburger_menu_text')) {
            $hamburger_text = pac_drh_get_option('pac_drh_hamburger_header_menu_text', 'Menu');
            if (!empty($hamburger_text)) {
                $style_arr[] = '#et-top-navigation #et_mobile_nav_menu:before{content:"'.esc_attr($hamburger_text).'";position: absolute !important;right: 30px;}';
                $style_arr[] = '@media (max-width: 980px) {#et-top-navigation #et_mobile_nav_menu:before{top: 28px;right: 30px;} #et-top-navigation #et_top_search {margin: 0 45px 0 0;}}';
                $style_arr[] = '@media only screen and (max-width:980px){#et_top_search {margin: 0 40px 0 0 !important;}}';
                if (pac_drh_get_settings_enabled('pac_drh_enable_hamburger_menu_desktop')) {
                    $top = !pac_drh_get_settings_enabled('pac_drh_enable_collapse_mobile_submenus') ? '28px' : '20px';
                    $style_arr[] = '@media (min-width: 980px) {#et-top-navigation #et_mobile_nav_menu:before{right: 40px;top:'.esc_attr($top).' ;}}';
                }
                $breakpoint = pac_drh_get_option('pac_drh_menu_breakpoint');
                if (!!empty($breakpoint) && (int)$breakpoint > 981 && !preg_match('/^[1-9][0-9]{0,15}$/', $breakpoint)) {
                    $style_arr[] = '@media only screen and (max-width:'.esc_attr($breakpoint).'){#et_top_search {margin: 14px 40px 0 0 !important;}}';
                }
            }
        }
        wp_register_style('divi-responsive-helper-customize-header', false);
        wp_enqueue_style('divi-responsive-helper-customize-header');
        wp_add_inline_style('divi-responsive-helper-customize-header', implode(' ', array_unique($style_arr)));
    }

    add_action('wp_enqueue_scripts', 'pac_drh_header_style');
endif;
/**
 * Adds custom JavaScript functionality to customize the header in Divi theme responsively.
 */
if (!function_exists('pac_drh_header_js')):
    function pac_drh_header_js()
    {
        $script_arr = [];
        if (pac_drh_get_settings_enabled('pac_drh_close_mobile_menu_outside_click')) {
            $script_arr[] = 'jQuery(function(e){e("body").click(function(n){let i=e("#et_mobile_nav_menu .mobile_nav");!i.find("a").is(n.target)&&i.hasClass("opened")&&e("#et_mobile_nav_menu .mobile_menu_bar").click()})});';
        }
        if (pac_drh_get_settings_enabled('pac_drh_enable_collapse_mobile_submenus')) {
            ob_start();
            ?>
            jQuery(function(e){e(document).ready(function(){e("body #et_mobile_nav_menu ul.et_mobile_menu li.menu-item-has-children, body #et_mobile_nav_menu ul.et_mobile_menu  li.page_item_has_children").append('<a href="javascript:void(0)" class="mobile-toggle"></a>'),e("#et_mobile_nav_menu ul.et_mobile_menu li.menu-item-has-children .mobile-toggle, #et_mobile_nav_menu ul.et_mobile_menu li.page_item_has_children .mobile-toggle").click(function(l){l.preventDefault();let i=e(this).parent("li");i.hasClass("dt-open")?(i.toggleClass("dt-open"),i.find("ul.children").first().toggleClass("visible"),i.find("ul.sub-menu").first().toggleClass("visible")):(!i.parent("ul").hasClass("visible")&&pac_drh_js_params.isCollapseSubMenus&&(e("li.menu-item-has-children").removeClass("dt-open"),e("li.menu-item-has-children .sub-menu").removeClass("visible")),i.toggleClass("dt-open"),i.find("ul.children").first().toggleClass("visible"),i.find("ul.sub-menu").first().toggleClass("visible"))}),e("body ul.et_mobile_menu li.menu-item-has-children, body ul.et_mobile_menu li.page_item_has_children").attr("data-icon","P"),e(".mobile-toggle").on("mouseover",function(){e(this).parent().addClass("is-hover")}).on("mouseout",function(){e(this).parent().removeClass("is-hover")})})});
            <?php
            if (pac_drh_get_settings_enabled('pac_drh_openclose_submenu_with_parent')) { ?>
                jQuery(document).ready(function(e){e("#et_mobile_nav_menu .et_mobile_menu .menu-item-has-children a").each(function(){e(this).click(function(){let i=e(this).siblings("ul").children("li").find("ul");i.removeClass("visible"),i.toggleClass("visible")})}),e("#et_mobile_nav_menu ul.et_mobile_menu li.menu-item-has-children, #et_mobile_nav_menu ul.et_mobile_menu li.page_item_has_children").each(function(){e(this).find("a:first").attr("href","javascript:void(0)").off("click"),e(this).find("a:first").click(function(){e(this).find("ul").toggleClass("visible")})}),setTimeout(function(){e("#et_mobile_nav_menu .et_mobile_menu .menu-item.menu-item-has-children a:first-child").click(function(){let i=e(this).parent("li");i.hasClass("dt-open")?(i.toggleClass("dt-open"),i.find("ul.children").first().toggleClass("visible"),i.find("ul.sub-menu").first().toggleClass("visible")):(i.parent("ul").hasClass("visible")||(e("#et_mobile_nav_menu .et_mobile_menu li.menu-item-has-children").removeClass("dt-open"),e("#et_mobile_nav_menu .et_mobile_menu li.menu-item-has-children .sub-menu").removeClass("visible")),i.toggleClass("dt-open"),i.find("ul.children").first().toggleClass("visible"),i.find("ul.sub-menu").first().toggleClass("visible"))})},100)});
                <?php
            }
            $script_arr[] = ob_get_clean();

        }
        wp_register_script('divi-responsive-helper-customize-header', false, ['jquery'], false, true);
        wp_enqueue_script('divi-responsive-helper-customize-header');
        wp_add_inline_script('divi-responsive-helper-customize-header', implode(' ', array_unique($script_arr)));

    }

    add_action('wp_enqueue_scripts', 'pac_drh_header_js');
endif;