var _hash = window.location.hash;

function displayTab(obj)
{
	var tabMenu = obj.parentNode;
	while(tabMenu.nodeType != 1 || tabMenu.tagName.toLowerCase() != "ul") {
		tabMenu = tabMenu.parentNode;
	}
	var prefix = tabMenu.id.split("_")[0];
	
	var tabs = tabMenu.getElementsByTagName("a");
	for(var i = 0; i < tabs.length; i++) {
		var tabBody = $(prefix+"_tab"+(i+1));
		if(tabs[i] == obj) {
			removeClassName(tabs[i], "hidden");
			addClassName(tabs[i], "active");
			removeClassName(tabBody, "hidden");
			addClassName(tabBody, "active");
			
			if(tabs[i].getAttribute("tabHash")) {
				window.clearInterval(_hashInterval);
				_hash = "#"+tabs[i].getAttribute("tabHash");
				window.location.hash = _hash;
				_hashInterval = window.setInterval(pollHashChange, 500);
			}
		} else {
			removeClassName(tabs[i], "active");
			addClassName(tabs[i], "hidden");
			removeClassName(tabBody, "active");
			addClassName(tabBody, "hidden");
		}
	}
}

function processTabHash() {
	var hash = window.location.hash.substring(1);
	if(hash.length) {
		var tabMenus = getElementsByClassName(document, "ul", "tabMenu");
		for(var i = 0; i < tabMenus.length; i++) {
			var tabs = tabMenus[i].getElementsByTagName("a");
			for(var j = 0; j < tabs.length; j++) {
				if(tabs[j].getAttribute("tabHash") && tabs[j].getAttribute("tabHash") == hash) {
					displayTab(tabs[j]);
					break;
				}
			}
		}
	}
}

function pollHashChange() {	
	if(_hash == window.location.hash) return;
	else {
		_hash = window.location.hash;
		processTabHash();
	}
}
var _hashInterval = window.setInterval(pollHashChange, 500);
