(function($) {

    $(document).on('ready', function(e){
        let widget = $('.webien-tabs');
        let tabKeyDown = false;

        // Listen for special click to open direct link
        $(document).keydown(function( event ) {
            if(event.which === 84){ // "t" key
                tabKeyDown = true;
            }
        });

        $(document).keyup(function( event ) {
            if(event.which === 84){ // "t" key
                tabKeyDown = false;
            }
        });


        widget.find('.tab').each(function(){
            let el = $(this);
            let id = el.attr('data-id');
            el.on('click', function (){

                // If "t" key is down open direct link to tab
                if(tabKeyDown){
                    tabKeyDown = false;
                    window.open(el.attr('data-direct-url'), '_blank').focus();
                    return false;
                }

                widget.find('.tab, .tab-content').removeClass('active');
                el.addClass('active');
                widget.find('.tab-content[data-id='+id+']').addClass('active');

                // Send pageview to Analytics for the original page behind the tab.
                let url = el.attr('data-url');
                if(url && typeof ga !== 'undefined'){
                    ga('gtm2.send', {
                        'hitType': 'pageview',
                        'location': url
                    });
                }

                return false;

            });
        });


        // Detect active tab from url param
        const urlParams = new URLSearchParams(window.location.search);
        const activeTab = urlParams.get('activeTab');

        if(activeTab) {
            $btn = $('button[data-slug="' + activeTab + '"]');
            if($btn){
                $('html, body').animate({
                    scrollTop: $btn.offset().top - 100
                }, 500);
                $btn.trigger( "click" );
            }
        }

    });






}(jQuery));