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 al = document.getElementById('toggle').getElementsByTagName('ul');
	for (var i = 0; i < al.length; i++) {
		al[i].style.display = 'none';
	}
	var as = document.getElementById('toggle').getElementsByTagName('a');
	for (var i = 0; i < as.length; i++) {
		as[i].onmouseover = function() {
			//Reset all main menu's to standard style
			
			//Lock Hoverstate on selcted item
			
			//Display Sub menu's
			toggleme(this);
			return false;
		}
		if (document.getElementById('current') == as[i] ){
			toggleme(as[i]);
		}
	}
	// Activate the first tab by default
	//toggleme(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]);
				}	
			}
		}
	}
}
			
			
function toggleme(l) {
	//alert('here');
	// 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.getElementById('submenu').removeChild(oldCloneUL);
	var cloneUL = document.createElement('ul');
	cloneUL.setAttribute('id', 'cloneUL');
//	document.body.appendChild(cloneUL);
	document.getElementById('submenu').appendChild(cloneUL);
	var lis = getElementsByClassName(document.getElementById("toggle"), "li", "main");

	//alert(lis.length);
	for (var i = 0; i < lis.length; i++) {
		lis[i].firstChild.className = '';
	}	
	l.className = 'selected';
	var lis = document.getElementById(l.getAttribute('rel').match(/#(\w.+)/)[1]).getElementsByTagName('li');
	var cat_class = l.getAttribute('rel').match(/#(\w.+)/)[1];
	// Create helper array for sorting elements on alphabetical order 
	var tmpSort = new Array();
	//alert(lis.length);
	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; 
			//alert(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]);
					//alert('test')
				}	
			}
		//}
	}
}
		
		
		
/*
    Written by Jonathan Snook, http://www.snook.ca/jonathan
    Add-ons by Robert Nyman, http://www.robertnyman.com
*/

function getElementsByClassName(oElm, strTagName, strClassName){
    var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }   
    }
    return (arrReturnElements)
}
