jQuery(document).ready(function($){
	var igScotiabankForm = document.getElementById("ig-scotiabank-form");
	var scotiaPaymentForm = document.getElementById("scotia-payment-form");
	var hasSubmittedScotiaForm = false;

	setTimeout(function () {
		if (igScotiabankForm) {
		   igScotiabankForm.scrollIntoView({
		       behavior : "smooth",
		       block    : "center",
		       inline   : "nearest"
		   });
		}
	}, 2000);


	$("#payment_method_ig_scotiabank").click();
	if (scotiaPaymentForm) {
		// Keep hosted-form submission one-shot to avoid duplicate gateway session starts.
		if (!hasSubmittedScotiaForm) {
			hasSubmittedScotiaForm = true;
			scotiaPaymentForm.submit();
		}
	}
    
    
    // triggering payment method change
    $(document).on('change', '#payment input[name="payment_method"]', function(){
        
        let is_sb_selected = $(this).val() == 'ig_scotiabank';
        
        $('#place_order').toggle( ! is_sb_selected );
        
    });
    
});

var scotiaHostedFrame = document.getElementById("ig-scotiabank-hosted-frame");
var placeOrderButton = document.getElementById("place_order");
// Pay/order screens can render different DOM structures, so only act when required nodes exist.

if (scotiaHostedFrame) {
	scotiaHostedFrame.onload = function() {

		jQuery( function($) {
        
			$('#ig-scotiabank-form').hide();
        
			var $orderReview = $("#order_review");
			var canUnblock = typeof $.fn.unblock === "function";

			// Checkout fragments vary, so only unblock when blockUI exists and this section is currently blocked.
			if (canUnblock && $orderReview.length > 0) {
				var isBlocked  = $orderReview.data("blockUI.isBlocked");

				if (1 == isBlocked) {
					$orderReview.unblock();
				}
			}
        
		});

		if ( "none" == scotiaHostedFrame.style.display ) {
        
			scotiaHostedFrame.style.display = "block";
		}
    
		if (placeOrderButton) {
			placeOrderButton.style.display = "none";
		}
	};
}
