if (jQuery) {
	jQuery(document).ready(function() {
		if (bpinfo.Passport == null) return;
		//popup the loading screen.
		jQuery('body').append('<div id="loadingScreenPaspoort"><p id="loadText">Er wordt gecontroleerd op een sessie bij BRIS paspoort.</p><img id="loadImg" src="/typo3conf/ext/dre_brispaspoort/res/ajax-loader.gif" alt="" title="" height="54" width="55" /></div>');
		
		jQuery('body').css('overflow', 'hidden');
		jQuery('#loadingScreenPaspoort')
			.width(jQuery(window).width())
			.height(jQuery(window).height())
			.css('position', 'absolute').css('left', 0).css('top', 0)
			.css('background', 'white');
		jQuery('#loadText')
			.css('text-align', 'center')
			.css('margin-top', '10em')
			.css('font-weight', 'bold').css('font-size', '16px');
		jQuery('#loadImg')
			.css('display', 'block').css('margin-left', 'auto').css('margin-right', 'auto');


		// call the TYPO3 eID func and put the request information of bris passport to TYPO3.
		jQuery.ajax({
			type: 'POST',
			url: '/?eID=dre_brispaspoort',
			data: {
				token: (bpinfo.Passport != null ? bpinfo.Request.Token : ''),
				referrer: String(window.location)
			},
			dataType: 'json',
			success: function(data) {
				var newLocation = false;
				if (data.login && data.login == true) {
					if (bpinfo.Passport != null) {

						var currLoc = String(window.location);

						// See if we found ?token
						if(currLoc.indexOf('&token')) {
							currLoc = currLoc.split('&token');

                        	newLocation = currLoc[0] + "?a" + new Date().getTime();
						} else if(currLoc.indexOf('?token')) {
							currLoc = currLoc.split('?token');

    	    	            if (bpinfo.Passport != null) {
        	    	            newLocation = currLoc[0] + "?a" + new Date().getTime();
		        	        }
						}
					}
				} else if (data.redirect && data.redirect.indexOf('http') > -1) {
					newLocation = data.redirect;
				}

				if (newLocation) {
					setTimeout('window.location = "'+newLocation+'"', 500);
					return;
				}
				jQuery('body').css('overflow', 'auto');
				jQuery('#loadingScreenPaspoort').hide();
			}

		});
	});
}

