<!--
// Clicky.js - A library for tracking when users click links away from your site.
// Written by Damien Katz (http://damienkatz.net) and placed into the public domain.

// To use Clicky, simply include put this line into your html:
//		<script type='text/javascript' src='/clicky.js'></script>

// If you have multiple domains for the same site then separate the names
// below with a pipe. This way links between the domain names won't be
// tracked.

//var clicky_additonal_domains = "YOURDOMAIN1.COM|YOURDOMAIN2.COM";
var clicky_additonal_domains = "mobil-parken.de";

// the clicky url report is made of the following parts:
// http://yourdomain.com/[link_url_arg]LINKURL[text_url_arg]LINKTEXT[stamp_url_arg]TIMESTAMP

var clicky_link_url_arg = "--link:";
var clicky_text_url_arg = "clicky/text:";
var clicky_stamp_url_arg = "?stamp=";

// How long clicky pauses to allow for image loading
var clicky_image_load_pause_msecs = 1000;


// Returns the domain portion from an url
function clicky_getDomain(url) {
	var i = url.indexOf("://");

	if (i == -1) {
		return "";
	}
	
	url = url.substring(i + 3, url.length);

	i = url.indexOf("/");
	if (i != -1) {
		url = url.substring(0, i);

	}
	return url;
}

// This just swallows any clicky errors that happens
function clicky_handleError() {
	return true;
}

// Our onclick event handlers
function clicky_report_onclick(ev) {
	// change to our new error handler
	var oldonerror = window.onerror;
	window.onerror = clicky_handleError;

	clicky_report_onclick_inner(ev);

	// change back error handler
	window.onerror = oldonerror;

	if (old_onclick) {
		// run old onclick
		old_onclick(e);
	}
}

// The actual guts of the onclick event handlers
function clicky_report_onclick_inner(ev) {

	var text;
	var url;
	var target;

	if(ev) {
		target = ev.target;
	}
	else {
		target = window.event.srcElement;
	}

	// Try to find an element in the target hierarchry that has
	// an href property.

	for (var i=0; target && i<=20; i++) {
		// lets put a 20 limit on the number of parents we search
		if(target.href) {
			break;
		}
		target = target.parentNode;
	}

	if (target && target.href) {
		url = target.href;
		// try various things to get the text
		if (target.innerHTML) {
			text = target.innerHTML;
		}
		else if (target.innerText) {
			text = target.innerText;
		}
		else if(target.text) {
			text = target.text;
		}
	}

	if (!url || typeof(url) != 'string' || url == "") {
		return;
	}

	if (!text || typeof(text) != 'string' || text == "") {
		text = "[no text found]";
	}
	
	var currentDomain = clicky_getDomain("" + window.location);
	var currentBaseDomain = currentDomain;
	
	if (currentBaseDomain.indexOf("www.") == 0) {
		// strip off www from front
		currentBaseDomain = currentBaseDomain.substring(3, currentBaseDomain.length);
	}
	
	var expressionStr = "(\\w+[.])*(" + currentBaseDomain + "|" + clicky_additonal_domains + ")$";

	var regex = new RegExp(expressionStr, "i");

	if (text.length > 20) {
		text = text.substring(0, 20) + "...";
	}
	
	if (!clicky_getDomain(url).match(regex)) {
		// its not our own domain(s), lets log it
		var timeStamp = new Date();
		var reportUrl = "http://" + currentDomain + "/" + clicky_text_url_arg + escape(text) + clicky_link_url_arg + escape(url) + clicky_stamp_url_arg + timeStamp.valueOf();
		var image = new Image();
		image.src = reportUrl;

		// loop for a bit to let the image load
		var now = new Date();
		var stopTime = now.getTime() + clicky_image_load_pause_msecs;
		while(now.getTime() < stopTime) {
			now = new Date();
		}
	}
}

var old_onclick = document.body.onclick;
document.body.onclick = clicky_report_onclick;


// -->
