// geleend van http://test.newplasticarts.co.uk/dom-js/flag-offsite-links/ maar zwaar aangepast door mij.
var functionJS = {
	
// Function: Initialise
	init: function() {
		if (document.getElementById('toggler')) functionJS.makeToggler();
		functionJS.checkCookie();
		},

// Function:	Add the toggler to the page.
	makeToggler: function() {
		var togglerParent = document.getElementById('toggler');
		var labelObj = document.createElement('label');
		var h2Obj = document.createElement('h2');
		var h2Text = document.createTextNode('Service :-)');
		var checkboxObj = document.createElement('input');
			checkboxObj.type = 'checkbox';
			checkboxObj.id = 'targetSwitcher';
			checkboxObj.onclick = function() { functionJS.checkTargets() }
		var newText = document.createTextNode('Alle links in een nieuw venster openen');
		//labelObj.appendChild(checkboxObj);
		labelObj.appendChild(newText);
		labelObj.setAttribute("for","targetSwitcher");
		labelObj.setAttribute("htmlFor","targetSwitcher"); // voor IE
		h2Obj.appendChild(h2Text);
		(togglerParent.childNodes.length > 0) ? togglerParent.insertBefore(labelObj,togglerParent.childNodes[0]) : togglerParent.appendChild(labelObj);
		togglerParent.insertBefore(checkboxObj,togglerParent.childNodes[0]);
		togglerParent.insertBefore(h2Obj,togglerParent.childNodes[0]);
		},

// Function:	Check Cookie
	checkCookie: function() {
		(functionJS.readCookie(window.location.hostname+'-offsiteLinks') == 'yes') ? functionJS.toggleExt(): functionJS.toggleInt();
		if (document.getElementById('toggler')) {
			document.getElementById('targetSwitcher').checked = (functionJS.readCookie(window.location.hostname+'-offsiteLinks'));
		}
		},

// Function:	Toggler function for checkbox
	checkTargets: function() {
		(document.getElementById('targetSwitcher').checked == true) ? functionJS.toggleExt() : functionJS.toggleInt() ;
		},

// Function:	Toggle new windows on
	toggleExt: function() {
		functionJS.createCookie(window.location.hostname+'-offsiteLinks','yes',365);
		var anchorEls = document.getElementsByTagName('a');
		for (var i = 0, anchorEl; anchorEl = anchorEls[i];i++) {
			if (anchorEl.rel == "external") {
				anchorEl.target = '_blank';
			}
		}
		},

// Function:	Toggle new windows off
	toggleInt: function() {
		functionJS.eraseCookie(window.location.hostname+'-offsiteLinks');
		var anchorEls = document.getElementsByTagName('a');
		for (var i = 0, anchorEl; anchorEl = anchorEls[i]; i++) {
			if (anchorEl.target == '_blank') {
				anchorEl.removeAttribute("target");
			}
		}
		},

// Function:	Create New Windows Cookie
	createCookie: function(name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime() + (days*24*60*60*1000));
			var expires = '; expires=' + date.toGMTString();
		}
		else var expires = '';
		document.cookie = name + '=' + value + expires + '; path=/';
		},

// Function:	Read New Windows Cookie
	readCookie: function(name) {
		var nameEQ = name + '=';
		var ca = document.cookie.split(';');
		for(var i=0, c; c = ca[i];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:	Delete New Windows Cookie
	eraseCookie: function(name) {
		functionJS.createCookie(name,'',-1);
		}
}

function venstertjes() {
	functionJS.init();
}

//addLoadEvent(venstertjes);
addEvent(window,'load',venstertjes);
