var containers = Array();

$(document).ready(function() {
	var container = null;
	var level;
	
	var f = function(i) {
		if (this.tagName == ('H' + (level - 2)) || this.tagName == ('H' + (level - 1))) {
			container = null;
		} else if (this.tagName == ('H' + level)) {
			container = this.id;
			containers.push(this.id);
			$(this).css('cursor', 'pointer');
			$(this).click(function() {document.location.hash = this.id; update();});
		} else if (container != null) {
//			$(this).addClass('heldby_' + container);
			this.className = 'heldby_' + container;
		}
	};
	
	level = 3; $('#content').children().each(f);
	level = 4; $('#content').children().each(f);
	
	update();
	setInterval('update()', 300);
});

function collapseAll() {
	for (var c in containers) {
		$('.heldby_' + containers[c]).css('display', 'none');
	}
}

var currentHash = null;

function update() {
	if (currentHash == document.location.hash)
		return true;
	
	currentHash = document.location.hash;
	//if (!currentHash)
	//	currentHash = '#';
	collapseAll();
	var splitted = currentHash.substring(1).split('_');
	var cc = '.heldby';
	for (var c in splitted) {
		$(cc = cc + '_' + splitted[c]).css('display', '');
	}
	if (currentHash)
		document.location.hash = currentHash;
	return true;
}
