//Creative template: Connatix PlaySpace - JS Tagless

/*================
 Move top slot on mWeb
==================*/
!function(){try{var e="a";Math.random()>0.5&&(e="b");var t,a,n=serverAdsOnPageLite.top.adUnitCode;if(-1==n.indexOf("mweb"))return;a=document.getElementById("top"),-1!=n.indexOf("local_home")&&0==document.getElementsByClassName("minutecast-banner").length&&((t=document.getElementsByClassName("weather-card")[0]).appendChild(a),a.style.padding="0 0 20px 0px"),-1!=n.indexOf("hourly-p")?(t=document.getElementsByClassName("hourly-wrapper")[0]).insertBefore(a,t.firstChild):-1!=n.indexOf("hourly")&&(t=document.getElementsByClassName("hourly-card-top")[0]).parentNode.insertBefore(a,t.nextSibling);var d=function(e,t){e.parentNode.insertBefore(t,e.nextSibling),t.style.background="#ffffff",t.style.margin="0",t.style.padding="0 0 12px 0"};if(-1!=n.indexOf("extended-p"))(t=document.getElementsByClassName("page-content")[0]).insertBefore(a,t.firstChild);else if(-1!=n.indexOf("extended")){d(document.getElementsByClassName("daily-forecast-card")[0],a);for(var l=document.getElementsByClassName("glacier-ad middle"),r=0;r<l.length;r++)d(l[r].previousElementSibling.getElementsByClassName("daily-forecast-card")[0],l[r])}if(-1!=n.indexOf("extended")){var s=document.head||document.getElementsByTagName("head")[0],i=document.createElement("style");s.appendChild(i),i.type="text/css",i.appendChild(document.createTextNode(".half-day-card-content{background: hsla(0,0%,100%,.55)} .daily-forecast-card {background: #ffffff}"))}}catch(f){}}();


 

//connatix on mobile
(function(){
	
	var au = '/6581/web/it/video_player/weather/month';
	if (au.indexOf('/mweb/us/video_player/weather/extended') == -1){
		return;
	}

try {
	if (document.getElementById("connatix")) {
   //skip 
	} else {
  	var ctxDiv = document.createElement('div');
		ctxDiv.id = 'connatix';
		document.body.appendChild(ctxDiv);
	}
} catch(e){}
	
	function insertAfter(referenceNode, newNode) {
		referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
	}
		try{
			let elem = document.getElementById("connatix");
			let div = document.getElementsByClassName("daily-wrapper")[9];
			insertAfter(div, elem);
		} catch (e){}
	
})();



/*================
CONTENT Key-VALUES
=================*/
(function(){	
	if (!window.adInfo){
		return;
	}
	var au = '/6581/web/it/video_player/weather/month';
	
	window.adInfo.content = window.adInfo.content || '';
	var addtokv = function(prop){
		if (window.adInfo.content != ''){
			window.adInfo.content += ",";
		}
		window.adInfo.content += prop;
	};
	
	try{
		
		const urlParams = new URLSearchParams(window.location.search);
	
		//"name" from the allergies pages
		if (au.indexOf('allergies') != -1){
			if (urlParams.get('name')){ 
				addtokv(urlParams.get('name')); 
			}
		}
	} catch (e){}
	
	
	//full url for content-landing
	try{
		const url = window.location.href;
		if (au.indexOf('content_landing') != -1){
			if (url.indexOf('/business') != -1) {addtokv("business");}
			if (url.indexOf('/climate') != -1) {addtokv("climate");}
			if (url.indexOf('/health-wellness') != -1) {addtokv("health-wellness");}
			if (url.indexOf('/space-news') != -1) {addtokv("space-news");}
			if (url.indexOf('/leisure-recreation') != -1) {addtokv("leisure-recreation");}
			if (url.indexOf('/sports') != -1) {addtokv("sports");}
			if (url.indexOf('/travel') != -1) {addtokv("travel");}
		}
	} catch (e){}
	
})();

 



/*================
Connatix
=================*/


(function(){
	
	!function(n){if(!window.cnxps){window.cnxps={},window.cnxps.cmd=[];var t=n.createElement('iframe');t.display='none',t.onload=function(){var n=t.contentWindow.document,c=n.createElement('script');c.src='https://cd.connatix.com/connatix.playspace.js',c.setAttribute('async','1'),c.setAttribute('type','text/javascript'),n.body.appendChild(c)},n.head.appendChild(t)}}(document);
	
	var au = '/6581/web/it/video_player/weather/month';
	try{
		if (window.serverAdsOnPage){
			for (var i=0;i<window.serverAdsOnPage.length;i++){
        if (window.serverAdsOnPage[i].adType == 'top'){
					au = window.serverAdsOnPage[i].adUnitCode;
          break;
				}                                               
      }
		}
	} catch (e){}
	
	
	try{
		var page = au.split('/')[2] + '-' + au.split('/')[6];
		au = au.replace('/6581/','');
		au = au.replace('/video_player/','/top/');
    	au = au.replace('/top/','/-/');
	} catch (e){}
	
	var s = document.createElement("script");
	s.id = '347d413251b04ccb910a122d526d4aac';
	s.type = 'text/javascript';
	var code = 'cnxps.cmd.push(function(){cnxps({playerId:"d44a175d-2cf3-46ec-918a-d433ff43124b",storyId:"308dc347-057e-404f-ac98-721d39475fb6",customParam1:"'+au+'",customParam2:"",customParam3:"'+window.adInfo.userid3p+'",customParam4:"",customParam5:"'+window.adInfo.partner+'"}).render("347d413251b04ccb910a122d526d4aac");});';
	    try {
	      s.appendChild(document.createTextNode(code));
	      document.body.appendChild(s);
	    } catch (e) {
	      s.text = code;
	      document.body.appendChild(s);
	    }
	try {
	if ('No' == 'Yes'){
		var track = new Image();
		track.src = 'https://securepubads.g.doubleclick.net/pcs/view?xai=AKAOjsuG0qZmLVPoKl0D4s84NIUafTUXJ19g0GIqJeJ3TzzKWd8Da8n8jHaSWgNqAbzV3dPnpgoZJQkPTwcScJKBweDRp-Q8T5eH34riRAROcpQS6TGhVXfbDOUQIZLAlOiErURQ2-gjOK44_Z0hSSA5_yB8W_93oAEFyb_cynWs8KlRh3jWSFn8MaYhA6QAfpqYJveWfUmne75Xmmfi1dMgZCdCnw6vv6IBJuONVj2BdSwf-aDhF3M1hdR3WNZvL19FguBpB5GtSTUtnfxCv6oCqp3bJTd8UvbsOb0gSjsh9li3wFp8bWIEKA4kDvBkQmJKdeQKSaxk_Y9-PwNFmM24DrIQK_do-mNx5ABMyHeKjP8oQuaOvhFv5Wdkag&sai=AMfl-YRwRg5Br2UFiGw-5dmHqZMeNlg6M_CaRWKwLd24RjgEF1r0k4x5q5UlQMhwrsknaVIPZUa6yNZnOy-xekdrqdsaUm6i6VvX1o_mYypxrwbLtlgdhX-60UCgJLwatQ&sig=Cg0ArKJSzK6BcA8RtP8SEAE&uach_m=%5BUACH%5D&urlfix=1&adurl=';
	}
	} catch (e){}	
})();

/*=========
REVCONTENT
=========*/
                                                         
(function(){
	const tier2pages = ['radar','search','country_home','admin_home','world_home','map','air_quality','snow','satellite','weather/lightning','wind'];
	const tier1widget = {elementId:'rc-widget-50cf01', widgetId:'276997', className:'revcontent content-module rc-3x2'};
	const tier2widget = {elementId:'rc-widget-50cf01', widgetId:'271054', className:'revcontent content-module rc-3x2'};
	const ctaLinks = document.getElementsByClassName("more-cta-links");
	if (ctaLinks.length < 2) {
		return;
	}
	
	const au = '/6581/web/it/video_player/weather/month';
	let rcWidgets = tier1widget;
	for (let i=0,l=tier2pages.length;i<l;i++){
		if (au.indexOf(tier2pages[i]) != -1){
			rcWidgets = tier2widget;
			break;
		}
	}
                                             
 //geo
	try{
		const geo = au.split('/')[3];
		const tier1geos = ['us', 'gb', 'au', 'ie', 'za', 'ca'];
	  if (rcWidgets.widgetId == '276997' && tier1geos.includes(geo) == false) {
	    rcWidgets.widgetId = '277516';
	  }
		if (geo == 'de'){
			return;		
		}
	} catch (e){}
	
	const rcDiv = document.createElement("div");
	rcDiv.setAttribute('class', rcWidgets.className);
	rcDiv.setAttribute('id', rcWidgets.elementId);
	rcDiv.setAttribute('data-widget-host', 'habitat');
	rcDiv.setAttribute('data-disable-demand', 'true');
	rcDiv.setAttribute('data-endpoint', '//trends.revcontent.com');
	rcDiv.setAttribute('data-widget-id', rcWidgets.widgetId);
	rcDiv.setAttribute('data-rc-widget', '');	
    rcDiv.setAttribute('data-image-quality', '85');	
                                     
	
	const ctaLink = ctaLinks[ctaLinks.length - 1];
	const parentDiv = ctaLink.parentNode;
	try{
		parentDiv.insertBefore(rcDiv, ctaLink);
	} catch (e){
		return;
	}
	
	
	function loadRevcontentScript() {
		const scriptEl = document.querySelector('script#revcontent-delivery');
		if (!scriptEl) {
			const s = document.createElement("script");
			const el = document.getElementsByTagName("script")[0];
			s.setAttribute('id', 'revcontent-delivery');
			s.src = "https://assets.revcontent.com/master/delivery.js";
			el.parentNode.insertBefore(s, el);
		}
	}
	
	if (!('IntersectionObserver' in window) ||
	    !('IntersectionObserverEntry' in window) ||
	    !('intersectionRatio' in window.IntersectionObserverEntry.prototype)) {
	loadRevcontentScript();
	} else {
		const rc_ll_config = {rootMargin: "0px 0px 1800px 0px"};
		try{
			const rc_container = document.getElementById(rcWidgets.elementId);
			let rc_observer = new IntersectionObserver(
				(entries, observer) => { 
					entries.forEach(entry => {
						if (entry.isIntersecting) {
							observer.unobserve(entry.target);
							loadRevcontentScript();
						}
					});
				}, rc_ll_config);
				rc_observer.observe(rc_container);
		} catch (e) {}
	}
})();


    
/*
	    Performance Testing
*/    
	(function(){
	
		if (!window.performance || !window.adInfo){
			return;
		}
		
		var au = '/6581/web/it/video_player/weather/month';
		if (au.indexOf('mweb/us/video_player/weather/extended') == -1){
			return;
		}
	
		window.adInfo.perf = '';
		var hasRequestBids = false;
		var hasAuctionInit = false;
		var hasSetTargeting = false;
		var firedTagless = false;
	
	
		var roundNearQtr = function(number) {
		  return (Math.round(number/1000 * 4) / 4).toFixed(2);
		};
	
	
		//GPT1: First Slot Requested
		var gptSlotRequestedListener = function(event) {
			if (event.slot.getSlotElementId() == googletag.pubads().getSlots()[0].getSlotElementId()){
				googletag.pubads().removeEventListener('slotRequested',gptSlotRequestedListener);
		        window.adInfo.perf += "GPT1:" + roundNearQtr((window.performance.now())) + ',';
			}
		};
		googletag.cmd.push(function() {
			googletag.pubads().addEventListener('slotRequested',gptSlotRequestedListener);
		});
	
		//GPT2: First Slot Render Ended
		var gptSlotRenderEndedListener = function(event) {
			if (event.slot.getSlotElementId() == googletag.pubads().getSlots()[0].getSlotElementId()){
				googletag.pubads().removeEventListener('slotRenderEnded',gptSlotRenderEndedListener);
		        window.adInfo.perf += "GPT2:" + roundNearQtr((window.performance.now())) + ',';
			}
		};
		googletag.cmd.push(function() {
			googletag.pubads().addEventListener('slotRenderEnded',gptSlotRenderEndedListener);
		});
	
	
		//GPT3: First Slot Load
		var gptSlotOnLoadListener = function(event) {
			if (event.slot.getSlotElementId() == googletag.pubads().getSlots()[0].getSlotElementId()){
				googletag.pubads().removeEventListener('slotOnload',gptSlotOnLoadListener);
		        window.adInfo.perf += "GPT3:" + roundNearQtr((window.performance.now())) + ',';
			}
		};
		googletag.cmd.push(function() {
			googletag.pubads().addEventListener('slotOnload',gptSlotOnLoadListener);
		});
	
	
		try{
			pbjs.getEvents().forEach(event => {
				//PB1: Prebid Request Bids
				if (event.eventType == 'requestBids') {
					if (event.elapsedTime && event.elapsedTime > 0){
						window.adInfo.perf += "PB1:" + roundNearQtr((event.elapsedTime)) + ',';
						hasRequestBids = true;
					}
				}
		
				//PB2: Prebid AuctinoInit
				if (event.eventType == 'auctionInit') {
					if (event.elapsedTime && event.elapsedTime > 0){
						window.adInfo.perf += "PB2:" + roundNearQtr((event.elapsedTime)) + ',';
						hasAuctionInit = true;
					}
				}
		
				//PB3: Prebid Set Targeting
				if (event.eventType == 'setTargeting') {
					if (event.elapsedTime && event.elapsedTime > 0){
						window.adInfo.perf += "PB3:" + roundNearQtr((event.elapsedTime)) + ',';
						hasSetTargeting = true;
					}
				}
			});		
		} catch (e){}
	
	
		try{
			//PB1: Prebid Request Bids
			if (!hasRequestBids){
				pbjs.onEvent('requestBids', function() {
					window.adInfo.perf += "PB1:" + roundNearQtr((window.performance.now())) + ',';
				});
			}
	
			//PB2: Prebid AuctinoInit
			if (!hasAuctionInit){
				pbjs.onEvent('auctionInit', function() {
					window.adInfo.perf += "PB2:" + roundNearQtr((window.performance.now())) + ",";
				});
			}
	
			//PB3: Prebid Set Targeting
			if (!hasSetTargeting){
				pbjs.onEvent('setTargeting', function() {
					window.adInfo.perf += "PB3:" + roundNearQtr((window.performance.now())) + ',';
				});
			}
		} catch (e){}
		
		
		try{
			if (!('IntersectionObserver' in window) ||
				    !('IntersectionObserverEntry' in window) ||
				    !('intersectionRatio' in window.IntersectionObserverEntry.prototype)) {
				//skip
				} else {
					var CurrentDayNum = -1;
					var ScrollToDayNum = 0;
					let observerOptions = {
					    rootMargin: '0px',
					    threshold: 0.25
					}

					var observer = new IntersectionObserver(observerCallback, observerOptions);

					function observerCallback(entries, observer) {
					    entries.forEach(entry => {
					        if(entry.isIntersecting) {
					          ScrollToDayNum = entry.target.getAttribute("data-qa").replace('dailyCard','');
							  ScrollToDayNum = parseInt(ScrollToDayNum);
							  if (ScrollToDayNum > CurrentDayNum){
								  CurrentDayNum = ScrollToDayNum;
								  window.adInfo.content = "DailyScroll" + CurrentDayNum;
							  }
					        }
					    });
					};

					let target = '.daily-wrapper';
					document.querySelectorAll(target).forEach((i) => {
					    if (i) {
					        observer.observe(i);
					    }
					});
				}
		} catch (e){
			
		}
		

	
		//fire tagless request on exit or visibility change
		function adPerfGetHiddenProp(){
		    var prefixes = ['webkit','moz','ms','o'];
    
		    // test for native support
		    if ('hidden' in document) return 'hidden';
    
		    //find prefixes
		    for (var i = 0; i < prefixes.length; i++){
		        if ((prefixes[i] + 'Hidden') in document) 
		            return prefixes[i] + 'Hidden';
		    }

		    // otherwise it's not supported
		    return null;
		} 
	
		function adPerfChangeEventListener(){
			if (!document[visProp]){
				//page is visible
			} else {
				adPerfFireTaglessAdRequest();
			}
		}
		var visProp = adPerfGetHiddenProp();
		if (visProp) {
			var evtname = visProp.replace(/[H|h]idden/,'') + 'visibilitychange';
			document.addEventListener(evtname, adPerfChangeEventListener , false);
		}
		document.addEventListener("unload", adPerfFireTaglessAdRequest);
	
	
		function adPerfFireTaglessAdRequest(){
			if (firedTagless){
				return;
			}
			document.removeEventListener("unload", adPerfFireTaglessAdRequest);
		
			var au = '/6581/web/it/video_player/weather/month';
		
			try{
				au = au.replace('/video_player/','/page/');
			} catch (e){}
		
			//create URL
			var url = ["https://securepubads.g.doubleclick.net/gampad/adx?"];
			url.push("iu="+au);
			url.push("&sz=1x1");
			try{
				url.push("&c=" + googletag.pubads().getCorrelator());
			} catch (e) {
				url.push("&c=" + Math.floor(Math.random()*1000000));
			}
		
			var kvs = '';
			for (var x in adInfo){
				kvs += x+"="+adInfo[x]+"&";
			} 
			url.push("&t=" + encodeURIComponent(kvs));
			var req = new Image();
			req.src = url.join('');
			firedTagless = true;
		}
	
	})();