function SetOpacity(object,opacityPct)
{
	// IE.
	object.style.filter = 'alpha(opacity=' + opacityPct + ')';
	// Old mozilla and firefox
	object.style.MozOpacity = opacityPct/100;
	// Everything else.
	object.style.opacity = opacityPct/100;
}

function ChangeOpacity(id,msDuration,msStart,fromO,toO)
{
	var element=document.getElementById(id);
	var opacity = element.style.opacity * 100;
	var msNow = (new Date()).getTime();
	opacity = fromO + (toO - fromO) * (msNow - msStart) / msDuration;
	if (opacity<0) 
		SetOpacity(element,0)
	else if (opacity>100)
		SetOpacity(element,100)
	else
	{
		SetOpacity(element,opacity);
		element.timer = window.setTimeout("ChangeOpacity('" + id + "'," + msDuration + "," + msStart + "," + fromO + "," + toO + ")",1);
	}
}

function FadeInImage(foregroundID,newImage,backgroundID)
{
	var foreground=document.getElementById(foregroundID);
	if (backgroundID)
	{
		var background=document.getElementById(backgroundID);
		if (background)
		{
		background.style.backgroundImage = 'url(' + foreground.src + ')';
		background.style.backgroundRepeat = 'no-repeat';
		}
	}
	SetOpacity(foreground,0.3);
	foreground.src = newImage;
	if (foreground.timer) window.clearTimeout(foreground.timer); 
	var startMS = (new Date()).getTime();
	foreground.timer = window.setTimeout("ChangeOpacity('" + foregroundID + "',8000," + startMS + ",0,200)",10);
}


function popunderFunction() {
	var links = ["https://kadampafestivals.org/spring?utm_source=nkt-ads&utm_medium=display&utm_content=wideskyscraper&utm_campaign=Spring2025","https://kadampafestivals.org/fall?utm_source=nkt-ads&utm_medium=display&utm_content=wideskyscraper&utm_campaign=fall2025","https://kadampafestivals.org/summer?utm_source=nkt-ads&utm_medium=display&utm_content=wideskyscraper&utm_campaign=summer2025"];

    
    
    
    
        var images = ["https://api.kadampa.org/images/banners/festivals/Spring-2025-ENG-Pop-Under-2160x900.jpg","https://api.kadampa.org/images/banners/festivals/Fall-2025-ENG-Pop-Under-2160x900-2x.jpg","https://api.kadampa.org/images/banners/festivals/Summer-2025-ENG-Pop-Under-2160x900-2x.jpg"];
    
	 
    

	var i = 1;
	var mouseOntheAd = false;

	// If bannerImage_3 and bannerLink_3 are null, we need to create this elements
	if (document.getElementById("bannerImage_3") == null & document.getElementById("bannerLink_3") == null) {

		// Creation of bannerLink
		var bannerLink_3 = document.createElement("a");
		bannerLink_3.id = "bannerLink_3";
		bannerLink_3.className = "largerectangle";
		bannerLink_3.href = "https://kadampafestivals.org/spring?utm_source=nkt-ads&utm_medium=display&utm_content=largerectangle&utm_campaign=Spring2025";

		// Creation of bannerImage_3
		var bannerImage_3 = document.createElement("img");
		bannerImage_3.id = "bannerImage_3";
		bannerImage_3.alt = "Spring Festival 2025";
        var src = "";
        
        
        
        
            src = "https://api.kadampa.org/images/banners/festivals/Spring-2025-ENG-Pop-Under-2160x900.jpg"
        
        
        
        bannerImage_3.src = src;
		bannerImage_3.style.maxWidth = "100%";
		bannerImage_3.style.height = "auto";
		bannerImage_3.style.display = "block";

		// Associate bannerImage_3 to bannerLink_3
		bannerLink_3.appendChild(bannerImage_3); 
			
		// Add elements to the empty div 
        var parentDiv = document.getElementById("DivAds_3"); 
        var currentDiv = document.getElementById("KadampaFestivalsAds_3")
        parentDiv.insertBefore(bannerLink_3, currentDiv);
	}

	// Interval to change the ads every 12 seconds
	var renew = setInterval(function(){

		// If i is superior to the number of redirection links : set i to 0
		if (links.length == i) {i=0;}
		

		// Asynchronous script to check if the mouse is over or out of the ads
		setTimeout(() => {
		
			// Take bannerImage_3 element
			var bannerImage_3 = document.getElementById('bannerImage_3');
			
			// Use mouse event to know if the mouse is over
			bannerImage_3.onmouseover = function(e) {  
				mouseOntheAd = true;
			}
			
			// Use mouse event to know if the mouse is out
			bannerImage_3.onmouseout = function(e) { 
				mouseOntheAd = false;
			}

		}, 20);

		console.log(mouseOntheAd)
		
		// Only if the the mouse is over, change the ads
		if (mouseOntheAd === false) {
			// Change the bannerImage_3 with transition and bannerLink_3
			FadeInImage("bannerImage_3",images[i],"KadampaFestivalsAds_3")
			document.getElementById("bannerLink_3").href = links[i]; 
			i++;
		}       
	},12000);
}
	
popunderFunction();