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

	/* Testing the Logo Image onLoad */
	var csl_logo_url_val = $("#emav_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.");
	}


//	var $emavField = $('.emav-age-header-option'),
//		$emavInput = $('.emav-age-header-option input'),
//		$emavHeader = $('#_emav_age_header'),
//		$emavOption = $('input[name=_emav_user_age_verify_option]:checked');
	// $('.emav-age-header-option input').on('change', function() {
	// 	alert($('input[name=_emav_user_age_verify_option]:checked').val());
	// });
//	$emavInput.on('change',function () {
//		let option = parseInt($('input[name=_emav_user_age_verify_option]:checked').val()),
//			text = emav_header_default_text[1];
//		if (option === (1||2||3)) {
//			text = emav_header_default_text[1];
//		} else if (option === 4){
//			text = emav_header_default_text[2];
//		}
//		else if (option === 5){
//			text = emav_header_default_text[3];
//		}
//		console.debug(option,
//			text,
//			$emavHeader.val(text));
//		$emavHeader.val(text).change();
//	});

	$('#emav_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();
			$('#emav_logo_field_id').val(attachment.url);
			$('.emav_logo_container').html("<IMG SRC='" + attachment.url + "'><BR>Save Changes to Set Logo");
		}).open();
	});

	$('#emav_logo_delete_button').click(function (e) {
		e.preventDefault();
		$('#emav_logo_field_id').val('');
		$('.emav_logo_container').html("Save Changes to Remove Logo");
	});

	/* Check Image Field */
	/*$('.emav-settings-form').on('submit', function () {
        var emav_logo_field_id = $('input#emav_logo_field_id').attr('value');    // Getting Width Value

        if ((emav_logo_field_id === '' || emav_logo_field_id === null)) {
            alert("Please select the img.");
            return false;
        }


    });*/

	$('#_emav_overlay_color').wpColorPicker();
	$('#_emav_agree_btn_bgcolor').wpColorPicker();
	$('#_emav_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;
			/* alert(AmountLeft) */
			$('#txt-length-left' + textSelector).html(AmountLeft + " Characters left");
			if (Length >= maxLen) {
				if (event.which != 8) {
					return false;
				}
			}
		});
	};
	// Disclaimer text box
	var $body = $('body');
	$body.find('#_emav_headline').maxLen();
	$body.find('#_emav_description').maxLen();
	$body.find('#_emav_heading').maxLen();
	$body.find('#_emav_disclaimer').maxLen();
});