<script type="text/javascript">
function calculateFrameHeight(iframeElement){
    setTimeout(function() {
        var the_height = iframeElement.contentWindow.document.body.scrollHeight;
        iframeElement.height = the_height;
    }, 0);
}
function calculateFrameHeightForPopup(iframeElement){
    setTimeout(function() {
        var the_height = iframeElement.contentWindow.document.body.scrollHeight + 100;
        iframeElement.height = the_height;
        iframeElement.style.height = the_height;
        $(iframeElement.parentElement).height(the_height+'px');
    }, 0);
}
{% if nested %}
function openPopup(id, frameId, url) {
    window.parent.postMessage({
        type: 'openPopup',
        id: id,
        frameId: frameId,
        url: url
    });
}
{% else %}
window.addEventListener("message", function(event) {
    console.log(event);
    var data = event.data;
    if (data.type == 'openPopup') {
        $(data.frameId).attr('src', data.url);
        $(data.id).modal();
    }
    if (data.type == 'closePopup') {
        setTimeout(function() {
            var frameId = data.frameId;
            $(frameId).attr('src', $(frameId).attr('src'));
            $('.modal').modal('hide');
        }, 200);
    }
});
{% endif %}

</script>