// v.2
 
$(document).ready( function() {
	
	var notree = /#notree$/.test(location.href);
	if( notree ) {
		$('#splitter').addClass('hidden');
		$('#content').addClass('hidden');
		return;
	}
	
	var selection = location.href.replace(/^.*?\/\/[^\/]*/, '');
	//# na koncu adresu powoduje blad w ie
	selection = selection.replace(/#$/, '');

	//dla kazdego pierszego a w li, ktory nie posiada ul > li 
	$('ul.treemenu :not(li:has(ul > li)) > a:first-child.toggle')
		.removeClass('toggle')
		.addClass('dummy');

	//dla kazdego pierszego a w li, ktory posiada ul
	$('ul.treemenu li:has(ul) > a:first-child.toggle')
		.click(function () {
			//rozwiaj/zwijaj prznalezny li
			$(this.parentNode).find('> ul').toggle(); 
			$(this).toggleClass('expand');
		})
		/*.each(function() { //standardowo menu jest ukryte na poziomie styli, wiec kod jest nadmiarowy
			//wstepnie ukryj
			if( ! $(this).hasClass('expand') ) {
				$(this.parentNode).find('> ul').hide();
			}
		})*/;
	
	$('ul.treemenu li > .dummy + a:not(.toggle)')
		.click(function () {
			//odznacz inne
			$('ul.treemenu a.selected')
				.removeClass('selected');
			//zaznacz
			$(this).toggleClass('selected');
		})
	
	//rozwin zaznaczony element	
	var expand = function(selection) {
		var found = false;
		$('ul.treemenu li:has(a[href="'+selection+'"]) > a:first-child.toggle').each(function() {
				found = true;
				$(this.parentNode).find('> ul').show(); 
				$(this).addClass('expand');
		}); 
		return found;
	}
	
	if( !expand(selection) ) {
		//brak kotwicy, rozwin galaz z plikiem
		selection = selection.replace(/#.*$/, '');
		expand(selection);
	} 
			
	//zaznacz
	$('ul.treemenu a[href="'+selection+'"]')
		.addClass('selected');
}); 