jQuery(document).ready(function ($) {
	var eavbtn = document.getElementById("evav-clear-cookie");
	if ( typeof eavbtn !== 'undefined' && eavbtn !== null ) {
		if ( Cookies.get('eav-age-verified') === undefined ) {
			eavbtn.innerHTML = "No Cookie Set";
			eavbtn.disabled = true;
		} else {
			eavbtn.innerHTML = "Clear Your Cookie";
			eavbtn.disabled = false;
		}
	}

	// Page Targeting drop-down toggle
	// Get references to the radio buttons and dropdown
	var radioButtons = $('input[name="_evav_pagetargeting_option[option]"]');
	var dropdown = $('#_evav_pagetargeting_option');
	var pageLabel = $('#page-dropdown-label');
    var categoryLabel = $('#category-dropdown-label');
    var categorySelect = $('#_evav_pagetargeting_categories');
    var tagLabel = $('#tag-dropdown-label');
    var tagSelect = $('#_evav_pagetargeting_tags');
    var pagesLabel = $('#pages-dropdown-label');
    var pagesSelect = $('#_evav_pagetargeting_pages');
	// Function to toggle the visibility of the dropdown based on the selected radio button
    function toggleDropdownVisibility(speed) {
		// Check which radio button is selected
		var selectedOption = $('input[name="_evav_pagetargeting_option[option]"]:checked').val();
		// If the "Show on this page only" or "Show everywhere except this page" option is selected, show the dropdown
		if (selectedOption === 'include' || selectedOption === 'exclude') {
			if (speed === 'fast') {
				pageLabel.fadeIn(); // Fade in the label
				dropdown.fadeIn(); // Show the dropdown
                toggleTargetingDetails('fast');
			} else if (speed === 'slow') {
				pageLabel.fadeIn(); // Fade in the label
				dropdown.fadeIn(); // Show the dropdown
                toggleTargetingDetails('slow');
			}
		} else {
			if (speed === 'fast') {
			pageLabel.hide(); // Fade in the label
			dropdown.hide(); // Show the dropdown
            hideTargetingDetails();
		} else if (speed === 'slow') {
			pageLabel.fadeOut(); // Fade in the label
			dropdown.fadeOut(); // Show the dropdown
            hideTargetingDetails();
		}
		}
	}

    function hideTargetingDetails() {
        categoryLabel.hide();
        categorySelect.hide();
        tagLabel.hide();
        tagSelect.hide();
        pagesLabel.hide();
        pagesSelect.hide();
    }

    function toggleTargetingDetails(speed) {
        var pageId = dropdown.val();
        var showCategories = pageId === '9999997';
        var showTags = pageId === '9999996';
        var showPages = pageId === '9999995';
        if (showCategories) {
            if (speed === 'fast') {
                categoryLabel.show();
                categorySelect.show();
            } else {
                categoryLabel.fadeIn();
                categorySelect.fadeIn();
            }
        } else {
            categoryLabel.hide();
            categorySelect.hide();
        }

        if (showTags) {
            if (speed === 'fast') {
                tagLabel.show();
                tagSelect.show();
            } else {
                tagLabel.fadeIn();
                tagSelect.fadeIn();
            }
        } else {
            tagLabel.hide();
            tagSelect.hide();
        }

        if (showPages) {
            if (speed === 'fast') {
                pagesLabel.show();
                pagesSelect.show();
            } else {
                pagesLabel.fadeIn();
                pagesSelect.fadeIn();
            }
        } else {
            pagesLabel.hide();
            pagesSelect.hide();
        }
    }

	// Add an event listener for when the form is submitted
    $('.evav-settings-form').on('submit', function() {
        // Get the selected option
        var option = $('input[name="_evav_pagetargeting_option[option]"]:checked').val();

        // If the selected option is "include" or "exclude"
        if (option === 'include' || option === 'exclude') {
              // Check if a page is selected
              var pageId = $('#_evav_pagetargeting_option').val();
              if (pageId === '0') {
                   // If no page is selected, display a warning message
                   alert('Please select a page.');
                   return false; // Prevent form submission
              }

              if (pageId === '9999997' && (!categorySelect.val() || categorySelect.val().length === 0)) {
                  alert('Please select at least one category.');
                  return false;
              }

              if (pageId === '9999996' && (!tagSelect.val() || tagSelect.val().length === 0)) {
                  alert('Please select at least one tag.');
                  return false;
              }

              if (pageId === '9999995' && (!pagesSelect.val() || pagesSelect.val().length === 0)) {
                  alert('Please select at least one page.');
                  return false;
              }
        }
    });

	// Initial call to toggleDropdownVisibility to set initial visibility
	toggleDropdownVisibility('fast');
    toggleTargetingDetails('fast');

	// Add event listener to each radio button to listen for changes
	radioButtons.on('change', function() {
		toggleDropdownVisibility('slow');
	});

    // Update category/tag/page selectors when dropdown changes
    dropdown.on('change', function() {
        toggleTargetingDetails('slow');
    });

    /* Testing the Logo Image onLoad */
    var csl_logo_url_val = $("#evav_logo_field_id").val();
    csl_logo_testImage(csl_logo_url_val);

    function csl_logo_testImage(URL) {
        if (URL != "Select Logo" && URL) {
            var tester = new Image();
            tester.onerror = csl_logo_imageNotFound;
            tester.src = URL;
        }
    }

    function csl_logo_imageNotFound() {
        alert("That image was not found.");
    }

	function toggleBirthdateAgeField() {
		var selectedType = $("input[name='_evav_adult_type']:checked").val();
		if (selectedType === 'birthdate') {
			$('#evav-birthdate-age-row').fadeIn('fast');
		} else {
			$('#evav-birthdate-age-row').fadeOut('fast');
		}
	}

	var previousAdultType = $("input[name='_evav_adult_type']:checked").val();

    function toggleNoButtonSettings() {
        var selectedType = $("input[name='_evav_adult_type']:checked").val();
        var noButtonRows = [
            $('#_evav_disagree_btn_text').closest('tr'),
            $('#_evav_disAgree_btn_bgcolor').closest('tr'),
            $('#_evav_disagree_error_text').closest('tr')
        ];

        if (selectedType === 'birthdate') {
            $.each(noButtonRows, function (_, row) {
                row.hide();
            });
        } else {
            $.each(noButtonRows, function (_, row) {
                row.show();
            });
        }
    }

    function toggleOverlayBoxGradientSettings() {
        var gradientRows = [
            $('#_evav_overlay_box_gradient_color').closest('tr'),
            $('#_evav_overlay_box_gradient_angle').closest('tr')
        ];
        var isBoxEnabled = $('#_evav_enable_overlay_box').is(':checked');
        var isEnabled = $('#_evav_enable_overlay_box_gradient').is(':checked');

        $.each(gradientRows, function (_, row) {
            if (isBoxEnabled && isEnabled) {
                row.show();
            } else {
                row.hide();
            }
        });
    }

    function toggleOverlayBoxSettings() {
        var isBoxEnabled = $('#_evav_enable_overlay_box').is(':checked');
        var boxRows = [
            $('#_evav_overlay_box_color').closest('tr'),
            $('#_evav_enable_overlay_box_gradient').closest('tr'),
            $('#_evav_overlay_box_gradient_color').closest('tr'),
            $('#_evav_overlay_box_gradient_angle').closest('tr')
        ];

        $.each(boxRows, function (_, row) {
            if (isBoxEnabled) {
                row.show();
            } else {
                row.hide();
            }
        });

        if (isBoxEnabled) {
            toggleOverlayBoxGradientSettings();
        }
    }

    function toggleOverlayBackgroundGradientSettings() {
        var gradientRows = [
            $('#_evav_overlay_gradient_color').closest('tr'),
            $('#_evav_overlay_gradient_angle').closest('tr')
        ];
        var isEnabled = $('#_evav_enable_overlay_gradient').is(':checked');

        $.each(gradientRows, function (_, row) {
            if (isEnabled) {
                row.show();
            } else {
                row.hide();
            }
        });
    }

    function toggleGradientStartLabels() {
        var overlayStartLabel = $('#evav-overlay-color-label');
        var boxStartLabel = $('#evav-box-color-label');

        if (overlayStartLabel.length) {
            overlayStartLabel.text($('#_evav_enable_overlay_gradient').is(':checked') ? 'Gradient Start Color' : 'Background Color');
        }

        if (boxStartLabel.length) {
            boxStartLabel.text($('#_evav_enable_overlay_box_gradient').is(':checked') ? 'Gradient Start Color' : 'Box Color');
        }
    }

	toggleBirthdateAgeField();
    toggleNoButtonSettings();
    toggleOverlayBackgroundGradientSettings();
    toggleOverlayBoxSettings();
    toggleGradientStartLabels();

    $('#_evav_enable_overlay_gradient').on('change', function () {
        toggleOverlayBackgroundGradientSettings();
        toggleGradientStartLabels();
    });

    $('#_evav_enable_overlay_box_gradient').on('change', function () {
        toggleOverlayBoxGradientSettings();
        toggleGradientStartLabels();
    });

    $('#_evav_enable_overlay_box').on('change', function () {
        toggleOverlayBoxSettings();
        toggleGradientStartLabels();
    });

	$('#_evav_adult_type_radio input[type="radio"]').on('change', function () {
            if (confirm('Are you sure you want to change this? You will lose any text edits.')) {
            var evavatr = $("input[name='_evav_adult_type']:checked").val();
            if (evavatr == "adult") {
                $('#_evav_heading').val('Please verify you are 18 years or older to enter.');
                $('#_evav_disclaimer').val('WARNING ADULT CONTENT!\nThis website is intended for adults only and may contain content of an adult nature or age restricted, explicit material, which some viewers may find offensive. By entering you confirm that you are 18+ years and are not offended by viewing such material. If you are under the age of 18, if such material offends you or it is illegal to view in your location please exit now.');
                $('#_evav_agree_btn_text').val('I am 18 or older [Enter Site]');
                $('#_evav_disagree_btn_text').val('I am under 18');
            } else if (evavatr == "alcohol") {
                $('#_evav_heading').val('Are you of legal drinking age 21 or older?');
                $('#_evav_disclaimer').val('THE ALCOHOL PRODUCTS ON THIS WEBSITE ARE INTENDED FOR ADULTS ONLY.\nBy entering this website, you certify that you are of legal drinking age in the location in which you reside (age 21+ in the United States).');
                $('#_evav_agree_btn_text').val('Yes I am of legal age');
                $('#_evav_disagree_btn_text').val('No I am under age');
            } else if (evavatr == "vape") {
                $('#_evav_heading').val('Are you of legal smoking age?');
                $('#_evav_disclaimer').val('THE PRODUCTS ON THIS WEBSITE ARE INTENDED FOR ADULTS OF LEGAL SMOKING AGE.\nBy entering this website, you certify that you are of legal smoking age in the location in which you reside (age 18+, 19+ and 21+ in some areas).');
                $('#_evav_agree_btn_text').val('Yes I am of legal age');
                $('#_evav_disagree_btn_text').val('No I am under age');
            } else if (evavatr == "birthdate") {
                $('#_evav_heading').val('Please verify your age before entering.');
                $('#_evav_disclaimer').val('');
                $('#_evav_agree_btn_text').val('Enter');
                $('#_evav_disagree_btn_text').val('No I am under age');
            }
            previousAdultType = evavatr;
			toggleBirthdateAgeField();
            toggleNoButtonSettings();
        } else {
        		$("input[name='_evav_adult_type'][value='" + previousAdultType + "']").prop('checked', true);
        		toggleBirthdateAgeField();
        		toggleNoButtonSettings();
        		return false;
		}
	});

    $('#evav_logo_button').click(function (e) {
        e.preventDefault();
        var csl_CustomSiteLogo_uploader = wp.media({
            title: 'Select or upload a logo',
            button: {text: 'Select Logo'},
            multiple: false
        }).on('select', function () {
            var attachment = csl_CustomSiteLogo_uploader.state().get('selection').first().toJSON();
            $('#evav_logo_field_id').val(attachment.url);
            $('.evav_logo_container').html("<IMG SRC='" + attachment.url + "'><BR>Save Changes to Set Logo");
        }).open();
    });

    $('#evav_logo_delete_button').click(function (e) {
        e.preventDefault();
        $('#evav_logo_field_id').val('');
        $('.evav_logo_container').html("Save Changes to Remove Logo");
    });

    $('#_evav_overlay_color').wpColorPicker();
    $('#_evav_overlay_box_color').wpColorPicker();
    $('#_evav_overlay_gradient_color').wpColorPicker();
    $('#_evav_overlay_box_gradient_color').wpColorPicker();
    $('#_evav_agree_btn_bgcolor').wpColorPicker();
    $('#_evav_disAgree_btn_bgcolor').wpColorPicker();
    // show character count
    $.fn.maxLen = function (maxLen) {
        var elm = $(this);
        var textSelector = Math.random().toString(10).substr(2);
        if (maxLen == null)
            var maxLen = $(elm).attr('maxlength');

        $(elm).after('<div id="txt-length-left' + textSelector + '"></div>');
        elm.keypress(function (event) {
            var Length = elm.val().length;
            var AmountLeft = maxLen - Length;
            $('#txt-length-left' + textSelector).html(AmountLeft + " Characters left");
            if (Length - 1 >= maxLen) {
				$('#txt-length-left' + textSelector).html("0 Characters left");
                if (event.which != 8) {
                    return false;
                }
            }
        });
    };

    // Disclaimer text box
    var $body = $('body');
    $body.find('#_evav_headline').maxLen();
    $body.find('#_evav_description').maxLen();
    $body.find('#_evav_heading').maxLen();
    $body.find('#_evav_disclaimer').maxLen();
    $body.find('#_evav_agree_btn_text').maxLen();
    $body.find('#_evav_disagree_btn_text').maxLen();
    $body.find('#_evav_disagree_error_text').maxLen();


    $('.evavpremhovertip').hover(function(e){ // Hover event
        var titleText = $(this).attr('title');
        $(this).data('tiptext', titleText).removeAttr('title');
        $('<p class="evavpremtooltip" style="display: none; z-index:999; position: absolute; padding: 10px; color: #555; background-color: #fff; border: 1px solid #777;	box-shadow: 0 1px 3px 1px rgba(0,0,0,0.5); border-radius: 3px;"></p>').text(titleText).appendTo('body').css('top', (e.pageY - 10) + 'px').css('left', (e.pageX + 20) + 'px').fadeIn('slow');
    }, function(){ // Hover off event
        $(this).attr('title', $(this).data('tiptext'));
        $('.evavpremtooltip').remove();
    }).mousemove(function(e){ // Mouse move event
        $('.evavpremtooltip').css('top', (e.pageY - 10) + 'px').css('left', (e.pageX + 20) + 'px');
    });

    $('.evavoptionshovertip').hover(function(e){ // Hover event
        var titleText = $(this).attr('title');
        $(this).data('tiptext', titleText).removeAttr('title');
        $('<p class="evavoptiontooltip"></p>').text(titleText).appendTo('body').css('top', ($(this).offset().top  - 25) + 'px').css('left', ($(this).offset().left + 5 ) + 'px').fadeIn('slow');
    }, function(){ // Hover off event
        $(this).attr('title', $(this).data('tiptext'));
        $('.evavoptiontooltip').remove();
    });

});

function evav_clear_cookie() {
	var eavbtn = document.getElementById("evav-clear-cookie");
	console.debug('Clearing cookie!');
	document.cookie='eav-age-verified=;Path=/;Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
	console.debug('Cleared!');
	eavbtn.innerHTML = "Cookie Cleared";
	eavbtn.disabled = true;
	setTimeout(function(){
		eavbtn.innerHTML = "No Cookie Set";
	}, 4000);
	return false;
}

// Embed jQuery js-cookies file to remove remote call
/*!
 * JavaScript Cookie v2.2.1
 * Minified by jsDelivr using Terser v3.14.1.
 * Original file: /npm/js-cookie@2.2.1/src/js.cookie.js
 */
!function(e){var n;if("function"==typeof define&&define.amd&&(define(e),n=!0),"object"==typeof exports&&(module.exports=e(),n=!0),!n){var t=window.Cookies,o=window.Cookies=e();o.noConflict=function(){return window.Cookies=t,o}}}(function(){function e(){for(var e=0,n={};e<arguments.length;e++){var t=arguments[e];for(var o in t)n[o]=t[o]}return n}function n(e){return e.replace(/(%[0-9A-Z]{2})+/g,decodeURIComponent)}return function t(o){function r(){}function i(n,t,i){if("undefined"!=typeof document){"number"==typeof(i=e({path:"/"},r.defaults,i)).expires&&(i.expires=new Date(1*new Date+864e5*i.expires)),i.expires=i.expires?i.expires.toUTCString():"";try{var c=JSON.stringify(t);/^[\{\[]/.test(c)&&(t=c)}catch(e){}t=o.write?o.write(t,n):encodeURIComponent(String(t)).replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g,decodeURIComponent),n=encodeURIComponent(String(n)).replace(/%(23|24|26|2B|5E|60|7C)/g,decodeURIComponent).replace(/[\(\)]/g,escape);var f="";for(var u in i)i[u]&&(f+="; "+u,!0!==i[u]&&(f+="="+i[u].split(";")[0]));return document.cookie=n+"="+t+f}}function c(e,t){if("undefined"!=typeof document){for(var r={},i=document.cookie?document.cookie.split("; "):[],c=0;c<i.length;c++){var f=i[c].split("="),u=f.slice(1).join("=");t||'"'!==u.charAt(0)||(u=u.slice(1,-1));try{var a=n(f[0]);if(u=(o.read||o)(u,a)||n(u),t)try{u=JSON.parse(u)}catch(e){}if(r[a]=u,e===a)break}catch(e){}}return e?r[e]:r}}return r.set=i,r.get=function(e){return c(e,!1)},r.getJSON=function(e){return c(e,!0)},r.remove=function(n,t){i(n,"",e(t,{expires:-1}))},r.defaults={},r.withConverter=t,r}(function(){})});
