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 + "',3000," + startMS + ",0,200)",10);
}


function wideskyscraperFunction() {
	var links = ["https://tharpa.com/uk/the-mirror-of-dharma.html?utm_source=tharpa-banners&utm_medium=display&utm_content=wideskyscraper&utm_campaign=mod-book-launch-2018-Q2","https://tharpa.com/uk/the-mirror-of-dharma.html?utm_source=tharpa-banners&utm_medium=display&utm_content=wideskyscraper&utm_campaign=mod-book-launch-2018-Q2"];

    
        var images = ["https://api.kadampa.org/images/banners/tharpas/Mirror-of-Dharma_web-banner_Wide-Sky-Scraper-160x600.jpg","https://api.kadampa.org/images/banners/tharpas/Mirror-of-Dharma_web-banner_Wide-Sky-Scraper-160x600_2.jpg"];
    
    
    
    
	 
    

	var i = 1;
	var mouseOntheAd = false;

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

		// Creation of bannerLink
		var bannerLink_0 = document.createElement("a");
		bannerLink_0.id = "bannerLink_0";
		bannerLink_0.className = "largerectangle";
		bannerLink_0.href = "https://tharpa.com/uk/the-mirror-of-dharma.html?utm_source=tharpa-banners&utm_medium=display&utm_content=wideskyscraper&utm_campaign=mod-book-launch-2018-Q2";

		// Creation of bannerImage_0
		var bannerImage_0 = document.createElement("img");
		bannerImage_0.id = "bannerImage_0";
		bannerImage_0.alt = "Promotion 1";
        var src = "";
        
            src = "https://api.kadampa.org/images/banners/tharpas/Mirror-of-Dharma_web-banner_Wide-Sky-Scraper-160x600.jpg"
        
        
        
        
        
        
        bannerImage_0.src = src;

		// Associate bannerImage_0 to bannerLink_0
		bannerLink_0.appendChild(bannerImage_0); 
			
		// Add elements to the empty div 
        var parentDiv = document.getElementById("TharpaAd_0"); 
        var currentDiv = document.getElementById("KadampaFestivalsAds_0")
        parentDiv.insertBefore(bannerLink_0, 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_0 element
			var bannerImage_0 = document.getElementById('bannerImage_0');
			
			// Use mouse event to know if the mouse is over
			bannerImage_0.onmouseover = function(e) {  
				mouseOntheAd = true;
			}
			
			// Use mouse event to know if the mouse is out
			bannerImage_0.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_0 with transition and bannerLink_0
			FadeInImage("bannerImage_0",images[i],"KadampaFestivalsAds_0")
			document.getElementById("bannerLink_0").href = links[i]; 
			i++;
		}       
	},12000);
}
	
wideskyscraperFunction();