$(function(){
    // adjustSidebarHeight();

    //menu
    var path = location.pathname.toString();
    if(path.match(/\/customer\//) && path.match(/\/customer\//).index > 1){
        $("#topnav-sharing").addClass('active');
    }

    $('.privacycenter .status-message').removeClass('fade');

    // Hide and then toggle/show details in activity list activities
    $('.activity-list-details-info').hide();
    $('.activity-list-details').on('click', function() {
        if($(this).hasClass("show")){
            $(this).removeClass("show");
            $(this).parent().find('.activity-list-details-info').slideUp();
        } else {
            $(this).addClass("show");
            $(this).parent().find('.activity-list-details-info').slideDown();
        }
    });

    // Reverts toggle switch if user cancels action
    $('.cancel-sharing').on('click', function() {
        cancelActivitySharingChange($(this));
    });

    // Change download icon to checkmark after link has been clicked on
    $('.pc-dl-link').on('click', function() {
        $(this).find('i:first').removeClass('fa-download fa-check').addClass('fas fa-check');
    });

    //Delete account
    var $delete_button = $(".pc-deletepage .delete-button-holder .btn");
    $("label.delete-checkbox").click(function(){
        $(".deletepage-deleteacct input.delete-checkbox").click();
    });
    $(".deletepage-deleteacct input.delete-checkbox").click(function(){
        if($delete_button.hasClass("disabled")){
            $delete_button.removeClass("disabled");
        } else {
            $delete_button.addClass("disabled");
        }
    });
    $delete_button.click(function(){
        if(!$delete_button.hasClass('disabled')){
            $('#PC_ConfirmDeleteModal').modal('show');
        }
    });
    $(".modal .modal-footer .cancel").click(function(){
        $(".modal.show").modal('hide');
    });
});

function adjustSidebarHeight() {
    var contentHeight = $('.pc-mainsectionrow').height();
    var headerFooterHeight = $('header').height() + $('footer').height();
    if ($(contentHeight).length && $(headerFooterHeight).length ) {
        if (contentHeight + headerFooterHeight < window.innerHeight) {
            var minHeight = window.innerHeight - headerFooterHeight;
            $('.pc-mainsectionrow').css('min-height', minHeight + 'px');
        }
    }
}

function confirmPCAccountInfoModal() {
    $('ul#pcaccountinfo-fieldstodelete').html('');      // first, clear list (in case user has opened this modal before)
    var fields = $('#PCAccountInfo_Form input:checked');
    if (fields.length) {
        $.each(fields, function() {
            var field = $(this).attr('name');
            field = field.match(/[A-Z][a-z]+|[0-9]+/g).join(" ");   // e.g. 'FirstName' becomes 'First Name'

            // A couple of exceptions to specify:
            if (field === 'State') field = 'Region';
            if (field === 'All Location') field = 'All IP-generated location data';

            $('#pcaccountinfo-fieldstodelete').append("<li>"+field+"</li>");
        });
        $('#PC_ConfirmAcctModal').modal('show');
    } else {
        $('#PC_ConfirmAcctModal_NoResults').modal('show');
    }
}

function confirmPCRemoveSocialModal(label, labelCapital, domains) {
    // first, clear list (in case user has opened this modal before)
    $('.PC_ConfirmSocialModal_Name, .PC_ConfirmSocialModal_Domains').html("");
    $('.PC_ConfirmSocialModal_Name').text(labelCapital);

    // If there's no domains then we don't need to show the list in the popup
    if (domains) {
        $('.confirm-with-domains').css('display', 'block');
        var domains = domains.split(", ");
        $.each(domains, function() {
            $('.PC_ConfirmSocialModal_Domains').append("<li>"+this+"</li>");
        });
    } else {
        $('.confirm-with-domains').css('display', 'none');
    }

    // add link
    $('.href-channel').attr('href', "/privacycenter/removeSocialChannel?channel="+label);

    // adding the icon to the header
    $('i#social-icon-modal').removeClass(); // clear current icon, in case this modal has been opened before
    if (label == 'applemusic') label = 'apple';

    if (label === 'deezer' || label === 'kakao' || label === 'yahoojapan'|| label === 'email') {
        $('i#social-icon-modal').addClass('im-filter-'+label);      // old icons
    } else {
        $('i#social-icon-modal').addClass('fab fa-'+label);         // font awesome
    }

    $('#PC_ConfirmSocialModal').modal('show');
}

function toggleActivitySharing(label, labelCapital, turnOff) {
    
    if (labelCapital == 'Email') labelCapital = 'On-site and Application';
    $('.PC_SocialModal_Name, .PC_SocialModal_ToggleActivity').html("");
    $('.PC_SocialModal_Name').text(labelCapital);
    $('.PC_SocialModal_ToggleActivity').text(turnOff ? 'off' : 'on');
    $('.href-channel-activity').attr('href', "/privacycenter/toggleActivitySharing?channel="+label+"&turnOff="+turnOff);
    // ID  matches ID on clicked toggle button
    $('button.cancel-sharing').attr('id', 'toggle-'+label);
    $('#PC_SocialModal_ActivitySharing').modal('show');
}

function cancelActivitySharingChange(e) {
    var toggle = document.getElementById(e.attr('id'));
    // reverting to what it was previous to being clicked on
    toggle.checked = !toggle.checked;
}


function togglePCEmailForm() {
    // Email form appears when user clicks on email icon
    $('.login-email-trigger-modal').on('click', function() {
        $('.pc-emailloginform').slideToggle();
    });
}

function addSpinnerOnClick(buttonClass) {
    $(buttonClass).one('click', function() {
        $(this).append("<br><div class='spinner-border text-light' role='status'></div>");
    });
}
