/* � 2010-2011 - Insite Innovations and Properties B.V., an Adversitement group company. All rights reserved.
 * */

// Initialize the Insight Web Analtyics Object
if (typeof(iwa) == "undefined") {
	var iwa = new Object();
}

// Code version:
iwa.cv = "20110609 0.0.2";

// Configuration specific variables:
iwa.c = new Object();

// Specify the sensor url (server).
iwa.c.sensorUrl = "i01.trusted9.nl";
// Specify the sensor application (pixel).
iwa.c.sensorApp = "i.gif";
// Specify the internal filters.
iwa.c.internalLinks = "watkanikdoen.nl";
// Profile on which the data is reported.
iwa.profile = "watkanikdoen";
// Which cookiedomain to use.
iwa.c.cookieDomain = "watkanikdoen.nl";

// Execute before the pixel is sent. This is meant to make some last changes to the data to be sent.
iwa.onBeforeSent = function() {}

// DO NOT ALTER ANYTHING BELOW THIS LINE.
// Setup the pixel and sent it to Sensor.
// version 0.8.1 by Adversitement
iwa.transmit = function() {
	if (typeof(iwa.c.sensorUrl) == "undefined" || iwa.c.sensorUrl == "") {
		return false;
	}
	if (typeof(iwa.c.sensorApp) == "undefined" || iwa.c.sensorApp == "") {
		return false;
	}
	// Specify whether the sensor is on a secure domain.
	var sd = false;
	if (typeof(document.location) != "undefined" && document.location.toString().indexOf("https://")>-1) {
		sd = "https";
	}else if(typeof(document.location) != "undefined" && document.location.toString().indexOf("http://")>-1) {
		sd = "http";
	}
	// Set the environment by hand:
	if (typeof(iwa.c.useSecure) != "undefined" && iwa.c.useSecure == true) {
		sd = "https";
	}else if(typeof(iwa.c.useSecure) != "undefined" && iwa.c.useSecure == false) {
		sd = "http";
	}
	// Specify the sensorUrl:
	var sensorUrl = sd+"://"+iwa.c.sensorUrl+"/"+iwa.c.sensorApp;
	// Set the random visitorID when it doesn't exist. If existing renew:
	var rn = Math.floor(Math.random()*11111111111111111111);
	var vi = iwa.getCookie("i_vi");
	var exp = (20*365);
	if (typeof(vi) == "undefined" || (typeof(vi) != "undefined" && vi == "")) {
		iwa.setCookie("i_vi", "vi_"+rn, exp);
		vi = "vi_"+rn;
	}else if(typeof(vi) != "undefined" && vi != ""){
		iwa.setCookie("i_vi", vi, exp);
	}
	iwa.vID = vi;
	// Call the onBeforeSent function if it has been initialised:
	if (typeof(iwa.onBeforeSent) != "undefined" && typeof(iwa.onBeforeSent) == "function") {
        iwa.onBeforeSent();
    }
	// Set constants (default variables):
	if (typeof(iwa.linkType) == "undefined" || (typeof(iwa.linkType) != "undefined" && iwa.linkType == "")) {
		iwa.setConstants();
	}
	if (typeof(iwa.linkType) != "undefined" && iwa.linkType != "") {
		if (typeof(iwa.track) != "undefined" && iwa.track != "") {
			var track = iwa.track.split(",");
		}else{
			var track = new Array();
		}
		track.push("linkLabel");
		track.push("linkName");
		track.push("link");
		track.push("linkType");
		track.push("vID");
		track.push("cv");
		track.push("profile");
	}
	// Generate the query parameter list:
	var k = "";
	var i = 1;
	var qp = new Array();
	qp[0] = "rn=" + Math.floor(Math.random() * 10000);
	for (k in iwa) {
        if (typeof(iwa[k]) != "undefined" && iwa[k] != "" && typeof(iwa[k]) != "function" && typeof(iwa[k]) != "object") {
        	if (typeof(iwa.linkType) != "undefined" && iwa.linkType != "") {
        		if (typeof(track) != "undefined" && track.length>0 && iwa.inarray(k, track)) {
           	 		qp[i] = k + "=" + escape(iwa[k]);
           	 		i++;
           	 	}
           	}else{
           		qp[i] = k + "=" + escape(iwa[k]);
            	i++;
           	}
        }
    }
	// Generate pixel:
	if (typeof(iwa.linkName) != "undefined" && iwa.linkName != "") {
		iwa.load(sensorUrl + "?log=1&" + qp.join("&"));
	}else{
		var loaded = false;
		if (window.addEventListener) {
			window.addEventListener("load", function() {
				iwa.load(sensorUrl + "?log=1&" + qp.join("&"));
				loaded = true;
			}, false);
		}else if(window.attachEvent) {
			window.attachEvent("onload", function() {
				iwa.load(sensorUrl + "?log=1&" + qp.join("&"));
				loaded = true;
			});
		}
		if (!loaded) {
			iwa.load(sensorUrl + "?log=1&" + qp.join("&"));
		}
	}
}

window.onload = function() {
	var elemns = document.getElementsByTagName("a");
	var il = iwa.c.internalLinks.split(",");
	for (var elemnt in elemns) {
	    if (typeof(elemns[elemnt]) != "undefined" && typeof(elemns[elemnt].href) != "undefined" && !iwa.inarray(elemns[elemnt].href, il)) {
			if (window.addEventListener) {
			    elemns[elemnt].addEventListener("click", function(e) {
			        iwa.el(e.target);
			    }, false);
			}else if(window.attachEvent) {
				elemns[elemnt].attachEvent("onclick", function(e) {
			        iwa.el(e.srcElement);
			    });
			}
		}
	}
}

//Exit link:
iwa.el = function(el) {
	var il = iwa.c.internalLinks.split(",");
	var ex = false;
	if (typeof(el) != "undefined" && typeof(el.parentNode) != "undefined" && typeof(el.parentNode.href) != "undefined") {
		var path = el.parentNode.href.split("/");
	}else if((typeof(el) != "undefined" && el.toString().indexOf("/")>-1) || (typeof(el.href) != "undefined")){
		var path = el.toString().split("/");
	}else{
		return false;
	}
	var element = el;
	if (typeof(path[2]) != "undefined" && path[2] != "") {
		var domain = path[2];
		if (!iwa.inarray(domain, il) || (typeof(element.href) != "undefined" && !iwa.inarray(element.href, il))) {
			window.onunload = function() {
				var content = element.innerHTML;
				var stripped = content.replace(/&(lt|gt);/g, function (strMatch, p1){
		 		 	return (p1 == "lt")? "<" : ">";
		 		});
				var stripped = stripped.replace(/<\/?[^>]+(>|$)/g, "");
				iwa.transmitLink(element, "Exit link: "+stripped, 'e');
			}
			return true;
		}
	}
}

iwa.load = function (url, preCallback, postCallback){
	var img = document.createElement("img");
	img.style.display = "none";
    img.style.border = "0px";
    img.style.width = "1px";
    img.style.height = "1px";
	if (typeof(preCallback) != "undefined") {
		preCallback();
	}
	if (img.readyState){ //Internet Explorer support
		img.onreadystatechange = function(){
			// When the readyState equals loaded or complete set the state to null and load the postCallback if
			// available.
			if (img.readyState == "loaded" || img.readyState == "complete"){
				img.onreadystatechange = null;
				if (typeof(postCallback) != "undefined") {
					postCallback();
				}
			}
		};
	} else { // Other regular browsers
		img.onload = function(){
			if (typeof(postCallback) != "undefined") {
				// When the script tag has loaded properly load the postCallback
				postCallback();
			}
		};
 	}
	img.src = url;
	document.getElementsByTagName("head")[0].appendChild(img);
}

iwa.setCookie = function(name, value, daysToExpire) {
	var expire = '',domain = '',path = '';
	if (typeof(iwa.c.cookieDomain) != "undefined" && iwa.c.cookieDomain != "") {
		var host = iwa.c.cookieDomain;
	}else if (typeof(iwa.c.sensorUrl) != "undefined" && iwa.c.sensorUrl != ""){
		var host = iwa.c.sensorUrl;
	}else{
		return false;
	}
	if (daysToExpire != undefined) {
		var d = new Date();
		if(isNaN(daysToExpire)) {
			d = daysToExpire;
		}else{
			d.setTime(d.getTime()+(86400000*parseFloat(daysToExpire)));
		}
		expire = '; expires=' + d.toGMTString();
		path = '; path=/';
		domain = '; domain='+host;
	}else {
		domain = '; domain='+host;
	}
	return (document.cookie = name + '=' + (value || '') + path + domain + expire);
}

iwa.getCookie = function(name) {
    var cookie = document.cookie.match ('(^|;) ?' + escape(name) + '=([^;]*)(;|$)');
    return (cookie ? unescape(cookie[2]) : "");
}

iwa.inarray = function(s, a) {
	if (typeof(s) != "undefined" && typeof(a) != "undefined") {
		for (var i=0;i<a.length;i++) {
			if (s.indexOf(a[i])>-1) {
				return true;
			}
		}
	}
	return false;
}

iwa.transmitLink = function(el, ln, lt) {
	iwa.linkName = (typeof(ln) != "undefined" && ln != "") ? ln : "unnamed link";
    iwa.linkType = (typeof(lt) != "undefined" && lt != "") ? lt : "l";
    if (typeof(el) != "undefined" && el.href != "") {
		iwa.link = el.href;
	}
	if (typeof(el) != "undefined" && typeof(el.innerHTML) != "undefined" && el.innerHTML != "" && typeof(iwa.linkLabel) == "undefined") {
		var stripped = el.innerHTML.replace(/&(lt|gt);/g, function (strMatch, p1){
 		 	return (p1 == "lt")? "<" : ">";
 		});
		var stripped = stripped.replace(/<\/?[^>]+(>|$)/g, "");
		iwa.linkLabel = stripped;
	}
	iwa.transmit();
	iwa.linkName = "";
	iwa.linkType = "";
	iwa.linkLabel = "";
	iwa.link = "";
	iwa.track = "";
}

// Set the base variables which should be send to Sensor.
iwa.setConstants = function() {
	iwa.sw = (typeof(screen.width) != "undefined") ? screen.width : "";
	iwa.sh = (typeof(screen.height) != "undefined") ? screen.height : "";
	iwa.cd = (typeof(screen.colorDepth) != "undefined") ? screen.colorDepth : "";
	iwa.dt = (typeof(document.title) != "undefined") ? document.title : "";
	iwa.dr = (typeof(document.referrer) != "undefined") ? document.referrer : "";
	iwa.cb = new Date().getTime();
	iwa.dl = (typeof(document.location) != "undefined") ? document.location.toString() : "";
}

iwa.transmit();

