// netlog advertising by adhese.com

function Adhese(){ 
	this.cookie = undefined;
	this.rand = Math.round(Math.random()*100);
	this.ps = "nosect";
	this.host = "http://ads.ads.netlog.com";
	this.hostAsia = "http://asia.ads.netlog.com";
	this.pm = false;
	this.pci = undefined;
	this.pti = undefined;
   return this;
}

Adhese.prototype.initSlot = function(ti) {
	if (this.pm) {
		if (ti == this.pti){
			this.tagPreview(this.pci);
		} else {
			this.tag(this.ps, ti, this.u);
		}
	} else {
		this.tag(this.ps, ti, this.u);
	}		
};

Adhese.prototype.tag = function(s, t, u) {
	var country = u.substring(3,5);
	var slot = "_" + country + "_-" + t;
	var host = ['ar','en','he'].indexOf(this.distro) >= 0 ? this.hostAsia : this.host;
	if (slot) {
		document.write('<scr' + 'ipt language="JavaScript" type="text/javascript" src="'+ host +'/ad/' + s + '/' + slot + '/' + this.rand + '/' + u.substring(0,u.length-1) + '?t=' + new Date().getTime() + '"><\/scr' + 'ipt>');
	}
};

Adhese.prototype.initSlotTrack = function(ti) {
	this.track(this.ps, ti, this.u);
};

Adhese.prototype.track = function(s, t, u) {
	var img = new Image();
	img.src = this.host +'/ad/' + s + '/' + t + '/' + this.rand + '/' + u.substring(0,u.length-1) + '?t=' + new Date().getTime();
	img.width=1;
	img.height=1;
	document.getElementsByTagName("BODY")[0].appendChild(img);
};

Adhese.prototype.tagPreview = function(c) {
	document.write('<scr' + 'ipt language="JavaScript" type="text/javascript" src="http://netlog.adhese.org/creatives/preview/tag.do?id=' + c + '"><\/scr' + 'ipt>');
};

Adhese.prototype.createCookie = function(name,value,days) {
	if (days) {
		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=/";
};

Adhese.prototype.readCookie = function(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;
};

Adhese.prototype.eraseCookie = function(name) {
	this.createCookie(name,"",-1);
};

Adhese.prototype.showPreviewSign = function() {
	var p = document.createElement('DIV');
	var msg = "<div onclick='adhese.quitPreview(); 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;'>";
	msg += "<p><b>adhese<br>preview<br>close</b></p></div>";
	p.innerHTML = msg;
	document.body.appendChild(p);
};

Adhese.prototype.quitPreview = function() {
	this.eraseCookie("adhese_preview");
	location.reload();
};

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

Adhese.prototype.checkForPreview = function() {
	//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") {
				this.pci = p[x].split("=")[1];
				if (this.pti) break;
			}
			if (p[x].split("=")[0]=="adhesePreviewCreativeTemplate") {
				this.pti = p[x].split("=")[1];
				if (this.pti) break;
			}
		}
		this.createCookie("adhese_preview",this.pci+"&"+this.pti,0);
		this.pci = unescape(this.pci);
		this.pm = true;
		this.addLoadEvent(this.showPreviewSign);
	} else if (this.readCookie("adhese_preview")) {
		var v = this.readCookie("adhese_preview").split("&");
		this.pci = unescape(v[0]);
		this.pti = v[1];
		this.pm = true;
		this.addLoadEvent(this.showPreviewSign);
	}
};

Adhese.prototype.initialize = function() {
	this.checkForPreview();

	if(typeof(ComCore.Page.pageSection) == 'undefined') {
		this.ps = "nosect";
	} else {
		this.ps = ComCore.Page.pageSection;
		this.ps = this.ps.replace(/\//g, "_");
	}

	if (location.pathname.indexOf("/apps/")!=-1) {
		var lp = location.pathname.split("/");
		if (lp.length >= 3) 
			this.ps = lp[1] + "_" + lp[2];
	} else if (location.pathname.indexOf("/groups/")!=-1) {
		this.ps = "groups_groups";
	}
	this.u = this.readCookie('ud');
	if (this.u) {
		this.u = unescape(this.u);
		this.distro = this.u.substring(0,2).toLowerCase();
	} else {
		this.u = '-/-/-1/-1/-//-1/';
		this.distro = 'nl';
	}

};

var adhese = new Adhese();
adhese.initialize();

