/**
 * ----------------------------------------------------------------------------
 * GXJS Footer
 * @module:   GX
 * @version:  1.3
 * @modified: 06/24/2022 - CN
 * ----------------------------------------------------------------------------
 */

'use strict';

// Set the var for which skuid page is active
var gx_skuid_page;

// Check if the data attribute exists
if (typeof $('body').data('pagename') !== 'undefined') {
    gx_skuid_page = $('body').data('pagename');
}

var ncf;

if (!ncf) {
    ncf = window.ncf;
}

$(document).ready(function() {

    // Remove the inline style Skuid applies
    $('.page-panel__primary').removeAttr('style');
    $('.page-panel__secondary').removeAttr('style');
    $('td').removeAttr('style');
    $('.nx-tabset-header > div').removeAttr('style');
    $('.nx-tabset-header > div input').removeAttr('style');
    $('.nx-tabset-header > div label').removeAttr('style');

    /**
     * Run the invoked modals
     *
     */
    $(document).on('click', '.js-modal', function() {
        const modalData = $(this).data('modal');
        modalDialogFn(modalData);
    });

    /**
     * Close a targeted dialog
     *
     */
    $(document).on('click', '.js-hide-modal', function() {
        const modalDialog = $(this).parents('.ui-dialog-content');
        $(modalDialog).dialog('destroy');
    });

    $('.js-load-activity').on('click', function() {

        let positionHeightFrame = $('.recent-activity__container .nx-editor-contents').height(),
            positionHeight = $('.recent-activity__container').height() + 1,
            position = $('.recent-activity__container').css('background-position').split(' ');

        position[1] = parseInt(position[1].replace('px', ''));
        position[1] = position[1] + 396;
        positionHeight = positionHeight + 396;

        $('.recent-activity__container').attr(
            'style',
            'height: ' + (positionHeight >= positionHeightFrame ? positionHeightFrame : positionHeight) + 'px;' +
            'background-position: 0 ' + position[1] + 'px !important;'
        );

        if (positionHeight >= positionHeightFrame) {
            $(this).remove();
        }
    });

    // Get the device type
    let deviceType = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);

    // Add a listener to change detect device type on resize
    window.addEventListener('resize', function(event) {
        deviceType = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
    });

    /**
     * Quick grab to retrieve the with of the scrollbar and use it to set right
     * padding on body and prevent content shift
     */
    const windowWidth = window.innerWidth;
    const documentWidth = document.documentElement.clientWidth;

    // Init the mobile menu dialog window
    $('.mobile-nav__container').dialog({
        dialogClass: 'mobile-nav',
        resizable: false,
        modal: false,
        autoOpen: false,
        open: function() {
            // Set the icon active class
            $('.js-mobile-nav-icon').addClass('is-active');

            $(this).dialog(
                'option',
                'height',
                $(document).height()
            );

            // Set body styles to prevent window scroll
            $('html, body').css({
                'overflow': 'hidden',
                'position': 'relative',
            });
        },
        close: function() {
            // Remove the icon active class
            $('.js-mobile-nav-icon').removeClass('is-active');

            // Clear out the temp body styles
            $('html, body').css({
                'overflow': '',
                'position': '',
                'height': ''
            });
        }
    });

    // Mobile menu trigger
    $('.js-mobile-nav-icon').on('click', function() {
        if ($('.mobile-nav__container').dialog('isOpen')) {
            $('.mobile-nav__container').dialog('close');
            $('body').removeAttr('style');
        } else {
            $('.mobile-nav__container').dialog('open');
            // Add the right padding if this is not a scrollbar-less device
            document.body.style.paddingRight = !deviceType ? windowWidth - documentWidth + 'px' : '';
        }
    });

    $('.js-mobile-close-icon').on('click', function() {
        $('.mobile-nav__container').dialog('close');
        $('body').removeAttr('style');
    });

    // Handle closing the menu on outside click
    $(document).on('click touchstart', function(event) {
        if ($('.mobile-nav__container').dialog('isOpen') && !$(event.target).is('.mobile-nav__container, .mobile-nav__item, a') && !$(event.target).closest('.mobile-nav__container').length) {
            $('.mobile-nav__container').dialog('close');
        }
    });

    // Get and output the affiliateData
    let affiliateData = '';

    if (ncf && ncf.affiliateRow && ncf.affiliateRow.Id !== null) {
        affiliateData = ncf.affiliateRow;
    } else if (ncf && ncf.contactRow && ncf.contactRow.Primary_Fund__r && ncf.contactRow.Primary_Fund__r.Affiliate__r) {
        affiliateData = ncf.contactRow.Primary_Fund__r.Affiliate__r;
    }

    if (ncf && affiliateData) {
        $('.affiliate-phones').html(
            (affiliateData.Phone__c ? '<span><a href="tel:' + affiliateData.Phone__c + '">' + affiliateData.Phone__c + '</a> phone</span>'  : '') +
            (affiliateData.Phone_cf800__c ? '<span><a href="tel:' + affiliateData.Phone_cf800__c + '">' + affiliateData.Phone_cf800__c + '</a> toll-free</span>'  : '') +
            (affiliateData.Fax__c ? '<span>' + affiliateData.Fax__c + ' fax</span>'  : '')
        );

        $('.affiliate-email').attr('href', 'mailto:' + affiliateData.Email__c);
        $('.affiliate-email').text(affiliateData.Email__c);

        $('.affiliate-address').html(
            '<span>' + affiliateData.Street1__c +
            (affiliateData.Street2__c ? ', ' + affiliateData.Street2__c : '') + '</span>' +
            '<span>' +
            affiliateData.City__c + ', ' +
            affiliateData.State__c + ' ' +
            affiliateData.ZIP__c + '</span>'
        );

        let google_directions_url = 'https://maps.google.com/?q=' + affiliateData.Street1__c + ' ' + affiliateData.City__c + ' ' + affiliateData.State__c + ' ' + affiliateData.ZIP__c;

        $('.affiliate-google-directions').attr('href', google_directions_url).attr('target', '_blank');
    }

    // $('.mobile-nav__contact-link').on('click', function() {
    //     $('.mobile-nav__contact-container').dialog('open');
    //     return false;
    // });

    /*
    if ( ncf ) {
        $('#mobile-nav .mobileMenuFundSelector #sprytrigger_funddropdown').after(
            $('#mobile-nav .mobileMenuFundSelector #sprytooltip_funddropdown')
        );
    }
    */
});

$('<div class="mobile-nav__contact-container">').html(
    `
    <div class="contact-details">
        <div class="contact-details__item">
            <div class="affiliate-phones"></div>
            <a class="contact-details__link affiliate-email"></a>
        </div>
        <div class="contact-details__item affiliate-address">
            <div class="affiliate-address"></div>
            <a class="contact-details__link affiliate-google-directions">Driving directions to our office</a>
        </div>
    </div>
    `
// @dialog
).dialog({
    dialogClass: 'mobile-nav__contact',
    resizable: false,
    modal: true,
    autoOpen: false,
    position: {
        my: 'center',
        at: 'center'
    },
    open: function(){
        $('.ui-widget-overlay').bind('click', function() {
            $('.mobile-nav__contact-container').dialog('close');
        });
    }
});
