<!--
<?php echo $text_license; ?>
-->

<div class="buttons">
	<div class="pull-right">
		<input type="button" value="<?php echo $button_confirm; ?>" id="button-confirm" class="btn btn-primary show-popup" data-loading-text="<?php echo $text_loading; ?>" />
	</div>
</div>

<!-- loader -->
<style>
    .TrustPayLoader {
        display:none; 
        position:fixed; 
        z-index: 999999; 
        width:1px; 
        height:1px;  
        top: 50%; 
        left: 50%; 
    }

    .loaderContent {
        position:relative; 
        width:200px; 
        height:150px; 
        top:-50px; 
        left:-100px; 
        background:white; 
        padding:20px; 
        border:3px solid #24CEFF; 
        border-radius: 25px;
    }
</style>
    
<div class="TrustPayLoader">
    <div class="loaderContent">
        <img src="catalog/view/theme/default/image/trustpay_loader.gif" style="width:160px; height:120px; max-height: none;" alt=""/>
    </div>
</div>

<script type="text/javascript">
    // loader
    var trustpayTs;

    function onTrustPayPopupLoaded() {
        // v opencart 2.3 ide nacitanie iframe skor ako kliknem na button, co ho zobrazi 
        showLoader();
    }

    function showLoader() {
        jQuery('.TrustPayLoader').show();
        trustpayTs = setTimeout(hideLoader, 1000);
    }

    function hideLoader() {
        jQuery('.TrustPayLoader').hide();
        clearTimeout(trustpayTs);
    }

</script>

<!-- loader end -->

<!--<script type="text/javascript" src="https://mapi.trustpay.eu/mapi5/Scripts/TrustPay/popup.js"></script>--><input type="submit" value="" />
<script type="text/javascript">
const POPUP_OPENED_MSG_RESEND_INTERVAL_MS = 500;
if(typeof POPUP_OPENED_MSG_RESEND_INTERVAL_MS == "undefined") { // toto v popup.js chyba a sposobovalo to zlobu, preto som musel to sem dat takto
    const POPUP_OPENED_MSG_RESEND_INTERVAL_MS = 500;
}
var isPopupOpenedMessageHandled = undefined;

function getTrustPayFrame() {
    return document.getElementById("TrustPayFrame");
}

jQuery(document).ready(function () {
    var iframe = getTrustPayFrame();

    if (iframe != null) {
        iframe.setAttribute("allow", "clipboard-write *; payment");
        iframe.allowPaymentRequest = true;
        iframe.style.position = 'fixed';
        iframe.style.left = 0;
        iframe.style.right = 0;
        iframe.style.top = 0;
        iframe.style.bottom = 0;
        iframe.style.width = 0;
        iframe.style.height = 0;
        iframe.style.border = 'none';
        iframe.style.opacity = 0;
        iframe.style.transition = 'opacity .5s ease-in-out';
        iframe.style.zIndex = '99999';
    }

    if (window.addEventListener) {
        window.addEventListener("message", onMessage, false);
    }
    else if (window.attachEvent) {
        window.attachEvent("onmessage", onMessage, false);
    }

    jQuery(".show-popup").on("click",
        function (e) {
            e.preventDefault();
            openPopup();
        });
});

function onMessage(event) {
    var data = event.data;

    if (typeof (window[data.func]) === "function") {
        window[data.func].call(null, data.message);
    }
    else if (data === "popupLoaded") {
        if (typeof onTrustPayPopupLoaded === "function") {
            onTrustPayPopupLoaded();
        }
    }
    else if (data === "getFqdn") {
        event.source.postMessage({
            type: data,
            fqdn: window.location.hostname
        }, event.origin);
    }
}

function openPopup() {
    console.log("openPopup()");
    var iframe = getTrustPayFrame();
    iframe.style.opacity = 1;
    iframe.style.width = '100%';
    iframe.style.height = '100%';
    iframe.focus();
    iframe.contentWindow.postMessage('focus-event', '*');
    iframe.contentWindow.postMessage('redirectMessage', '*');
    iframe.contentWindow.postMessage('authorizationMessage', '*');

    isPopupOpenedMessageHandled = false;
    postPopupOpenedMsgWithHandlingCheck();
}

function postPopupOpenedMsgWithHandlingCheck() {
    if (isPopupOpenedMessageHandled === false) {
        getTrustPayFrame().contentWindow.postMessage('popupOpenedMessage', '*');
        setTimeout(function () { postPopupOpenedMsgWithHandlingCheck(); }, POPUP_OPENED_MSG_RESEND_INTERVAL_MS);
    }
}

function setPopupOpenedMessageHandled() {
    isPopupOpenedMessageHandled = true;
}

function closePopup(message) {
    isPopupOpenedMessageHandled = undefined;

    var iframe = document.getElementById('TrustPayFrame');
    iframe.style.opacity = 0;

    // Delay execution so that the iframe changes dimension only after opacity is 0
    setTimeout(function() {
        iframe.style.width = 0;
        iframe.style.height = 0;
    }, 1000);
}

function redirectPopup(url) {
    window.top.location.href = url;
}
</script>


<iframe id="TrustPayFrame" src="<?php echo $action_url; ?>" allow="payment" style="position: fixed;"></iframe>
<script type="text/javascript">
	jQuery(window).on("message", function(event) {
		if (typeof _QuickCheckout !== "undefined" && event.originalEvent.data.func === "closePopup") {
			_QuickCheckout.payment();
			jQuery(".journal-loading-overlay").remove();
		}
	});

	
	if (typeof _QuickCheckout !== "undefined" && !document.getElementById("trustpay_bt_css")) {
		var trustpay_css = document.createElement("style");
		trustpay_css.setAttribute("id", "trustpay_bt_css")
		trustpay_css.appendChild(document.createTextNode(".payment-trustpay_bt {padding:0;margin:0;border:none} .payment-trustpay_bt .title, .payment-trustpay_bt .alert, .payment-trustpay_bt .quick-checkout-payment > .btn {display:none!important}"));
		document.head.appendChild(trustpay_css);
	}
	
</script>