


jQuery(document).ready(function ($) {

    // Check for global variable settings
    let $body = $('body'),
        $overlay = $('#emav-overlay-wrap');
    if ( emav_ajax_object !== undefined && Cookies.get('age-verified') === undefined ) {
        // set status
        $.get( emav_ajax_object.ajax_url, { action: 'emav_get_status' })
            .success( function(resp) {
                let status = resp;
                console.debug(
                    status,
                    $.inArray( status , [ 'all', 'guests' ]) > -1,
                    $body.hasClass('logged-in') !== false,
                    status !== 'disabled'
                );
                if ( $.inArray( status , [ 'all', 'guests' ]) > -1 ){
                    console.debug( status === 'all',
                        status === 'guests', false !== $body.hasClass('logged-in') );
                    if ( status === 'all' ) {
						$body.css('position', 'fixed');
						$body.css('width', '100%');
						$overlay.show();
                        $('#confirm_age').focus();
					}
                    else if ( $body.hasClass('logged-in') !== true && status === 'guests') {
						$body.css('position', 'fixed');
						$body.css('width', '100%');
                        $overlay.show();
                        $('#confirm_age').focus();
					}
                }
            });
    }

    $("#confirm_age").on('click touch', function () {
		// Setting cookie options
		let options = {path: location.pathname};

		Cookies.set('age-verified', 1, options);

		$body.css('position', '');
		$body.css('width', '');
		$overlay.hide();
    });

    $("#not_confirm_age").on('click touch', function () {

        $("#emav_verify_confirm").val('0');

        var emav_verify_confirm = $("#emav_verify_confirm").val();

        $.ajax({
            url: emav_ajax_object.ajax_url,
            type: 'post',
            data: {
                action: 'emav_user_age_verify',
                verifyConfirm: emav_verify_confirm
            },
            success: function (response) {
                if (response === 'verified') {
                    // window.wpCookies.remove('age-verified');
                    window.reload();
                } else {
                    $(".error").html(response);
                }
            }
        });
    });
});
