function js(){
	$(document).on("AgGeometryChanged", function() {

		
		var getLink = function(URL, target){	
			$.ajax({ 
				url: URL,
				type:'GET',
				dataType: 'text',
				crossDomain:true,
				success: function (data) { 
						target.attr('href', data);
						target.attr('target','_blank');
						
				}
		    });
		}
		//getLink('https://subscriptions.guardian.co.tt/issue/link.php', $('.todays-guardian a'));
		
		//Fixed nav
		$('.scroll-nav').hide();
		$(window).scroll(function () {
			var newScrollTop = $(window).scrollTop();
			
			if (newScrollTop <= 160) {
					$('.scroll-nav').hide();
			} else {
				$('.scroll-nav').show();
			}
		});
		
		//Removing lead from department names
		$(".department-name").each(function() {
		    var text = $(this).text();
		    text = text.replace("lead", "");
			text = text.replace("on sunday", "");	
		    $(this).text(text);
		});


		//add numbers to list for trending
		$(".trending-list a").each(function() {
			$(this).find("span").prepend("<b>"+($(this).index()+1)+".</b> ");
		});
		
		//Mobile search
		$('.mobile-search').click(function(){
			$('.guardianSearch-mobile').toggleClass('active')
		})
		
		//search form
		$('.search-trigger').click(function(){
			$('.search-form').toggleClass('active')
		})
		
		//Scroll to top
		var anchor = $('.anchor-up');

		//blockmodal
		$('.ag-login-trigger').click(function(){
			var modal = document.querySelector('.ag-modal.ag-login')
			modal.classList.add('active');
		})
		
		//mobile login
		$('.mobile-login-trigger').click(function(){
			var modal = document.querySelector('.ag-modal.ag-login')
			modal.classList.add('active');
		})

		$(window).scroll(function () {
			if ($(this).scrollTop() > 500) {
				anchor.css('bottom', '10px');
			} else {
				anchor.css('bottom', '-80px');
			}
		});

		anchor.click(function () {
			$('html, body').animate({
				scrollTop: $('.page').offset().top
			}, 500, "swing");
			return false;
		});

		//close modal
		$('.ag-modal-footer button[name="cancel"]').click(function(){
			$(this).parent().parent().parent().removeClass('active')
		})
		

		//Current Date to top nav
		var date = new Date();
		var days = ['Sunday','Monday','Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
		var months = ['January','February', 'March', 'April','May','June','July','August','September','October','November','December'];
		
		var html = days[date.getDay()]+', '+months[date.getMonth()]+'  '+date.getDate()+',  '+date.getFullYear();
		
		if($('#toDay').length > 0){
			document.getElementById('toDay').innerHTML = html;
		}
		
		//set active to nav
		var docURL = document.URL
		var docCut = docURL.indexOf('NeoProxy.dll') + 12;
		var cutURL = docURL.slice(docCut);

		$('.ag-nav-links').children('a').each(function(){
			if ($(this).attr('href') == cutURL){
				$(this).find('p').addClass('active');
			}
			else{
				$(this).find('p').removeClass('active')
			}
		});
		
		//Calculate how old post is and set view
		$('.datejs').each(function(){
			var publishDate = $(this).text();
			var pubYear = publishDate.slice(0,4);
			var pubMonth = publishDate.slice(4,6);
			var pubDate = publishDate.slice(6,8);
			var pubHours = publishDate.slice(8,10);
			var pubMins = publishDate.slice(10,12);
			var pubSecs = publishDate.slice(12,14);

			var pubDate = new Date(pubYear+'/'+pubMonth+'/'+pubDate+' '+pubHours+':'+pubMins+':'+pubSecs);
			var diff = Math.abs(new Date() - pubDate);

			var dSecs = (diff / 1000).toFixed(0);
			var dMins = Math.floor(dSecs / 60);
			var dHours = Math.floor(dMins / 60);

			
			if (dHours < 24){
				//Hours ago
				if(dHours == 1)
					$(this).text(dHours+' hour ago');
						else if(dHours < 1)
							$(this).text('less than hour ago')
						else
							$(this).text(dHours+' hours ago');
			} else if(dHours >= 24 && dHours <= 48){
				//Yesterday
				$(this).text('Yesterday');
			}else {
				var dDays = Math.floor(dHours/24);
				//Days ago
				$(this).text(dDays+' days ago');
			}
			
		})
		
		$('.share-launch').each(function(){
			$(this).click(function(event){
				event.preventDefault();
				event.stopPropagation();
			})
		});

if($('.ag-carousel-content').length > 0){
$('.ag-carousel-content').each(function(index){

	$(this).css({
		"transition":"transform 0.4s ease "+index+"s"
	})
	
	var x = this;
	
	var interval = setInterval(function(){
						$(x).toggleClass('animated')
					},8000, x);
	
	$(this).hover(function(){
		
		clearInterval(interval);
		
	}, function(){
		
		interval = setInterval(function(){
						$(x).toggleClass('animated')
					},8000,x);
		
	})
})


}


		
		//AG tabs function
		if($('.ag-tab-nav>span').length > 0){
		    $('.ag-tab-nav>span').each(function(){
			$(this).click(function(){
			    var tabval = $(this).attr('nav-val');
			    var path = $(this).parent('.ag-tab-nav').siblings('.ag-tab-content').children('.ag-tab-content-section').children('span[section-val='+tabval+']');
			    var route = $(this).parent('.ag-tab-nav').siblings('.ag-tab-content').children('.ag-tab-content-section');
			    
			    $(this).parent().find('span').removeClass('active');
			    $(this).addClass('active');

			    $(route).removeClass('active');
			    $(path).parent().addClass('active');	
			})
		    })
		}
		//AG accordion
		$('.ag-accordion-frame').each(function(){
			$(this).find('.ag-accordion-content').not(':first').hide();
		})
		$('.ag-accordion-tab').click(function(){
			if($(this).siblings('.ag-accordion-content').is(':visible')){
				$(this).closest('.ag-accordion-frame').find('.ag-accordion-content').hide(400);
			}else{
				$(this).closest('.ag-accordion-frame').find('.ag-accordion-content').hide(400);
				$(this).siblings('.ag-accordion-content').show(400);
			}
		})
		//Box article if no IMG
		if($('.ag-overlay-article').length > 0){
			$('.ag-overlay-article').each(function(){
				if($(this).find('.crop_container').length < 1){
					$(this).addClass('ag-no-img');
				}
			})
		}
		
		//Share widget hover
		$('.share-frame').hover(function(){
			$(this).parent().children('.hoverhide').css('opacity','0');
		},function(){
			$(this).parent().children('.hoverhide').css('opacity','1');
		})
		//MENU TOGGLE
		$('.menu-toggle').click(function(){
			$(this).toggleClass('active');
		$('.mobile-menu').toggleClass('active');
		})

		//LOGIN 
		$('.login-trigger').click(function(){
			if($('.register-form').hasClass('active')){
				$('.register-form').removeClass('active');
			}
			$('.login-form').toggleClass('active');
		})
		//Register
		$('.register-trigger').click(function(){
			if($('.login-form').hasClass('active')){
				$('.login-form').removeClass('active');
			}
			$('.register-form').toggleClass('active');
		})

		//Lables link accordingly
		var host = encodeURIComponent(window.location.host);
		$('span.department-name').each(function(){
			switch(this.innerHTML){
				//news departments
				case'news':
					this.parentNode.innerHTML = '<a href="http://'+host+'/section-6.1.0d4abcd258">'+this.outerHTML+'</a>'
				break;
				case'news lead':
					this.parentNode.innerHTML = '<a href="http://'+host+'/section-6.1.0d4abcd258">'+this.outerHTML+'</a>'
				break;
				//sports departments
				case 'sports':
					this.parentNode.innerHTML = '<a href="http://'+host+'/section-6.5.69d73a4bfe">'+this.outerHTML+'</a>'
				break;
				case 'sports lead':
					this.parentNode.innerHTML = '<a href="http://'+host+'/section-6.5.69d73a4bfe">'+this.outerHTML+'</a>'
				break;
				//life departments
				case 'life':
					this.parentNode.innerHTML = '<a href="http://'+host+'/section-6.4.7cb5eb93ff">'+this.outerHTML+'</a>'
				break;
				case 'life lead':
					this.parentNode.innerHTML = '<a href="http://'+host+'/section-6.4.7cb5eb93ff">'+this.outerHTML+'</a>'
				break;
				case 'talk of trinidad':
					this.parentNode.innerHTML = '<a href="http://'+host+'/section-6.4.7cb5eb93ff">'+this.outerHTML+'</a>'
				break;
				case 'features':
					this.parentNode.innerHTML = '<a href="http://'+host+'/section-6.4.7cb5eb93ff">'+this.outerHTML+'</a>'
				break;
				case 'features lead':
					this.parentNode.innerHTML = '<a href="http://'+host+'/section-6.4.7cb5eb93ff">'+this.outerHTML+'</a>'
				break;
				//opinions departments
				case 'opinion':
					this.parentNode.innerHTML = '<a href="http://'+host+'/section-6.6.7924c8ab58">'+this.outerHTML+'</a>'
				break;
				case 'opinion lead':
					this.parentNode.innerHTML = '<a href="http://'+host+'/section-6.6.7924c8ab58">'+this.outerHTML+'</a>'
				break;
				case 'columnist':
					this.parentNode.innerHTML = '<a href="http://'+host+'/section-6.6.7924c8ab58">'+this.outerHTML+'</a>'
				break;
				case 'letter':
					this.parentNode.innerHTML = '<a href="http://'+host+'/section-6.6.7924c8ab58">'+this.outerHTML+'</a>'
				break;
				case 'editorial':
					this.parentNode.innerHTML = '<a href="http://'+host+'/section-6.6.7924c8ab58">'+this.outerHTML+'</a>'
				break;
				case 'editorial lead':
					this.parentNode.innerHTML = '<a href="http://'+host+'/section-6.6.7924c8ab58">'+this.outerHTML+'</a>'
				break;
				case 'letters on sunday':
					this.parentNode.innerHTML = '<a href="http://'+host+'/section-6.6.7924c8ab58">'+this.outerHTML+'</a>'
				break;
				case 'commentary':
					this.parentNode.innerHTML = '<a href="http://'+host+'/section-6.6.7924c8ab58">'+this.outerHTML+'</a>'
				break;
				//deaths departments
				case 'deaths & memorials':
					this.parentNode.innerHTML = '<a href="http://'+host+'/section-6.27.54666ec801">'+this.outerHTML+'</a>'
				break;
				//business departments
				case 'business':
					this.parentNode.innerHTML = '<a href="http://'+host+'/section-6.3.c1ecc2a125">'+this.outerHTML+'</a>'
				break;
				case 'business lead':
					this.parentNode.innerHTML = '<a href="http://'+host+'/section-6.3.c1ecc2a125">'+this.outerHTML+'</a>'
				break;
				case 'after hours':
					this.parentNode.innerHTML = '<a href="http://'+host+'/section-6.3.c1ecc2a125">'+this.outerHTML+'</a>'
				break;
				case 'biz tips':
					this.parentNode.innerHTML = '<a href="http://'+host+'/section-6.3.c1ecc2a125">'+this.outerHTML+'</a>'
				break;
				case 'country profile':
					this.parentNode.innerHTML = '<a href="http://'+host+'/section-6.3.c1ecc2a125">'+this.outerHTML+'</a>'
				break;
				case 'cover story':
					this.parentNode.innerHTML = '<a href="http://'+host+'/section-6.3.c1ecc2a125">'+this.outerHTML+'</a>'
				break;
				case 'energy':
					this.parentNode.innerHTML = '<a href="http://'+host+'/section-6.3.c1ecc2a125">'+this.outerHTML+'</a>'
				break;
				case 'entrepernomicss':
					this.parentNode.innerHTML = '<a href="http://'+host+'/section-6.3.c1ecc2a125">'+this.outerHTML+'</a>'
				break;
				case 'excutive briefing':
					this.parentNode.innerHTML = '<a href="http://'+host+'/section-6.3.c1ecc2a125">'+this.outerHTML+'</a>'
				break;
				case 'feeback':
					this.parentNode.innerHTML = '<a href="http://'+host+'/section-6.3.c1ecc2a125">'+this.outerHTML+'</a>'
				break;
				case 'finance':
					this.parentNode.innerHTML = '<a href="http://'+host+'/section-6.3.c1ecc2a125">'+this.outerHTML+'</a>'
				break;
				case 'financial road map':
					this.parentNode.innerHTML = '<a href="http://'+host+'/section-6.3.c1ecc2a125">'+this.outerHTML+'</a>'
				break;
				case 'international':
					this.parentNode.innerHTML = '<a href="http://'+host+'/section-6.3.c1ecc2a125">'+this.outerHTML+'</a>'
				break;
				case 'mutual fund':
					this.parentNode.innerHTML = '<a href="http://'+host+'/section-6.3.c1ecc2a125">'+this.outerHTML+'</a>'
				break;
				case 'regional':
					this.parentNode.innerHTML = '<a href="http://'+host+'/section-6.3.c1ecc2a125">'+this.outerHTML+'</a>'
				break;
				case 'verbatim':
					this.parentNode.innerHTML = '<a href="http://'+host+'/section-6.3.c1ecc2a125">'+this.outerHTML+'</a>'
				break;
				case 'view':
					this.parentNode.innerHTML = '<a href="http://'+host+'/section-6.3.c1ecc2a125">'+this.outerHTML+'</a>'
				break;
				case 'around the world':
					this.parentNode.innerHTML = '<a href="http://'+host+'/section-6.3.c1ecc2a125">'+this.outerHTML+'</a>'
				break;
				case 'business in focus':
					this.parentNode.innerHTML = '<a href="http://'+host+'/section-6.3.c1ecc2a125">'+this.outerHTML+'</a>'
				break;
				case 'excutive focus':
					this.parentNode.innerHTML = '<a href="http://'+host+'/section-6.3.c1ecc2a125">'+this.outerHTML+'</a>'
				break;
				case 'from the archives':
					this.parentNode.innerHTML = '<a href="http://'+host+'/section-6.3.c1ecc2a125">'+this.outerHTML+'</a>'
				break;
				case 'how its made':
					this.parentNode.innerHTML = '<a href="http://'+host+'/section-6.3.c1ecc2a125">'+this.outerHTML+'</a>'
				break;
				case 'how to spend it':
					this.parentNode.innerHTML = '<a href="http://'+host+'/section-6.3.c1ecc2a125">'+this.outerHTML+'</a>'
				break;
				case 'in the seat':
					this.parentNode.innerHTML = '<a href="http://'+host+'/section-6.3.c1ecc2a125">'+this.outerHTML+'</a>'
				break;
				case 'personal finance':
					this.parentNode.innerHTML = '<a href="http://'+host+'/section-6.3.c1ecc2a125">'+this.outerHTML+'</a>'
				break;
				case 'sunday business explained':
					this.parentNode.innerHTML = '<a href="http://'+host+'/section-6.3.c1ecc2a125">'+this.outerHTML+'</a>'
				break;
				
				default:
				break;
			}
		})

		//ag tab box 
		var target = document.querySelectorAll('button[ag-tab-role="next"]');

		for(i=0; i<target.length; i++){
			
			target[i].addEventListener('click', function(){
				
				var act = this.getAttribute('curr-val');
				var max = this.getAttribute('max-val');	
				var math = Math.floor(Number(act)+1);
				
				
				try{
					if(act < max){
						this.parentNode.querySelector('span[nav-val="'+math+'"]').click();
						this.setAttribute('curr-val',math)
					}else{
						throw 'No content';
					}
				}catch(err){
					console.log(err)
				}
				
				
			})
			
		}

		$('span[nav-val]').click(function(){

            var next = $(this).siblings('button[ag-tab-role]');
			
            next.attr('curr-val',$(this).attr('nav-val'));
            if( next.attr('curr-val') == next.attr('max-val'))
            next.hide();
                    else
                        next.show();
			
		})


		/*if($('.spot-im-replies-count').length >= 1){
			$('.spot-im-replies-count').each(function(){
				var URL = $(this).closest('a').attr('href');
				var ex = 'NeoDirect&com=6/4/';
				var cutIndex = URL.indexOf(ex) + ex.length;
				var remIndex = URL.length - Math.floor(Number(cutIndex) + 6);
				
				$(this).attr('data-post-id', URL.slice(cutIndex, -remIndex))
				
			})
		}*/

		if($('.ag-box-tabs').length > 0){
	
    $('.ag-box-tabs .ag-tab-content-section').each(function(){
        
		var content = this.querySelectorAll('div')
		var test = $(this).children('[section-val]').attr('section-val');

		if(content.length < 1){
		    $(this).parent().siblings('.ag-tab-box').children('[nav-val='+test+']').remove();
		    $(this).closest('.ag-box-tabs').children('.ag-tab-box').children('span[nav-val='+test+']').remove();
		}
		
		    })
		 $('.ag-tab-box').each(function(){
			var content = $(this).children('span');
			
			if(content.length < 2){
			    $(this).remove();
			}
			
		    })  
	}
/*
	if(!document.cookie.includes('amemberPaper')){
    
	    var modal = document.querySelector('.ag-modal.ag-epaper')

	    document.querySelector('.todays-guardian').addEventListener('click', function(event){
		event.preventDefault();
		modal.classList.add('active');
	    })
	    
	}
*/

	$('.ag-modal-bg').each(function(){
		$(this).click(function(){
			$(this).parent().removeClass('active')
		})
	})

	//SPOT IM REPLIES COUNT
	$('.spot-im-title').each(function(){
		var title = $(this).find('.spot-im-title-react');
		var count = $(this).find('.spot-im-replies-count').text();
		if(parseInt(count) == 1)
			title.text('Response');
	})

	

});
}

function setBlurBg(){

var images = document.querySelectorAll('.ag-overlay-sizer img');

for (var i = 0; i<images.length; i++){ var image  = images[i];

	image.addEventListener('load', handleEvent, false)
	
}

function handleEvent(){
	console.log('setting blur background...');
	var imageURL = this.getAttribute('src');
	var bg = this.parentNode.parentNode.parentNode.querySelector('.image-fade');
	
	bg.style.cssText = "background-image:url("+imageURL+")";
	
	this.removeEventListener('load',handleEvent);
	
}
}

document.addEventListener('DOMContentLoaded', function(){
	console.log('DOM loaded');
	setBlurBg();
})// JavaScript Document