function getDiv(li) {
	return li.select('div')[0];
}

function alone(li) {
	return li.siblings().length == 0;
}

function notAlone(li) {
	return li.siblings().length > 0;
}

function addClasses()
{
	$(document.body).addClassName('hasJS');

	$$('#contain > ul').invoke('addClassName', 'sitemap');

	// root
	$$('.sitemap > li:first-child > div:first-child').invoke('addClassName', 'root');

	// sections
	$$('.sitemap li').findAll(
		function(li) {
			return li.select('li').length > 0
		}).map(getDiv).invoke('addClassName', 'section');
	
	// first
	$$('ul > li:first-child').findAll(notAlone).map(getDiv).invoke('addClassName', 'first');

	// last
	$$('ul > li:last-child').findAll(notAlone).map(getDiv).invoke('addClassName', 'last');

	// solo
	$$('ul').findAll(
		function(ul) {
			return ul.childElements().findAll(function(chld) { return chld.tagName == 'LI' }).length == 1
		}).invoke('addClassName', 'solo');

	return;
}

function getDepth(node)
{
	var children = node.childElements();
	if (children.length == 0) {
		return 1;
	} else {
		return 1 + children.max(getDepth);
	}
}

function hugeSiteMap()
{
	var maps = $$('.sitemap');

	var maxWidth = $$('.sitemap li:last-child').max(function(li) { return  li.cumulativeOffset().left + li.getWidth() });
	var emWidth = (Math.floor(maxWidth / 10) + 3) + 'em';

	maps.each(function(ul) {
		var emHeight = (getDepth(ul) * 3) + 'em';
		ul.setStyle( { width: emWidth, height: emHeight } );
	});


	$('contain').setStyle({width: 'auto'});
}


Event.observe(document, 'dom:loaded', addClasses);
Event.observe(document, 'dom:loaded', hugeSiteMap);