function lazyload() {
	let lazyImages = [].slice.call(document.querySelectorAll("img.lazy"));
	let lazyIframes = [].slice.call(document.querySelectorAll("iframe.lazy"));
    if (lazyactive === false) {
		  
		lazyactive = true;

		/*if(lazyIframes.length === 0 && lazyIframes.length === 0)
		{
			document.removeEventListener("scroll", lazyload);
			window.removeEventListener("resize", lazyload);
			window.removeEventListener("orientationchange", lazyload);
		}*/
	  
        lazyImages.forEach(function(lazyImage) {
			if ((lazyImage.getBoundingClientRect().top <= window.innerHeight && lazyImage.getBoundingClientRect().bottom >= 0) && getComputedStyle(lazyImage).display !== "none" && lazyImage.src != lazyImage.dataset.src) {

				lazyImage.src = lazyImage.dataset.src;
				if(lazyImage.dataset.srcset){
					lazyImage.srcset = lazyImage.dataset.srcset;
				}

				lazyImage.addEventListener('load', function(){
					lazyImage.classList.remove("lazy");
					lazyImage.parentElement.classList.add("loaded");
					this.parentElement.style.maxHeight = "unset";
					setTeaserHeight();
				});
			}
        });

		lazyIframes.forEach(function(lazyIframe){
			if ((lazyIframe.getBoundingClientRect().top <= window.innerHeight && lazyIframe.getBoundingClientRect().bottom >= 0) && getComputedStyle(lazyIframe).display !== "none" && lazyIframe.src != lazyIframe.dataset.src) {

				lazyIframe.src = lazyIframe.dataset.src;
				if(lazyIframe.dataset.srcset){
					lazyIframe.srcset = lazyIframe.dataset.srcset;
				}
  
				lazyIframe.addEventListener('load', function(){
					lazyIframe.classList.remove("lazy");
					lazyIframe.parentElement.classList.add("loaded");
					this.parentElement.style.maxHeight = "unset";
				});
			}
		});

        lazyactive = false;
    }
};

document.addEventListener("DOMContentLoaded", function() {
	lazyactive = false;
	lazyload();
	document.addEventListener("scroll", lazyload);
	window.addEventListener("resize", lazyload);
	window.addEventListener("orientationchange", lazyload);
});
window.addEventListener("tac.root_available", function() {
	for(var id in tarteaucitron.services){
		document.addEventListener(tarteaucitron.services[id].key+'_loaded', function() {
			lazyload();
		});
	}
});