
function isInViewport(element) {
    const rect = element.getBoundingClientRect();
	var inTopBottom =  rect.y < window.innerHeight && rect.bottom > 0;
	
	//ce code permet de v?rifier que l'?l?ment concern? est enti?rement affich? dans la page
	//pour les besoins de addserving et les problematiques de fluidification de l'interface, 
	//on veut charger la pub d?s qu'un pixel de l'emplacement est vu
	//rect.top >= 0 && rect.bottom <= (window.innerHeight || document.documentElement.clientHeight);
	
	//la v?rification leftright ne se pose pas de probleme ici,  donc on  le force toujours ? true
	var inLeftRight = true; rect.left >= 0 && rect.right <= (window.innerWidth || document.documentElement.clientWidth)
    return (
        inTopBottom && inLeftRight
    );
}

var MaxRetry = 0;
var MaxIdCursor = 0;
function Mash() {
	var n = 0xefc8249d;

	var mash = function (data) {
		data = data.toString();
		for (var i = 0; i < data.length; i++) {
			n += data.charCodeAt(i);
			var h = 0.02519603282416938 * n;
			n = h >>> 0;
			h -= n;
			h *= n;
			n = h >>> 0;
			h -= n;
			n += h * 0x100000000; // 2^32
		}
		return (n >>> 0) * 2.3283064365386963e-10; // 2^-32
	};

	mash.version = 'Mash 0.9';
	return mash;
}

// From http://baagoe.com/en/RandomMusings/javascript/
function Alea() {
	return (function (args) {
		// Johannes Baagøe <baagoe@baagoe.com>, 2010
		var s0 = 0;
		var s1 = 0;
		var s2 = 0;
		var c = 1;

		if (args.length === 0) {
			args = [+new Date];
		}
		var mash = Mash();
		s0 = mash(' ');
		s1 = mash(' ');
		s2 = mash(' ');

		for (var i = 0; i < args.length; i++) {
			s0 -= mash(args[i]);
			if (s0 < 0) {
				s0 += 1;
			}
			s1 -= mash(args[i]);
			if (s1 < 0) {
				s1 += 1;
			}
			s2 -= mash(args[i]);
			if (s2 < 0) {
				s2 += 1;
			}
		}
		mash = null;

		var random = function () {
			var t = 2091639 * s0 + c * 2.3283064365386963e-10; // 2^-32
			s0 = s1;
			s1 = s2;
			return s2 = t - (c = t | 0);
		};
		random.uint32 = function () {
			return random() * 0x100000000; // 2^32
		};
		random.fract53 = function () {
			return random() +
				(random() * 0x200000 | 0) * 1.1102230246251565e-16; // 2^-53
		};
		random.version = 'Alea 0.9';
		random.args = args;
		return random;

	}(Array.prototype.slice.call(arguments)));
};

var addMaxProg = function () {
	var myscript = document.createElement('script');
	myscript.setAttribute('src', '~/v140/' + (brdata.pubserial === undefined ? new Date().getTime() : brdata.pubserial) + '/responsive,js,maxprog.js');
	document.getElementsByTagName('head')[0].appendChild(myscript);
	tryServe();
};
var serve = function () {
	$(".maxget").each(function (index, el) {
		var html = '';
		var $el = $(el);
		var emplacement = $el.attr('emp');
		var page = $el.attr('page');
		var region = $el.attr('region');
		var keydiv = $el.attr('keyDiv');
		var MaxBanDone = [];
		var mobileinterstitiel = $el.attr('mobileinterstitiel') === '1';
		var ui = '';
		if (!keydiv) keydiv = 'maxdiv_' + emplacement + '-' + page + '-' + region + '_id' + (MaxIdCursor++);

		function getContenu(emp) {
			var isHabi = emp === 'habi_v2';
			var rnd = rndx();
			c = MaxElements.e[emp];
			if (!c)
				return;
			cib = [page + 'Reg' + region, 'Reg' + region, page + 'Reg', 'Reg'];
			for (i = 0; i < cib.length; i++) {
				x = c.d[cib[i]];
				if (x !== undefined) break;
			}
			if (MaxElements.e[emp])
				idemp = MaxElements.e[emp].eid;
			if (x) {
				ce = x[Math.floor(rndx() * (x.length))];
				var myce = ce;
				var camphasFlashFallback = (MaxElements.ce[ce] ? true : false);
				if (!camphasFlashFallback && flashok) {
					myce = ce + '-1';
					c = MaxElements.ce[myce];
					if (!c) c = MaxElements.ce[ce];
				}
				else {
					c = MaxElements.ce[ce];
					if (!c) {
						MaxRetry++;
						return getContenu(emp);
					}
				}
				b = c[Math.floor(rndx() * (c.length))];
				ban = MaxElements.b[b];
				ban.id = b;
				ban.ce = ce;
				if ($.inArray(ban.id, MaxBanDone) > -1 && MaxRetry < 4) {
					if (MaxRetry > 5) return;
					MaxRetry++;
					return getContenu(emp);
				}
				MaxBanDone.push(ban.id);
				if (ban.html) {
					if (isHabi) {
						var dataHabi = ban.html.split('|');
						habi = ban;
						var url = 'https://billetreduc.com/cgi/max.aspx?rnd=' + rnd + '&b=' + habi.id + '&ce=' + habi.ce;
						var $ctn = $('<div id="habillage_container">');
						var $e = $('<a>');
						$e.attr("href", url);
						$e.attr("id", "nhh");

						var $cssHabi = $('<link defer rel="stylesheet">');
						$cssHabi.attr('href', dataHabi[1].replace('.css', '_v2.css'));

						var $divImg = $('<div id="img_habillage">');
						$ctn.append($e);
						$ctn.append($cssHabi);
						$ctn.append($divImg);
						return $ctn;
					}
					else { html = ban.html; }

				}
				else if (ban.src) {
					if (ban.src.indexOf('.swf') > 0) {
						flaban = ban;
						html = "<div id='flash_" + ban.id + "'></div>";
					}
					else {

						html = '<a href="https://billetreduc.com/cgi/max.aspx?rnd=' + rnd + '&b=' + ban.id + '&ce=' + ban.ce + '" '
							+ 'gtm-pub-tracker="' + (ban.idevent ? 'pub' : 'push') + '"'
							+ 'gtm-cat="' + emp + '" gtm-action="click"'
							+ (ban.idevent ? 'spectid="' + ban.idevent + '"' : '')
							+ ' >' +
							'<img src="https://www.billetreduc.com' + ban.src + '"/>'
							+ '</a>';

					}
				}
				if (ban.ui) {
					ui = '<img height="0" width="0" style="width: 0px; height: 0px;" alt="" src="' + ban.ui + '"/>';
				}
				urllog = '&ce=' + ban.ce
					+ '&b=' + ban.id;
			}
			html += '<div id="maxbea_' + emp + '-' + page + '-' + region + "_" + keydiv
				+ '" style="position: absolute; left: 0px; top: 0px; visibility: hidden;">'
				+ '<img height="0" width="0" style="width: 0px; height: 0px;" alt="" src="https://billetreduc.com/cgi/max.aspx?rnd=' + rnd
				+ '&p=' + page
				+ '&r=' + region
				+ '&e=' + idemp
				+ urllog
				+ '"/>' + ui + '</div>';
			return html;
		}

		var mobileAdded = false;
		var desktopAdded = false;
		var oldEmp = $el.attr('old-emp');
		var addPub = function (screenSize) {
			if(isInViewport($el[0]) || IsResponsivePage){
				var isHab = emplacement.indexOf('habi') >= 0;
				if (!desktopAdded && (screenSize > 650 || !IsResponsivePage)) {
					var contenuDesktop = null;
					if (oldEmp)
						contenuDesktop = getContenu(oldEmp);
					if (contenuDesktop === null) {
						contenuDesktop = getContenu(emplacement);
					}
					if (contenuDesktop === undefined)
						return;
					$divDesk = $('<div></div>');
					$divDesk.attr('class', 'desktop');
					$divDesk.html(contenuDesktop);
					if (!isHab)
						$divDesk.append('<span class="pub__notifier" role="presentation">Publicit&eacute;</span>');
					$el.append($divDesk);
					desktopAdded = true;
				}
				else if (screenSize <= 650 && IsResponsivePage) {
					var contenuMobile = getContenu(emplacement + 'm');
					if (contenuMobile === undefined)
						return;
					$divMobile = $('<div></div>');
					$divMobile.attr('class', 'mobile');
					$divMobile.html(contenuMobile);
					$divMobile.append('<span class="pub__notifier" role="presentation">Publicit&eacute;</span>');
					if (mobileinterstitiel) {
						var lastDisplay = Cookies.get('interstitiel');
						if (lastDisplay === undefined || new Date((new Date() - new Date(lastDisplay))).getMinutes() > 3) {
							Cookies.set('interstitiel', new Date());
							$divMobile.attr('class', 'mobile interstitiel');
							$divMobile.appendTo('body').modal();
							Cookies.set('interstitiel', new Date());
						}
					}
					else if (!mobileAdded) {
						$el.append($divMobile);
						mobileAdded = true;
					}
				}
			}
		};
		var screenSize = $(window).width();
		addPub(screenSize);
		$(window).resize(function () {
			screenSize = $(window).width();
			addPub(screenSize);
		});
		$(window).scroll(function () {
			addPub(screenSize);
		});
	});
};

var rndx = new Alea();
var tryServe = function () {
	if (typeof MaxElements !== 'undefined') {
		serve();
	}
	else
		setTimeout(
			function () {
				tryServe();
			}, 50);
};
addMaxProg();

