$(document).ready(function() {
	if (typeof(window.CM) === 'undefined') {
		embedVideoServices({ youtube:true, vimeo:true });
	} else {
		let inputYoutube = document.querySelectorAll('.consent-input[data-storage-key="youtube"]');
		if(!inputYoutube.length) {
			embedVideoServices({ youtube:true });
		}
		let inputVimeo = document.querySelectorAll('.consent-input[data-storage-key="vimeo"]');
		if(!inputVimeo.length) {
			embedVideoServices({ vimeo:true });
		}
	}
});
document.addEventListener('consent.ready', function(e) {
	embedVideoServices(e.detail);
});
document.addEventListener('consent.updated', function(e) {
	embedVideoServices(e.detail);
});
function embedVideoServices(detail) {
	if (detail !== null && typeof detail.youtube !== 'undefined' && detail.youtube === true) {
		let wrappers = document.querySelectorAll('div.needs-consent.youtube');
		for (let k = 0; k < wrappers.length; ++k) {
			wrappers[k].classList.remove('d-none');
		}
		let embeds = document.querySelectorAll('iframe.needs-consent.youtube, script[data-consent="youtube"]');
		for (let i = 0; i < embeds.length; ++i) {
			embeds[i].src = embeds[i].dataset.src;
			embeds[i].className = 'youtube';
		}
		let notices = document.querySelectorAll('a[data-consent="youtube"]');
		for (let j = 0; j < notices.length; ++j) {
			notices[j].classList.add('d-none');
		}
	}
	if (detail !== null && typeof detail.vimeo !== 'undefined' && detail.vimeo === true) {
		let wrappers = document.querySelectorAll('div.needs-consent.vimeo');
		for (let k = 0; k < wrappers.length; ++k) {
			wrappers[k].classList.remove('d-none');
		}
		let embeds = document.querySelectorAll('iframe.needs-consent.vimeo, script[data-consent="vimeo"]');
		for (let i = 0; i < embeds.length; ++i) {
			embeds[i].src = embeds[i].dataset.src;
			embeds[i].className = 'vimeo';
		}
		let notices = document.querySelectorAll('a[data-consent="vimeo"]');
		for (let j = 0; j < notices.length; ++j) {
			notices[j].classList.add('d-none');
		}
	}
}
function embedYoutube(detail) {
	embedVideoServices(detail);
}