// source --> https://www.profitap.com/wp-content/themes/Divi-child/js/menu.js?ver=1771854674 
jQuery( document ).ready( function( $ ) {

    // handy function to get a specific class from a classList
    function getClass(element, startsWith) {

		var result = "";

        $(element.attr('class').split(' ')).each(function() {

            if (this.indexOf(startsWith) > -1) { 
                result = this;
            }
        });
        
		return result.valueOf();
	}

    // handler for toggling mobile sub menu items
    function setup_collapsible_submenus() {
        // toggle div to insert
        var toggle = "<div class='sub-menu-toggle'></div>";

        // only insert on items with children
        var targetItem = $("#main-header #mobile_menu.et_mobile_menu .menu-item-has-children > a");

        // check that we don't add double toggles
        for (var i = 0; i < targetItem.length; i++ ) {
            if($(targetItem[i]).siblings(".sub-menu-toggle").length === 0) {
                $( toggle ).insertBefore( targetItem[i] );
            }
        }

        // toggle when tapped
        $( "#main-header #mobile_menu.et_mobile_menu .sub-menu-toggle" ).click(function () {
            $(this).toggleClass("popped");
            $(this).parent('li').toggleClass("popped");

            // add an extra check to see if we clicked on a pft mega menu, in this case we open the items one level deeper
            if ($(this).parent().hasClass('pft-mega')) {
                $(this).parent('li').find('.pft-mega-menu > .sub-menu-toggle').toggleClass('popped');
            }
        });
    }

    // handler for showing acf description on the pft mega menu
    (function pft_mega_description_handler() {
        var pftMega = $('.pft-mega', '#main-header');
        var pftClass = 'pft-link-';
        var defaultClass = 'pft-link-default';

        // loop through all the pft mega menus so it works for multiple mega menu's as well
        for (var i = 0; i < pftMega.length; i++) {
            // store the current pft mega menu parent item
            var currentMega = pftMega[i];
            var allDescriptions = $('li.pft-mega .pft-mega-hover-area li', '#main-header');
            var defaultLink = $('li.pft-mega .pft-mega-hover-area li.pft-link-default', '#main-header');

            // array to store the pft-link classes for easy filtering later on
            var descArray = [];

            // get all the relevant Description classes and fill the array
            $(allDescriptions).each(function(){
                descArray.push(getClass($(this), pftClass));
            });

            // find all the list items in the pft mega menu
            $(currentMega).find('li > a').hover(function() {
                
                // check if this item or it's parent has a pft-link-class, if so show the appropriate description
                if ($(this).parent('li').is('[class^=pft-link-]') || $(this).parents('.menu-item-has-children').is('[class^=pft-link-]')) {
                    // the pft-link to match with the correct description
                    var toMatch = getClass($(this).closest('li[class^=pft-link-]'), 'pft-link-');
                    
                    // this returns false if the current link is not present 
                    var valid = $.inArray( toMatch, descArray );

                    // loop through check all the descriptions for a match
                    $(allDescriptions).each(function() {
                        // if we match, show the match
                        if ($(this).hasClass(toMatch) && valid !== -1) {
                            
                            // show the correct description
                            $(this).addClass('pft-show-link');

                            // hide the default one
                            $(defaultLink).removeClass('pft-show-link');

                        }
                        // add fallback for showing the default
                        else if ($(this).hasClass(toMatch) && valid === -1) {
                            // show the default description
                            if ($(this).hasClass(defaultClass)) {
                                $(this).addClass('pft-show-link');
                            } else {
                                $(this).removeClass('pft-show-link');
                            }
                        }
                        // fallback default
                        else {
                            // show the default description
                            if ($(this).hasClass(defaultClass)) {
                                $(this).addClass('pft-show-link');
                            } else {
                                $(this).removeClass('pft-show-link');
                            }
                        }
                        
                    });

                } 
                // if nothing just show the default instead of nothing
                else {
                    $(allDescriptions).each(function() {
                        if ($(this).hasClass(defaultClass)) {
                            $(this).addClass('pft-show-link');
                        } 
                        else {
                            $(this).removeClass('pft-show-link');
                        }
                    });
                }
            });

        }
    })();

    // run on load because Divi loads the menu differently on different pages
    // this way the toggles will appear and work even for slow connections, it just might take a while 
    $(window).load(function() {
        setup_collapsible_submenus();
    });
    
});
// source --> https://www.profitap.com/wp-content/themes/Divi-child/js/tabs.js?ver=1771854674 
jQuery(document).ready(function($) {
    // add class to tab content that contains oldskool tables
    if ( $(".pfttabs .et_pb_tab table").length > 0 ) {
        // if pfttabs with a table inside exist on the page add a class to the parent
        for(var i = 0; i < $(".pfttabs .et_pb_tab table").length; i++) {
            $(".pfttabs .et_pb_tab table").parent().addClass("tab-has-table");
        }
    }
});