/*
	author: Wilfred Nas

	function: essent.tracking

	function to enable tracking of pages using site catalist
*/
// first we make sure that the essent namespace exists
if (typeof(essent) == "undefined") {
	var essent = {};
}

// and do the same for the wa namespace
if (typeof(wa) == "undefined") {
	var wa = {};
}
var scriptURL;
if (document.URL.indexOf("acties-tst.") != -1 || document.URL.indexOf("www-tst.") != -1){
	scriptURL = 'http://www-tst.essent.nl/content/system/';
} else if (document.URL.indexOf("acties-acc.") != -1 || document.URL.indexOf("www-acc.") != -1){
	scriptURL = 'http://www-acc.essent.nl/content/system/';
} else if (document.URL.indexOf("acties.") != -1 || document.URL.indexOf("www.") != -1 || document.URL.indexOf("www-staging.") != -1){
	scriptURL = 'http://www.essent.nl/content/system/';
};
essent.tracking = (function(){
	var conf = {
		// name : value
		//script : 'js/s_code.js', // location of the s_code script
		script: scriptURL + 'lib/js/s_code.js', // s_code
		openclosed : 'open'
	},	init = function(){
	
		// get the script and go to the execution function	
		$.getScript( conf.script,getData);
	},	getData = function(){
		// set up an empty var for page name.
		var pagename = '',
		// get a clean url
			cleanUrl = location.href.split('?')[0]
			.replace('https://','')
			.replace('http://','')
			.replace('.html','')
			.replace('.jsp','')
			.replace('/content','')
			.split('/');
		// get the starting bit of the url to detect the enviroment.	
		var envi = cleanUrl[0].split('.')[0];
	//	var envi = env[0]
		cleanUrl.splice(0,1);
		// check where we need to send our data to...

		switch (envi){
			case 'www-tst':
			case 'acties-tst':
			//	alert('test')
				un = 'advessnltest';
				pagename = 'acties-tst:';
				break;
			case 'www-acc':
			case 'acties-acc':
			case 'www-staging':
			//	alert('acc')
				un = 'advessnlacc';
				pagename = 'acties-acc:';
				break;
			default:
			//	alert('prod')
				un = 'advessnlprod';
				pagename = 'acties:';
				break;
		}
		// add localhost to pagename when we are on localhost
		if (envi === 'localhost'){
			pagename = 'localhost:';
		}
		// build a nice string
		url = cleanUrl.join(':');
		// set the var wa.pagename to the correct value
		if (location.href.charAt(location.href.length -1) == "/"){
			url += "index";
		}
		
		wa.pagename = pagename+url;
	//	$('body').append('<p>'+wa.pagename)
		// do the same with wa.un
		wa.un = un;
		// set default for openclosed
		wa.openclosed = conf.openclosed;
		// send the data.
	//	alert('tracking : '+wa.pagename)
	if (typeof(scNoTrack) == "undefined" ) {
		setTimeout('wa.track()',100);
	};
	//	wa.track();
	};
	return {
		// make init accessible
		init:init
	};
}());

// call the init function.
$.getScript("http://www.essent.nl/content/system/js/s_code_config.js",essent.tracking.init);

window.onunload = function(){ 
	essent.tracking = null;
};

/*
	tested in:
	
	win xp
	
	IE 6
	IE 7
	IE 8
	firefox 3.0.16
	chrome 8.0.552.237
	opera 10.62
	
	os x
	
	chrome 8.0.552.237
	firefox 3.6.13
	safari 5.0.3 (6533.19.4)
	opera 11.0.1
	
*/
