// source --> https://biggestnumber.com/wp-content/plugins/pebbles-games/game.js?ver=1683753847 
// JavaScript Document

jQuery(document).ready(function($) {
	"use strict";
	
	var score = 0;
	
	//----- CLICK ON RESPONSES
	// click repsonses
	if($(".question-box .responses span").length){
		$('.question-box .responses span').click(function(e){
			var clickedButton = $(this);
			var questionBox = $(this).parents('.question-box');
			var questionContent = questionBox.find('.question-content');
			var explainBoxContent = questionBox.find('.question-explanation').html();
			
			// fade in response result
			questionContent.fadeOut(function(){
				$(this).html(explainBoxContent);
				questionBox.addClass('answered');

				// style correct or incorrect
				if(!clickedButton.hasClass('correct')){
					questionBox.addClass('incorrect');
					questionBox.find('i').removeClass('fa-check-circle').addClass('fa-times-circle');
				}
			}).fadeIn();
			
			// score the response
			if(clickedButton.hasClass('correct')){
				score++;
			}

			// Cancel the default action
			e.preventDefault();
		});
	}
	
	//----- CLICK ON NEXT QUESTION
	// click repsonses
	$('body').on('click', '.question-box .next-question', function(e){
		var questionBox = $(this).parents('.question-box');
		var nextQuestionBox = questionBox.next('.question-box');
		
		// update score
		if($(this).hasClass('finish-quiz')){
			var numQuestions = parseInt($('.percent-score').data('numquestions'));
			var percentScore = Math.round((score / numQuestions) * 100);
			$('.percent-score').text(percentScore + '%');
		}

		// show next questions
		questionBox.fadeOut(300);
		nextQuestionBox.delay(300).fadeIn(300);

		// Cancel the default action
		e.preventDefault();
	});
	
});
// source --> https://biggestnumber.com/wp-content/plugins/recaptcha-woo/js/rcfwc.js?ver=1.0 
/* Woo Checkout */
jQuery( document ).ready(function() {
    jQuery( document.body ).on( 'update_checkout updated_checkout applied_coupon_in_checkout removed_coupon_in_checkout checkout_error', function() {
        if(jQuery('.g-recaptcha').length > 0) {
            if (typeof grecaptcha !== "undefined" && typeof grecaptcha.reset === "function") {
                var count = 0;
                jQuery(".g-recaptcha").each(function () {
                    grecaptcha.reset(count);
                    count++;
                });
            }
        }
    });
});

/* Woo Checkout Block */
(function() {
    document.addEventListener('DOMContentLoaded', function() {
        // Global callbacks used by auto-rendered v2 widgets in the block checkout
        window.rcfwcRecaptchaCallback = function(token) {
            try {
                if (typeof wp !== 'undefined' && wp.data) {
                    wp.data.dispatch('wc/store/checkout').__internalSetExtensionData('rcfwc', { token: token });
                }
            } catch (e) {}
        };
        window.rcfwcRecaptchaExpired = function() {
            try {
                if (typeof wp !== 'undefined' && wp.data) {
                    wp.data.dispatch('wc/store/checkout').__internalSetExtensionData('rcfwc', { token: '' });
                }
            } catch (e) {}
        };

        // Try to render explicitly if needed once the blocks mount/update
        if (typeof wp !== 'undefined' && wp.data) {
            var unsubscribe = wp.data.subscribe(function() {
                var el = document.getElementById('g-recaptcha-woo-checkout');
                if (!el) {
                    return;
                }
                // If already rendered (has inner HTML/iframe), stop listening
                if (el.innerHTML && el.innerHTML.trim() !== '') {
                    unsubscribe && unsubscribe();
                    return;
                }
                // Render explicitly with callbacks if the API is ready
                if (typeof grecaptcha !== 'undefined' && typeof grecaptcha.render === 'function') {
                    try {
                        grecaptcha.render(el, {
                            sitekey: el.getAttribute('data-sitekey'),
                            callback: rcfwcRecaptchaCallback,
                            'expired-callback': rcfwcRecaptchaExpired
                        });
                    } catch (e) {
                        // Ignore if already rendered or API not ready
                    }
                    unsubscribe && unsubscribe();
                }
            }, 'wc/store/cart');
        }
    });
})();