if (document.getElementById && document.getElementsByTagName && document.createElement) {
	document.write('<link rel="stylesheet" type="text/css" href="js_hideLinks_ex3c.css" />');
	window.onload = initToggleCategories;
}

function initToggleCategories() {
	document.getElementById('contentPool').style.display = 'none';
	var as = document.getElementById('toggle').getElementsByTagName('a');
	for (var i = 0; i < as.length; i++) {
		as[i].onclick = function() {
			toggle(this);
			return false;
		}
	}
	// Activate the first tab by default
	toggle(as[0]);
}

function toggle(l) {
	// Try to get a reference to an element with the id 'current' (a previously active tab header)
 	var oldCurrent = document.getElementById('current');
	// If this element exists, remove its ID attribute
	if (oldCurrent) oldCurrent.removeAttribute('id');
	// Add the ID attribute with value 'current' to the newly active tab header (LI element)
	l.parentNode.setAttribute('id', 'current');
	var oldCloneUL = document.getElementById('cloneUL');
	if (oldCloneUL) document.body.removeChild(oldCloneUL);
	var cloneUL = document.createElement('ul');
	cloneUL.setAttribute('id', 'cloneUL');
	document.body.appendChild(cloneUL);
	var lis = document.getElementById('contentPool').getElementsByTagName('li');
	var cat_class = l.getAttribute('href').match(/#(\w.+)/)[1];
	// Create helper array for sorting elements on alphabetical order 
	var tmpSort = new Array();
	for (var i = 0; i < lis.length; i++) {
		if (lis[i].className.indexOf(cat_class) != -1) {
			var clone = lis[i].cloneNode(true);
			// Get value of the link element
			var aValue = clone.firstChild.firstChild.nodeValue;
			// Add this value to the helper Array; Note that we cannot use Array.push(), because older browsers like IE5 don't support it
			tmpSort[i] = aValue; 
			// Sort the helper Array on alphabetical order
			tmpSort.sort();
			// Check what the new position of the element should be
			for (var j = 0; j < tmpSort.length; j++) {
				if (tmpSort[j] == aValue) {
					// If the new LI element should be appended at a new position, append it
					if (typeof cloneUL.childNodes[j] == 'undefined') cloneUL.appendChild(clone);
					// Otherwise insert the new LI element at alphabetical order
					else cloneUL.insertBefore(clone, cloneUL.childNodes[j]);
				}	
			}
		}
	}
}
			