//Emulate the CSS :target Pseudo-Class in IE Copyright 2005 Mark Wubben <http://novemberborn.net/colophon>This software is licensed under the CC-GNU LGPL <http://creativecommons.org/licenses/LGPL/2.1/>


// Simple normalization function to clean extra white-space.
String.prototype.normalize = function(){
	return this.replace(/\s+/g, " ");
};

//The hack: this function gets executed on load. It'll set `.target` on the element who's ID matches the fragment identifier of the URI.It then monitors this identifier every 200 ms for changes. Originally I wanted to use the propagation of the `focus` event so I could check it every time an element got focus, but unfortunately this is not supported in IE.

function emulateCSSPseudoClassTarget(){
	var sCurrentTargetID = getFragmentID();
	
	function setClass(sID){
		var node = document.getElementById(sID);
		if(node == null){
			return;
		};
		var sClassName = node.className;
		if(sClassName == null){
			sClassName = "target";
		} else {
			sClassName = sClassName.normalize() + (sClassName == "" ? "" : " ") + "target";
		};
		node.className = sClassName;
	};
	
	function removeClass(sID){
		var node = document.getElementById(sID);
		if(node == null){
			return;
		};
		node.className = node.className.replace(/\btarget\b/, "");
	};
	
	function getFragmentID(){
		return document.location.hash.replace(/^#(.*)$/, "$1");
	};
	
	function monitor(){
		var sNewTargetID = getFragmentID();
		if(sNewTargetID != sCurrentTargetID){
			removeClass(sCurrentTargetID);
			setClass(sNewTargetID);
			sCurrentTargetID = sNewTargetID;
		};
	};
	
	setClass(sCurrentTargetID);
	
	// Your brain (well, the conscious part) won't notice a change if it happens inside a time-frame of 200 ms. Therefore this is a safe interval.
	setInterval(monitor, 150);
};

