window.onload = function () {
	showTab(document.getElementById('default-tab'));
	setEventHandlers();
}

function setEventHandlers() {
	var tabs = getLikeElements('ul', 'class', 'tabs')[0].childNodes;
	
	for (var i = 0; i < tabs.length; i++) {
		if (tabs[i].nodeType == 1 && tabs[i].childNodes[0].nodeType == 1)
			tabs[i].childNodes[0].onclick = linkHandler;
	}
}


function linkHandler(e) {	
	showTab(this);
	return false;
}
	

function showTab(target) {
	var node = document.getElementById(target.getAttribute('tab'));

	if (node == null) {
		return false;
	}
	
	hideAllTabs();
	
	node.style.display = 'block';
	
	setTabLink(target);
}
	

function hideAllTabs() {
	var tabs = getLikeElements('div', 'class', 'tab')

	for (var i = 0; i < tabs.length; i++) {
		tabs[i].style.display = 'none';
	}
}



function setTabLink(link) {
	var li   = link.parentNode;
	var tabs = li.parentNode.childNodes;
		
	for (var i = 0; i < tabs.length; i++) {
		if (tabs[i].nodeType == 1)
			tabs[i].className = '';
	}

	li.className = 'current';
}
		

function getLikeElements(tagName, attrName, attrValue) {
	var startSet;
	var endSet = new Array();

	if (tagName) {
		startSet = document.getElementsByTagName(tagName);    
	} else {
		startSet = (document.all) ? document.all : document.getElementsByTagName("*");
	}
	
	if (attrName) {
		for (var i = 0; i < startSet.length; i++) {
			if (attrName == 'class') {
				if (attrValue && startSet[i].className == attrValue) {
					endSet[endSet.length] = startSet[i];
				}
			} else if (startSet[i].getAttribute(attrName)) {
				if (attrValue) {
					if (startSet[i].getAttribute(attrName) == attrValue) {
						endSet[endSet.length] = startSet[i];
					}
				} else {
					endSet[endSet.length] = startSet[i];
				}
			}
		}
	} else {
		endSet = startSet;
	}

	return endSet;
}

