// all tab navigation is done with pure css
// the only thing we do here is format the current link
// we can not do this is php, as the fragment ("#target") is not passed to the server

function clearCurrents() {
	var tabs = document.getElementById("tabs");
	for (var i=0; i<tabs.childNodes.length; i++) {
		var listNode = tabs.childNodes[i];
		if (listNode.nodeName=="LI") {
			var rep=listNode.className.match(' current')?' current':'current';
			listNode.className=listNode.className.replace(rep,'');
		}
	}
}
function markCurrentOnClick() {
	if(!document.getElementById("tabs")) return false;
	var tabs = document.getElementById("tabs");
	for (var i=0; i<tabs.childNodes.length; i++) {
		var listNode = tabs.childNodes[i];
		if (listNode.nodeName=="LI") {
			for (var j=0; j<listNode.childNodes.length; j++) {
				var linkNode = listNode.childNodes[j];
				if (linkNode.nodeName=="A") {
					listNode.onclick = function() {
						clearCurrents();
						if(!this.className) this.className='current';
						else this.className=this.className+' current';
					}
				}
			}
		}
	}
}
function currentTab() {
	if(!document.getElementById("tabs")) return false;
	var tabs = document.getElementById("tabs");
	for (var i=0; i<tabs.childNodes.length; i++) {
		var listNode = tabs.childNodes[i];
		if (listNode.nodeName=="LI") {
			// set first list item to current if there is no URL target
			if(!document.location.hash) {
				if(!listNode.className) listNode.className='current';
				else listNode.className=listNode.className+' current';
				return true;
			}
			for (var j=0; j<listNode.childNodes.length; j++) {
				var linkNode = listNode.childNodes[j];
				if (linkNode.nodeName=="A") {
					if (linkNode.hash==document.location.hash) {
						if(!listNode.className) listNode.className='current';
						else listNode.className=listNode.className+' current';
						return true;
					}
				}
			}
		}
	}
	return false;
}

// Simon Willison addLoadEvent code (http://simonwillison.net/2004/May/26/addLoadEvent/)
function addLoadEvent(func) {
	var oldonload = window.onload;
	if(typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if(oldonload) {
				oldonload();
			}
			func();
	    }
	}
}
addLoadEvent(markCurrentOnClick);
addLoadEvent(currentTab);
