/* --- Medián WebAudit Mérőkód --- */

_webaudit = function ()
{
	this.WACID = null;
	this.WACIDName = "WACID";
	
	this.getCookie = 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;
	}
	
	this.setCookie = function ()
	{
		var name = this.WACIDName;
		var value  = this.WACID;
		var topDomain = this.getTopDomain(this.getDomain(document.URL));
	
		var date = new Date (2020,12,31,23,59,59);
		var expires = "; expires="+date.toGMTString();
		
		document.cookie = name+"="+value+expires+"; path=/; domain=" + topDomain;
	}
	
	this.generateID = function (splitter)
	{
		var sp = (splitter) ? splitter : 'A';
		var now = new Date();
		
		return Date.parse(now.toGMTString()) + sp + Math.floor(Math.random()*1000000000);
	}
	
	this.getTopDomain = function (fullDomain)
	{
		var darabok = fullDomain.split('.');
		
		return darabok[(darabok.length-2)] + '.' + darabok[(darabok.length-1)];
	}
	
	this.getDomain = function(url)
	{
		var urlDarabok = url.split('/');
		
		return urlDarabok[2];
	}
	
	this.WACID = this.getCookie (this.WACIDName);
	this.same = Math.floor(Math.random()*1000000);
	
	this.felbontas = "";
	this.wa_url = "@u=";
	this.wa_referrer = "@r=";
	
	if (this.WACID == null)
	{
		this.WACID = this.generateID ('A');
		this.setCookie();
	}
	
	this.same = this.same + "@c=" + this.WACID;
	
	if (screen) this.felbontas = '@s='+screen.width+'x'+screen.height;
	
	if (document.referrer) this.wa_referrer = this.wa_referrer+document.referrer;
	
	if (document.URL) this.wa_url = this.wa_url+document.URL;
	
	this.same = this.same + this.felbontas + this.wa_url + this.wa_referrer;
	
	this.GetPixel = function(uc)
	{
		rnd = "&rnd=" + Math.floor(Math.random()*1000000);
		
		return '<img style="position:absolute;top:-100px;left:-100px" src="http://audit.median.hu/cgi-bin/track.cgi?uc='+uc+'&dc=1&ui='+this.same+rnd+'" width="1" height="1">';
	}
	
	this.Measure = function(uc)
	{
		var pixelPlaceholder = document.getElementById("webaudit-container");
		
		if (!pixelPlaceholder)
		{
			pixelPlaceholder = document.createElement("div");
			
			pixelPlaceholder.setAttribute("webaudit-generated", "true");
			pixelPlaceholder.setAttribute("id", "webaudit-container");
			
			document.body.appendChild(pixelPlaceholder);
		}
		
		var pixel = this.GetPixel(uc);
		
		pixelPlaceholder.innerHTML = pixel;
	}
}

var Webaudit = new _webaudit();
