$(document).ready(function () {	
	$('#maptree li:has("ul")').find('span:first').toggleClass('treemark'); 
	$('#maptree li span').click(function () {		
		toggleNode(this.parentNode);
	});
	postLoad();
})

function toggleNode(Node) {
	prepareLast(Node); 
	var ul=$('ul:first',Node);
	if (ul.length) {
		ul.slideToggle(200);
		var em=$('span:first',Node);
		em.toggleClass('open'); 
	}     
}

function prepareLast(Node) {
	$(Node).each(function(){
		if (!$(this).next().length) {
			$(this).find('ul:first > li').addClass('last');
		}  
	}) 
}

function postLoad(){	
	$('#maptree > ul > li > ul').each(function(){
		toggleNode(this.parentNode);
	});
}
