if(!Edito) var Edito = new Object();

Edito.Stats =
{
	UniqueUserId : "",
	UniqueVisitId : "",
	UniqueSessionId : "",
	
	IsUniqueUser : false,
	IsUniqueVisit : false,
	IsUniqueSession : false,
	
	Duration : 0,
	CurrentStep : 0,
	
	Init : function()
	{
		if (Edito.Stats.checkCookies())
		{
			Edito.Stats.Process();
			
			var img = new Image();
			img.src = Edito.Stats.statsUrl();
		}
	},
	
	Process : function()
	{
		var uuKey = "_uuid";
		var uvKey = "_uvid";
		var usKey = "_usid";
		
		var cookie = null;
		var expires = null;
		var expired = null;
		
		// Przeterminowane
		expired = new Date();
		expired.setFullYear(expired.getFullYear() - 1); // rok wstecz
		
		// Unique User
		expires = new Date();
		expires.setFullYear(expires.getFullYear() + 2); // 2 lata
		
		cookie = Edito.Stats.getCookie(uuKey);
		if (cookie == null || cookie.length == 0)
		{
			var uuid = Edito.Stats.getToken();
			
			Edito.Stats.setCookie(uuKey, uuid, expires, "/");
			Edito.Stats.setCookie(uvKey, "", expired, "/");
			Edito.Stats.setCookie(usKey, "", expired, "/");
			
			Edito.Stats.UniqueUserId = uuid;
			Edito.Stats.IsUniqueUser = true;
		}
		else
		{
			Edito.Stats.setCookie(uuKey, cookie, expires, "/");
			
			Edito.Stats.UniqueUserId = cookie;
			Edito.Stats.IsUniqueUser = false;
		}
		
		// Unique Visit
		expires = new Date();
		expires.setMinutes(expires.getMinutes() + 30); // 30 minut
		
		cookie = Edito.Stats.getCookie(uvKey);
		if (cookie == null || cookie.length == 0)
		{
			var uvid = Edito.Stats.getToken();
			
			Edito.Stats.setCookie(uvKey, uvid, expires, "/");
			Edito.Stats.setCookie(usKey, "", expired, "/");
			
			Edito.Stats.UniqueVisitId = uvid;
			Edito.Stats.IsUniqueVisit = true;
		}
		else
		{
			Edito.Stats.setCookie(uvKey, cookie, expires, "/");
			
			Edito.Stats.UniqueVisitId = cookie;
			Edito.Stats.IsUniqueVisit = false;
		}
		
		// Unique Session
		cookie = Edito.Stats.getCookie(usKey);
		if (cookie == null || cookie.length == 0)
		{
			var usid = Edito.Stats.getToken();
			var time = parseInt(new Date().getTime() / 1000);
			
			cookie = new Object();
			cookie.id = usid;
			cookie.t = time;
			cookie.s = 0;
			
			Edito.Stats.setCookie(usKey, Edito.Stats.Serialize(cookie), null, "/");
			
			Edito.Stats.UniqueSessionId = uvid;
			Edito.Stats.IsUniqueSession = true;
			Edito.Stats.Duration = 0;
			Edito.Stats.CurrentStep = 0;
		}
		else
		{
			var time = parseInt(new Date().getTime() / 1000);
			cookie = Edito.Stats.Unserialize(cookie);
			
			Edito.Stats.UniqueSessionId = cookie.id;
			Edito.Stats.IsUniqueSession = false;
			Edito.Stats.Duration = time - parseInt(cookie.t);
			Edito.Stats.CurrentStep = parseInt(cookie.s) + 1;
			
			cookie.t = time;
			cookie.s = Edito.Stats.CurrentStep;
			
			Edito.Stats.setCookie(usKey, Edito.Stats.Serialize(cookie), null, "/");
		}
	},
	
	// Generuje token
	getToken : function ()
	{
		var chars = "0123456789abcdefghiklmnopqrstuvwxyz";
		var length = 16;
		var randomString = '';
		for (var i = 0; i < length; i++)
		{
			var rnum = Math.floor(Math.random() * chars.length);
			randomString += chars.substring(rnum, rnum + 1);
		}
		return randomString;
	},
	
	// Ustawia cookie
	setCookie : function(name, value, expires, path, domain, secure)
	{
		//value = escape(value);
		
		document.cookie = name + "=" + value +
			((expires) ? "; expires=" + expires.toGMTString() : "") +
			((path) ? "; path=" + path : "") +
			((domain) ? "; domain=" + domain : "") +
			((secure) ? "; secure" : "");
	},

	// Pobiera wartość cookie na podstawie nazwy
	getCookie : function(name)
	{
		var dc = document.cookie;
		var cname = name + "=";
		var clen = dc.length;
		var cbegin = 0;
		
		while (cbegin < clen)
		{ 
			var vbegin = cbegin + cname.length;
			if (dc.substring(cbegin, vbegin) == cname)
			{ 
				var vend = dc.indexOf (";", vbegin);
				if (vend == -1) vend = clen;
				
				//return unescape(dc.substring(vbegin, vend));
				return dc.substring(vbegin, vend);
			}
			cbegin = dc.indexOf(" ", cbegin) + 1;
			
			if (cbegin== 0) break;
		}
		return null;
	},
	
	// Serializacja obiektu do stringa
	Serialize : function(data)
	{
		var cookie = new String();
		
		for (key in data)
		{
			if (cookie.length > 0)
				cookie += "&";
			
			cookie += key + "=" + data[key];
		}
		
		return cookie;
	},
	
	// Deserializacja obiektu ze stringa
	Unserialize : function(cookie)
	{
		var data = new Object();
		
		if (cookie != null)
		{
			var values = cookie.split('&');
			for (var i = 0; i < values.length; i++)
			{
				if (values[i].length > 0)
				{
					var item = values[i].split('=');
					data[item[0]] = item[1];
				}
			}
		}
		
		return data;
	},
	
	checkCookies : function()
	{
		var cookieEnabled = false;
		
		document.cookie = "_test=1; path=/";
		
		var pos = document.cookie.indexOf("_test=");
		if (pos != -1)
		{
			var start = pos + 6;
			var end = document.cookie.indexOf(";", start);
			if (end == -1)
			{
				end = document.cookie.length;
			}
			var value = document.cookie.substring(start,end);
			
			cookieEnabled = unescape(value);
		}
		
		var expires = new Date();
		expires.setDate(expires.getDate() - 10);
		document.cookie = "_test=; expires=" + expires.toGMTString() + "; path=/";
		
		return (cookieEnabled == "1") ? true : false;
	},
	
	detectFlash : function()
	{
		var flashVersion = 0;
		
		if (navigator.plugins && navigator.plugins.length)
		{
			x = navigator.plugins["Shockwave Flash"];
			if (x && x.description)
			{
				if(y = x.description.match(/\s(\d+)\./))
				{
					flashVersion = y[1];
				}
			}
		}
		else
		{
			for (var i = 20; i > 0; i--)
			{
				try
				{
					var flash = new ActiveXObject("ShockwaveFlash.ShockwaveFlash." + i);
					flashVersion = i;
					break;
				}
				catch (e) { false; }
			}
		}
		
		return flashVersion;
	},
	
	statsUrl : function()
	{
		var url = new String();
		
		var scripts = document.getElementsByTagName("script");
		for(var i = 0; i < scripts.length; i++)
		{
			var s = scripts[i];
			if(s.src && (result = s.src.match(/stats\.js(\?p=(.+))?&.+$/)))
			{
				if (result == undefined)
				{
					return url;
				}
				
				url += s.src.replace(/\/Themes\/.+?\/scripts\/stats\.js.+$/, '/');
				url += 'Stats/' + result[2].replace(/,/g, '/');
				url += '?Date=' + (new Date()).getTime();
				
				break;
			}
		}
		
		if (url.length == 0)
		{
			return url;
		}
		
		// uzytkownik
		url += '&UU=' + (Edito.Stats.IsUniqueUser ? '1' : '0') + '&UUID=' + Edito.Stats.UniqueUserId;
		
		// wizyta
		url += '&UV=' + (Edito.Stats.IsUniqueVisit ? '1' : '0') + '&UVID=' + Edito.Stats.UniqueVisitId;
		
		// sesja
		url += '&US=' + (Edito.Stats.IsUniqueSession ? '1' : '0') + '&USID=' + Edito.Stats.UniqueSessionId;
		
		// czas
		url += '&Duration=' + Edito.Stats.Duration;
		
		// krok
		url += '&Step=' + Edito.Stats.CurrentStep;
		
		// ustawienia ekranu
		if (screen)
		{
			var s = screen;
			if (s.width) url += '&Screen=' + s.width + 'x' + s.height;
			if (s.colorDepth) url += '&Bits=' + s.colorDepth;
		}
		
		// flash
		url += '&Flash=' + Edito.Stats.detectFlash();
		
		// java
		url += '&Java=' + (navigator.javaEnabled() ? '1' : '0');
		
		// aktualny adres url
		url += '&Url=' + escape(window.top.location.href).replace(/\+/g, '%2B').replace(/\"/g,'%22').replace(/\'/g, '%27');
		
		// adres referer
		if(typeof(window.top.document.referrer) != 'undefined')
		{
			url += '&Ref=' + escape(window.top.document.referrer).replace(/\+/g, '%2B').replace(/\"/g,'%22').replace(/\'/g, '%27');
		}
		
		return url;
	}
}

Edito.Stats.Init();