function Stats(pid, tpid) {
	watchPoints = new Array();
	this.pid = pid;
	this.__stats = '';
	try {
		this.addLoadEvent(function () {
			stats.__stats = new _stats(pid, tpid, watchPoints);
			stats.__stats.init();
		});
	} catch (e) {
	}
}

Stats.prototype.addLoadEvent = function (func) {
	try {
		var oldonload = window.onload;
		if (typeof window.onload != 'function') {
			window.onload = func;
		} else {
			window.onload = function() {
				oldonload();
				func();
			};
		}
	} catch (e) {
	}
}

Stats.prototype.watch = function(wp) {
	watchPoints.push(wp);
}

Stats.prototype.track = function(tpid) {
	if(typeof this.__stats == 'object') {
		this.__stats.track(tpid, 'action');
	} else {
		var ___stats = new _stats(this.pid, tpid);
		___stats.init();
	}
}

_stats.prototype.init = function() {

	try {
		this.ref = escape(document.referrer);
		this.res = escape(document.URL);
		this.title = escape(document.title);
		
	} catch (e) {
	}
	
	if(this.tpid != undefined) {
		if(this.tpid == true) {
			this.track(this.title, 'default');
		} else {
			this.track(this.tpid, 'default');
		}
	}
	
	if(typeof this.watchPoints == 'object') {
		this.trackWatchPoints();
	}
}

function _stats (pid, tpid, watchPoints) {
	this.pid = pid;
	this.tpid = tpid;
	this.watchPoints = watchPoints;
}

_stats.prototype.track = function (tpid, type) {
	
	try{
		var img = document.createElement('img');
		img.setAttribute('id','statimg');
		img.setAttribute('width','1');
		img.setAttribute('height','1');
		img.setAttribute('alt','');
		img.setAttribute('src','http://stats.plantcph.dk/stats.gif?pid='+this.pid+'&ref='+this.ref+'&res='+this.res+'&tpid='+tpid+'&title='+this.title+'&type='+type+'&r='+Math.random());
		if (document.documentElement) {
			try{
				document.documentElement.appendChild(img);
			} catch(e){}
		} else {
			try {
				document.appendChild(img);
			} catch(e){}
		}
	} catch (e) {
	}
}

_stats.prototype.trackWatchPoints = function () {
	if(this.watchPoints.length > 0) {
		
		var hash;
		var hashes = document.URL.slice(document.URL.indexOf('?') + 1).split('&');
	   	for(var i = 0; i < hashes.length; i++){
	   		var hash = hashes[i].split('=');
			
			for(var j=0; j < this.watchPoints.length; j++) {
				if(hash[0] == this.watchPoints[j]) {
					try{
						
						this.track(hash[1], 'watch_point');
					} catch (e) {
					}
				}
			}
		}
	}
}