// corelio advertising by adhese.com
var adheseLang = "fr";
var adheseSite = "dso";
var adheseCookie;
var adheseProfile;
var adheseRand;
var adheseIframe = false;
var adhesePreviewMode = false;
var adheseDemoMode = false;
var adhesePreviewCreativeId = [];
var adhesePreviewTemplateId = [];
var adhesePreviewSlotId = [];
var adhesePageview = 1;
var adheseLocation = "";

function checkForPreview() {
	//check for preview parameter
	if (location.search.indexOf("adhesePreview")!=-1) {
		var p = location.search.substring(1).split("&");
		for (var x=0; x<p.length; x++) {
			if (p[x].split("=")[0]=="adhesePreviewCreativeId") {
				adhesePreviewCreativeId.push(unescape(p[x].split("=")[1]));
				//if (adhesePreviewTemplateId) break;
			}
			if (p[x].split("=")[0]=="adhesePreviewSlotId") {
				adhesePreviewSlotId.push(p[x].split("=")[1]);
				//if (adhesePreviewTemplateId) break;
			}
			if (p[x].split("=")[0]=="adhesePreviewCreativeTemplate") {
				adhesePreviewTemplateId.push(p[x].split("=")[1]);
				//if (adhesePreviewTemplateId) break;
			}			
		}
		createAdheseCookie("adhese_preview",adhesePreviewCreativeId.join(',')+"&"+adhesePreviewTemplateId.join(',')+"&"+adhesePreviewSlotId.join(','),0);
		adhesePreviewMode = true;
		addAdheseLoadEvent(showPreviewSign);
	} else if (readAdheseCookie("adhese_preview")) {
		var v = readAdheseCookie("adhese_preview").split("&");
		adhesePreviewCreativeId = v[0].split(",");
		adhesePreviewTemplateId = v[1].split(",");
		adhesePreviewSlotId = v[2].split(","); 
		adhesePreviewMode = true;
		addAdheseLoadEvent(showPreviewSign);
	}
}

function checkForDemo() {
	//check for preview parameter
	if (location.search.indexOf("adheseDemo")!=-1) {
		createAdheseCookie("adhese_demo","y",0);
		adheseDemoMode = true;
	} else if (readAdheseCookie("adhese_demo")) {
		adheseDemoMode = true;
	}
}

function createAdheseCookie(name,value,days) {
	if (days && days > 0) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readAdheseCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseAdheseCookie(name) {
	createAdheseCookie(name,"",-1);
}

function showPreviewSign() {
	var p = document.createElement('DIV');
	var msg = "<div onclick='quitAdhesePreview(); return false;' style='font-family:Helvetica,Verdana; font-size:12px; text-align:center; background-color:#000000; color: #ffffff; position:fixed; top:0px; left:6px; padding:4px; border-style:dashed; border:2px; border-color:#000000;z-index:9999'>";
	msg += "<p><b>Disable<br>Adhese<br>preview</b></p></div>";
	p.innerHTML = msg;
	document.body.appendChild(p);
}

function quitAdhesePreview() {
	eraseAdheseCookie("adhese_preview");
	if(location.search.indexOf("adhesePreviewCreativeId") != -1){
		location.href = location.href.split("?")[0]
	}else{
		location.reload();
	}	
}

function clearURL(s){
	// clears url from # and ?
	var r = s;
	r = r.split(" ").join("_");
	r = r.split("|").join("_");
	if(r.indexOf("#")>0){
		r = r.substr(0, r.indexOf("#"));
	}
	if(r.indexOf("?")>0){
		r = r.substr(0, r.indexOf("?"));
	}
	return r
}

function getAdheseProfileData() {
	var p = new Object();
	if(typeof(user_postcode) != 'undefined') {
		p.zip = user_postcode;
	}
	if(typeof(user_leeftijd) != 'undefined') {
		p.age = user_leeftijd;
	}
	if(typeof(user_geslacht) != 'undefined') {
		p.gender = user_geslacht;
		if (p.gender == "M") p.gender = "1";
		else if (p.gender == "V") p.gender = "3";
		else p.gender = "0";
	}
	if(typeof(user_abo) != 'undefined') {
		p.abo = user_abo;
	}
	
	//set adheseSite as memberStatus
	p.memberstatus = adheseSite;
	
	// add interests
	p.interest = "";
	if(typeof(job_functie) != 'undefined') {
		p.interest += "jobs" + job_functie;
	}
	if (document.referrer) {
		var ref = document.referrer;
		p.interest += ";" + ref.split(/\/+/g)[1];
	}
	//add proxi interest
	p.interestExtra = ";prx";
	
	// add brands
	p.brand = AdheseBrowserDetect.browser + ";" + AdheseBrowserDetect.browser + AdheseBrowserDetect.version + ";" + AdheseBrowserDetect.OS;
	
	p.count = adhesePageview;
	return p;
}

function isResponsiveSite() {
	var m = document.getElementsByTagName("META");
	for (var x=0; x<m.length; x++) {
		if (m[x].name == "viewport") {
			return true;
		}
	}
	return false;
}

function initPageAdhese() {
	checkForPreview();
	checkForDemo();
	if(typeof(adheseRand) == 'undefined') {
		adheseRand = Math.round(Math.random()*1000);
	}
	
	// count pageview / day
	var v = readAdheseCookie("adhese_count");
	if (v!=undefined) adhesePageview = parseInt(v);
	createAdheseCookie("adhese_count",adhesePageview+1,0);
	AdheseBrowserDetect.init();
	adheseProfile = getAdheseProfileData();
}

function getAdheseAdLoc() {
	var res = new Array();
	//if(adheseLocation == "" || adheseLocation == "undefined"){
		if(typeof(adhese_extra) != 'undefined') {
			if(adhese_extra == "nbo_videopartner" || adhese_extra == "dso_videopartner" || adhese_extra == "ave_videopartner"){
				if(typeof(provider) != undefined){
					return new Array("_" + adhese_extra + '_' + provider + "_");		
				}
			}else{
				return new Array("_" + adhese_extra + "_");
			}
		}
		if(typeof(sectie) == 'undefined') {
			sectie = "0";
		}
		if(typeof(subsectie) == 'undefined') {
			subsectie = "110";
		}
		if(typeof(section) == 'undefined') {
			section = "unknown";
		}
		var adh_channel = "";
		if(typeof(kanaal) != 'undefined') {
			adh_channel = "_kanaal_" + kanaal + "_";
		}
		if(typeof(channel) != 'undefined') {
			adh_channel = "_kanaal_" + channel + "_";
		}
		var loc = "_";
		adheseLang = "nl";
		if (location.protocol.indexOf("file")!=-1) {
			loc += section+"_";
		// food and shop
		} else if (location.host.indexOf("citysecrets.nieuwsblad.be") != -1 ){
			if(location.pathname == "/"){
				loc += "fns_nl_home_"
			}else{
				loc += "fns_nl_";
			}		
			// postcode
			if(typeof(postcode) != undefined){
				loc += "zip-" + postcode + "_";
			}
		} else if (location.host.indexOf("citysecrets.lavenir.net") != -1 ){
			if(location.pathname == "/"){
				loc += "fns_fr_home_"
			}else{
				loc += "fns_fr_";
			}
			// postcode
			if(typeof(postcode) != undefined){
				loc += "zip-" + postcode + "_";
			}
		// maintenance
		}else if(location.host.indexOf("mnt.standaard.be") != -1){
		
			if(location.search.toLowerCase().indexOf("pageid=1000") != -1){
				loc = "_dso_e2eee66e-b0b1-4f9f-ab77-a1432da40c7d_";
			}else if(location.search.toLowerCase().indexOf("pageid=1004") != -1){
				loc = "_dso_451c8e1e-f9e4-450e-aa1f-341eab6742cc_";			
			}else if(location.search.toLowerCase().indexOf("pageid=5000") != -1){
				loc = "_nbo_home_";			
			}else if(location.search.toLowerCase().indexOf("pageid=5001") != -1){
				loc = "_nbo_c0c3b215-10be-4f82-86d6-8b8584a5639d_";			
			}else if(location.search.toLowerCase().indexOf("pageid=5004") != -1){
				loc = "_nbo_2b11c4e2-caed-4ea5-b0bf-a689302e89d4_";			
			}else if(location.search.toLowerCase().indexOf("pageid=6000") != -1){
				loc = "_spo_972b9573-b54b-44f3-86af-df8857c80ac9_";			
			}
		} else if (location.host.indexOf("nieuwsblad")!=-1 && location.pathname.toLowerCase().indexOf("sportwereld")!=-1) {
			//if (location.pathname=="/sportwereld/" || location.pathname.indexOf("index.html")!=-1) loc += "spo_home_";
			//else loc += "spo_"+sectie+"_"+subsectie+"_";
			loc += "spo_"+section+"_";
			adheseSite = "nbo";
		} else if (location.host == "preview.nieuwsblad.be" || location.host == "test.nieuwsblad.be") { 
			loc = "_nbo_preview_"; 
		} else if (location.host == "preview.standaard.be" || location.host == "test.standaard.be") { 
			loc = "_dso_preview_";	
		} else if (location.host.indexOf("nieuwsblad")!=-1) {
			adheseSite = "nbo";
			if (location.search.indexOf("genericId=508")!=-1) {
				loc += "nbo_thema_fortis_";
			} else if (location.host.indexOf("verenigingen.nieuwsblad.be")!=-1) {
				loc += "nbo_verenigingen_";
			} else if (location.search.indexOf("genericId=404")!=-1) {
				loc += "nbo_thematic_";
			} else if (location.search.indexOf("genericId=507")!=-1) {
				loc += "nbo_dossier_batibouw_";
			} else if (location.pathname=="/" || location.pathname.indexOf("index.html")!=-1 || location.pathname.indexOf("cmstester.aspx")!=-1) {
				if(typeof(gentenaar) != 'undefined' && gentenaar) {
					loc += "nbo_gentenaarhome_";
				} else {
					loc += "nbo_home_";
				}
			} else if(location.pathname.indexOf("sportwereld")!=-1 || (typeof(cimpage) != 'undefined' && cimpage.indexOf('sw/')!=-1)) {
				loc += "spo_"+section+"_";
				//loc += "spo_"+sectie+"_"+subsectie+"_";
				adheseSite = "nbo";	
			} else if(typeof(postcode) == 'undefined' || postcode == '') {
				loc += "nbo_"+section+"_";
				//loc += "nbo_"+sectie+"_"+subsectie+"_";
			} else {
				loc += "nbo_zip-"+postcode+"_";
			} 
		} else if (location.host.indexOf("standaard.be")!=-1) {
			if (location.search.indexOf("kanaalid=715")!=-1)
				loc += "dso_dossier_batibouw_";
			else if (location.pathname=="/" || location.pathname=="/index.html" || location.pathname.indexOf("cmstester.aspx")!=-1 || location.pathname=="/index.aspx")
				loc += "dso_home_";
			else if (location.pathname=="/mobilia" || location.pathname=="/mobilia/")
				loc += "dso_mobilia_home_";
			else 
				loc += "dso_"+section+"_";
			adheseSite = "dso";
		} else if (location.host.indexOf("actu24")!=-1 || location.host.indexOf("lavenir")!=-1) {
			adheseLang = "fr";
			if (location.pathname=="/" || location.pathname=="/index.html" || location.pathname=="/index.aspx")
				loc += "ave_home_";
			else if(typeof(zipcode) == 'undefined' || zipcode == '')
				loc += "ave_"+section+"_";
			else
				loc += "ave_zip-"+zipcode+"_";
			adheseSite = "ave";
		} else if (location.host.indexOf("jobat")!=-1 || (location.host.indexOf("preview38.uniway.be")!=-1)) {
			if(typeof(taal) == 'undefined') taal = "nl";
			if(typeof(categorie) == 'undefined') categorie = "0";
			loc += "jobat_" + taal + "_" + categorie + "_";
			adheseSite = "jobat";
		} else if (location.host.indexOf("sportwereld.be")!=-1) {
			loc += "spo_"+section+"_";
			//loc += "spo_"+sectie+"_"+subsectie+"_";
			adheseSite = "nbo";
		} else if (location.host.indexOf("gezondheid.be")!=-1) {
			adheseLang = "--";
			loc += "gezondheid.be_topical_";
		} else if (location.host.indexOf("vroom.be")!=-1) {
			adheseLang = "--";
			loc += "vroom.be_topical_";
		} else if (location.host.indexOf("zdnet.be")!=-1) {
			adheseLang = "--";
			loc += "zdnet.be_topical_";
		} else if (location.host.indexOf("clickx.be")!=-1) {
			adheseLang = "--";
			loc += "clickx.be_topical_";
		} else if (location.host.indexOf("itprofessional.be")!=-1) {
			adheseLang = "--";
			loc += "itprofessional.be_topical_";
		// zimmo.be
		}else if(location.host.indexOf("productie.zimmo.be")!=-1 || location.host.indexOf("ruuds.zimmo.be")!=-1){
			loc = "_zimmo_nl_productie_";
		}else if(location.host.indexOf("zimmo.be")!=-1){
			if(typeof(sitelang) != undefined){
				adheseLang = sitelang;
			}
			loc += "zimmo_"+adheseLang+"_";
		
		} else if (location.host.indexOf("inmemoriam.be")!=-1 || location.host.indexOf("im.brainlane.com")!=-1 ) {
			adheseLang = "--";
			if (location.pathname == "/nl" || location.pathname == "/nl/"){
				loc += "inmemoriam_nl_home_";
			}else if(location.pathname.indexOf("/nl/") != -1){
				loc += "inmemoriam_nl_";
			}else if (location.pathname == "/fr" || location.pathname == "/fr/"){
				loc += "inmemoriam_fr_home_";
			}else if(location.pathname.indexOf("/fr/") != -1){
				loc += "inmemoriam_fr_";
			}
		} else if (location.host.indexOf("flanderstoday.eu")!= -1 || location.host.indexOf("mediargus.be")!= -1){
			loc = "_fla_today_";
		} else if (location.host.indexOf("thebulletin.be")!=-1 || location.host.indexOf("xpats.com")!=-1) {
			if (location.pathname == "/en" || location.pathname == "/")
				loc = "_xpats_home_";
			else
				loc = "_xpats_other_";
		} 
		res[0] = loc;
		if (adh_channel != "") 
			res[1] = "_" + adheseSite + adh_channel;
		adheseLocation = loc;
	//}
	return res;
}

function initAdhese(templateId) {
	var loc = getAdheseAdLoc();
	initAdheseWithLoc(loc, templateId);                                
}

function initAdheseAB(templateId, slotId) {
	initAdheseDimensionAB(templateId, -1, -1, slotId);
}

function initAdheseDimension(templateId, minWidth, maxWidth) {
	initAdheseDimensionAB(templateId, minWidth, maxWidth, "");
}

var adheseABArray = new Array();

function setupAdheseAB(jsonText) {
	var abConfig = eval(jsonText);
	for (var x=0; x<abConfig.formats.length; x++) {
		adheseABArray.push( abConfig.formats[x].templateId + abConfig.formats[x].values[Math.round(Math.random()*(abConfig.formats[x].values.length-1))] );
	}
}

function initAdheseDimensionAB(templateId, minWidth, maxWidth, slotId) {
	if (document.body && document.body.clientWidth) {
		if ( (minWidth == -1 || minWidth <= document.body.clientWidth) && (maxWidth == -1 || maxWidth >= document.body.clientWidth) ) {
			var loc = getAdheseAdLoc();
			if(slotId != undefined && slotId != "") {
				var l = adheseABArray.length;
				var s = templateId+slotId;
				for (var i=0; i<l; i++) {
					if (adheseABArray[i]==s) {
						initAdheseWithLoc(loc, s);
						break;
					}
				}				
			} else {
				initAdheseWithLoc(loc, templateId);
			}
		}
	}
}

function initAdheseWithLoc(loc, templateId) {
	if (adhesePreviewMode) {
		var foundPreviewTempl = false	
		for(var i=0;i<adhesePreviewTemplateId.length;i++){
			if (adhesePreviewTemplateId[i] == templateId){
				tagAdhesePreview(adhesePreviewCreativeId[i], adhesePreviewSlotId[i]);
				foundPreviewTempl = true;
			} 	
		}
		if(!foundPreviewTempl){
			tagAdhese(loc, templateId);
		}   
	} else if (adheseDemoMode && (templateId == "BAN" || templateId == "SKY" || templateId == "IMU")) {
		if (templateId == "BAN") {
			var ids = new Array("12922", "12984", "12299");
			tagAdhesePreview("http://corelio.adhese.org/creatives/preview/tag.do?id=" + ids[Math.round(Math.random() * (ids.length-1))]);
		}
		if (templateId == "SKY") {
			var ids = new Array("13157", "13230");
			tagAdhesePreview("http://corelio.adhese.org/creatives/preview/tag.do?id=" + ids[Math.round(Math.random() * (ids.length-1))]);
		}
		if (templateId == "IMU") {
			var ids = new Array("13097", "13183", "12470");
			tagAdhesePreview("http://corelio.adhese.org/creatives/preview/tag.do?id=" + ids[Math.round(Math.random() * (ids.length-1))]);
		}
	} else {
		tagAdhese(loc, templateId);
	}
}

function initAdheseIframe(templateId, width, height) {
	var loc = getAdheseAdLoc();
	if (adhesePreviewMode) {
		if (templateId == adhesePreviewTemplateId){
			tagAdhesePreview(adhesePreviewCreativeId);
		} else {
			tagAdheseIframe(loc, templateId, width, height);
		}
	} else {
		tagAdheseIframe(loc, templateId, width, height);
	}
}

function tagAdheseIframe(h, t, wi, he) {
document.write('<iframe name="iframe-tag" width="' + wi + '" height="' + he + '" frameborder="no" border="0" margin="0" src="http://host3.adhese.be/tag/iframe-tag.html?' + h[0] + '&' + t + '&' + wi + '&' + he + '&' + adheseRand + '"></iframe>');
}

function tagAdhesePreview(c, s) {
	if(s){
		document.write('<scr' + 'ipt language="JavaScript" type="text/javascript" src="http://corelio.adhese.org/creatives/preview/tag.do?id=' + c + '&slotId=' + s + '"><\/scr' + 'ipt>');
	}else{
		document.write('<scr' + 'ipt language="JavaScript" type="text/javascript" src="http://corelio.adhese.org/creatives/preview/tag.do?id=' + c + '"><\/scr' + 'ipt>');
	}
}

function tagAdhese(h, t) {
	var slot = "";
	if(typeof h == "object"){
		slot = h[0] +'-'+ t;
		if (h.length>1) {
			slot = h[1] +'-'+ t + "/sl" + h[0] +'-'+ t;
		}
	}else{
		slot = h +'-'+ t;;
	}
	
	if (adheseProfile != undefined && adheseProfile.zip != undefined && adheseProfile.zip != 'undefined' && adheseProfile.zip != 'null') {
		document.write('<scr' + 'ipt type="text/javascript" src="http://ads.adhese.be/ad3/sl'+ slot +'/la' + adheseLang + '/rn' + adheseRand + '/pv' + adheseProfile.count + '/ci' + adheseProfile.zip + '/ag' + adheseProfile.age + '/ge' + adheseProfile.gender + '/br' + adheseProfile.brand + '/ms' + adheseProfile.memberstatus + '/in' + adheseProfile.interest + adheseProfile.interestExtra + '/?t=' + new Date().getTime() + '"><\/scr' + 'ipt>');
	} else {
		document.write('<scr' + 'ipt type="text/javascript" src="http://ads.adhese.be/ad3/sl'+ slot +'/la' + adheseLang + '/rn' + adheseRand + '/pv' + adheseProfile.count + '/br' + adheseProfile.brand + '/ms' + adheseProfile.memberstatus + '/in' + adheseProfile.interest + adheseProfile.interestExtra + '/?t=' + new Date().getTime() + '"><\/scr' + 'ipt>');
	}
}

function addAdheseLoadEvent(func) {
	var ol = window.onload;
	if (typeof window.onload != 'function') {window.onload = func;}
	else {window.onload = function() {ol(); func();}}
}

function refreshAds() {
	
}

var AdheseBrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "unknownBrowser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "unknownVersion";
		this.OS = this.searchString(this.dataOS) || "unknownOS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.userAgent,
			subString: "Windows NT 6.1",
			identity: "Windows7"
		},
		{
			string: navigator.userAgent,
			subString: "Windows NT 6.0",
			identity: "WindowsVista"
		},
		{
			string: navigator.userAgent,
			subString: "Windows NT 5.1",
			identity: "WindowsXP"
		},
		{
			string: navigator.userAgent,
			subString: "Windows 98",
			identity: "Windows98"
		},
		{
			string: navigator.userAgent,
			subString: "Windows 95",
			identity: "Windows95"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		},
		{
			string: navigator.userAgent,
			subString: "iPad",
			identity: "iPad"
	    },
		{
			string: navigator.userAgent,
			subString: "iPhone",
			identity: "iPhone"
	    },
		{
			string: navigator.userAgent,
			subString: "iPod",
			identity: "iPod"
	    },
		{
			string: navigator.userAgent,
			subString: "Android",
			identity: "Android"
	    },
		{
		  	string: navigator.userAgent,
			subString: "PlayStation Portable",
			identity: "PlayStationPortable"
		},
		{
			string: navigator.userAgent,
			subString: "Bada",
			identity: "Bada"
	    },
	    {
			string: navigator.userAgent,
			subString: "BlackBerry",
			identity: "BlackBerry"
	    },
		{
			string: navigator.userAgent,
			subString: "Symbian",
			identity: "Symbian"
	    }
	]

};

function AdheseTracker(d, i) {
	this.visiblePerc = 0.50;
	this.viewPortDivID= d;
	this.isVisible = false;
	this.overStart = [];
	this.creativeID = i;
	this.arr = new Array();
	this.timeout = null;
	this.image = document.createElement('img');
	
	var me = this;
	
	var onsF = window.onscroll;
	var scrollF = function() {me.checkVisible(me);};
	if (typeof window.onscroll != 'function') {window.onscroll = scrollF;}
	else {window.onscroll = function() {onsF(); scrollF();}}
	
	var onbF = window.onblur;
	var blurF = function() {if (me.isVisible) me.stopTimer("visible");};
	if (typeof window.onblur != 'function') {window.onblur = blurF;}
	else {window.onblur = function() {onbF(); blurF();}}
	
	var onfF = window.onfocus;
	var focusF = function() {if (me.isVisible) me.startTimer("visible");};
	if (typeof window.onfocus != 'function') {window.onfocus = focusF;}
	else {window.onfocus = function() {onfF(); focusF();}}
	
	me.addMouseEvents();
	me.addListener(this.image, 'load',	function(e){me.imageLoaded(me);}, false);
	me.checkQueue();
	
}
AdheseTracker.prototype.getViewH = function() {
    if (typeof window.innerWidth != 'undefined') {
        return window.innerHeight;
    } else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {
        return document.documentElement.clientHeight;
    } else {
        return document.getElementsByTagName('body')[0].clientHeight;
    }
    return 0;
}
AdheseTracker.prototype.getPosY = function() {
    var oElement = document.getElementById(this.viewPortDivID);
	var posX = 0;
	var posY = 0;
    if (typeof(oElement.offsetParent) != 'undefined') {
        for (posX = 0, posY = 0; oElement; oElement = oElement.offsetParent) {
            posX += oElement.offsetLeft;
            posY += oElement.offsetTop;
        }
    } else {
        posX += oElement.x;
        posY += oElement.y;
    }
    return posY;
}
AdheseTracker.prototype.getScrollY = function() {
    if (document.documentElement && document.documentElement.scrollTop) {
        return document.documentElement.scrollTop;
    } else if (document.body && document.body.scrollTop) {
        return document.body.scrollTop;
    } else if (window.pageYOffset) {
        return window.pageYOffset;
    } else if (window.scrollY) {
        return window.scrollY;
    }
    return 0;
}
AdheseTracker.prototype.getDivH = function() {
	return document.getElementById(this.viewPortDivID).offsetHeight;
}
AdheseTracker.prototype.checkVisible = function(){
	var posY = this.getPosY();
	var scrollY = this.getScrollY();
	var viewH = this.getViewH();
	var divH = this.getDivH();

	if((posY + (divH*this.visiblePerc) - scrollY) < viewH && (posY + (divH*(1-this.visiblePerc)) - scrollY) > 0){
		if(!this.isVisible){
			this.isVisible = true;
			this.startTimer("visible");
		}		
	}else{
		if(this.isVisible){
			this.isVisible = false;
			this.stopTimer("visible");
		}
	}				
}
AdheseTracker.prototype.mouseover = function(t) {
	this.startTimer("mouse");	
}
AdheseTracker.prototype.mouseout = function() {
	this.stopTimer("mouse");
}
AdheseTracker.prototype.startTimer = function(t) {
	this.overStart[t] = new Date().getTime();
}
AdheseTracker.prototype.stopTimer = function(t) {
	var dur = Math.round((new Date().getTime() - this.overStart[t])*0.001);
	if (dur>0) {
		this.log("http://track.corelio.adhese.be/log/creative/"+this.creativeID+"/"+t+"/"+dur+"sec/");
	}
}
AdheseTracker.prototype.addMouseEvents = function() {
	var me = this;
	document.getElementById(this.viewPortDivID).onmouseover = function() {me.mouseover(me);};
	document.getElementById(this.viewPortDivID).onmouseout = function() {me.mouseout(me);};
	this.checkVisible();
}
AdheseTracker.prototype.log = function(s){
	this.arr.push(s);	
}
AdheseTracker.prototype.addListener = function(element, type, expression, bubbling){
  bubbling = bubbling || false;
  if(window.addEventListener)	{ // Standard
    element.addEventListener(type, expression, bubbling);
    return true;
  } else if(window.attachEvent) { // IE
    element.attachEvent('on' + type, expression);
    return true;
  } else return false;
}                                
AdheseTracker.prototype.popFromArray = function(){
	return this.arr.shift(); 
}
AdheseTracker.prototype.processQueue = function(m){
	if(m.arr.length != 0){
		m.image.src = this.popFromArray();
	}else{
		m.checkQueue();
	}
}
AdheseTracker.prototype.imageLoaded = function(m){
	m.processQueue(m);
}
AdheseTracker.prototype.checkQueue = function(){
	var me = this;
	if (me.arr.length == 0) {
		clearTimeout(me.timeout);
		me.timeout = setTimeout(function() {me.processQueue(me)}, 500);
	}
}
var AdheseFlashTool = {
  	flashInstalled : false,
	flashWorking : false,
	init : function(){             
		this.checkFlashInstalled();
	},
	checkFlashInstalled : function (){
		if (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"]){
			this.flashInstalled = navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin;
		}else if (document.all && (navigator.appVersion.indexOf("Mac")==-1)){
			eval ('try {var xObj = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");if (xObj) this.flashInstalled = true; xObj = null; } catch (e) {}');
		}
	}  
};
AdheseFlashTool.init();

if(window.location.search.indexOf("tagtest=true") == -1){
  //init overall / once per page load
  initPageAdhese();
}else{
	document.write('<scr'+'ipt type="text/javascript" charset="utf-8" src="http://pool.adhese.be/tag/tagv1_test.js"></scr'+'ipt>');
}

