// JavaScript Document

//alert('track this');


var WatchDog = function(constructVars) {
	this.trackingVars = {
		viewportSize: null,
		//url: null,
		title: null
	};
	this.client = 0;
	this.trackerImgUrl = "http://tracking.dogindot.de/trackerimg.php";
	this.counterJsUrl = "http://tracking.dogindot.de/counter.js";
	this.url = window.location.href;
	
	this.initialize();
	
	
}

WatchDog.prototype = {
	initialize: function() {
		// collect the data
		this.trackingVars.viewportSize = this.getViewportSize();
		//this.trackingVars.url = this.getUrl();
		this.trackingVars.title = this.getTitle();
		this.trackingVars.url = this.url;
		// generate the tracking pixel
		this.sendData();
		
		//referer
		if(location.href.indexOf('Update') == -1)
    	document.cookie = "referrer=" + document.referrer + "; path=/";
		
		
		// check unload
		window.onunload = function(){
			//alert('schüss');
		};
		
	},
	getViewportSize: function() {
		var w = 0;
		var h = 0;
	
		//IE
		if(!window.innerWidth)
		{
			//strict mode
			if(!(document.documentElement.clientWidth == 0))
			{
				w = document.documentElement.clientWidth;
				h = document.documentElement.clientHeight;
			}
			//quirks mode
			else
			{
				w = document.body.clientWidth;
				h = document.body.clientHeight;
			}
		}
		//w3c
		else
		{
			w = window.innerWidth;
			h = window.innerHeight;
		}
		return w+'x'+h;

	},
	getUrl: function() {
		return window.location.href;
	},
	getTitle: function() {
		
		return document.title;
	},
	sendData: function() {
		// creates an img tag with the src of the tracking php
		// adding a get querystring like:
		// ?windowstuff=bla&title=bar&url=encodedurl
		var seriVars = this.serialize(this.trackingVars);
		
		var getVars = this.getGetParam();
		this.client = getVars.client;
		
		seriVars = encodeURI(seriVars);
		var ref = escape(getReferrer());
		//alert(ref);
		document.write('<img style="display:none;" src="'+this.trackerImgUrl+'?id='+this.client+'&'+seriVars+'&ref='+ref+'" />');
	},
	serialize: function(object) {
		var newObj = "";
		for (itemE in object) {
			newObj += itemE + "=" + object[itemE] + "&";
		}
		return newObj;
	},
	getGetParam: function() {
		var scriptTags = document.getElementsByTagName('script');
		
		
		for (itemE in scriptTags) {
			
			var src =   scriptTags[itemE].src ;
			var isCounterScript = false;
			if(src){
				
				isCounterScript = src.match(/counter.js/);
			}
			if(isCounterScript ){
				
				var clientparam = src.match(/(client=)(\d)+/);
				clientparam = clientparam[0];
				var clientarr = clientparam.split('=');
				
				var client = clientarr[1];
				
			}
		}
		
		
		var param = {client: client};
		return param;
	}
}

function getReferrer()
{
	var nameEQ = "referrer=";
	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 init(){
	
	if(location.href.indexOf('Update') == -1)
    document.cookie = "referrer=" + document.referrer + "; path=/";
	
	 new WatchDog();
}


function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
         if (oldonload) {
           oldonload();
         }
         func();
    }
  }
}
                         
addLoadEvent(init());

	//window.onload = function() { new WatchDog(); };
