$(document).ready(function() {
    
    $(document).on('click', '.js-close-custom-popup', function() {
        hideCustomPopup($(this).closest('.custom-popup'));
    });

    $(document).keydown(function(e) {
        var key = e.keyCode || e.which;
        if(key == 27) {
            hideCustomPopup($('.custom-popup.is-visible'));
        }
    });

});

function showCustomPopup($elem) {
    $($elem).fadeIn().addClass('is-visible');
    $('body').addClass('overlayered');
}

function checkCustomPopupSize($elem) {
    var contH = $($elem).find('.custom-popup__content').height();
    console.log('content-height: ' + contH);
    console.log('window-height: ' + $(window).height());
    if(contH > $(window).height() * 0.9) {
        $($elem).addClass('top-7r');
    }else {
        $($elem).removeClass('top-7r');
    }
}

function hideCustomPopup($popup) {
    $popup.fadeOut().removeClass('is-visible');
    $('body').removeClass('overlayered');
    $popup.find('.tree__hypo-elem__inserting').remove();
    $popup.find('.tree__hypo-elem.inserted').removeClass('inserted').html('');
}