var G = {
	nav: null,
	currentNode: null,
	doc: null
}

$( document ).ready( function() {
	G.nav = $( "#nav" );
	$( G.nav ).find( "li" ).click( setCurrent );
	if ( window.location.hash ) {
		var loc = unescape( window.location.hash.substr( 1 ) );
		$( "#doc" )[0].src = loc;
		setCurrent( $( "a[ href=\"" + loc + "\"]" ).parent()[0] );
	}
	setCurrent();
} );


function showNode( n ) {
	$( n ).show().addClass( "hilite" );
	$( n ).parents( "#toc li, #toc ul" ).show().addClass( "hilite" );
	$( n ).siblings().show();
	$( n ).children( "ul" ).show().addClass( "hilite" ).children( "li" ).show().addClass( "hilite" );
	showTopLevelNodes();
}

function hideNode( n ) {
	$( n ).find( "ul, li" ).hide().removeClass( "hilite" );
	$( n ).siblings().hide();
	$( n ).parents( "#toc li, #toc ul" ).hide().removeClass( "hilite" );
	$( n ).hide().removeClass( "hilite" );
	showTopLevelNodes();
}

function showTopLevelNodes() {
	$( "#toc" ).children( "li" ).show();
}

function setCurrent( param ) {
	var el, event;
	if ( param && typeof param.stopPropagation != "undefined" ) {
		event = param;
		event.stopPropagation();
		var doc = $( this ).find( "a" ).attr( "href" );
		window.location.hash = "#" + doc;
	}
	if ( typeof this.nodeName != "undefined" ) {
		el = this;
	} else if ( param && typeof param.nodeName != "undefined" ) {
		el = param;
	} else {
		el = $( ".current" );
		if ( el.length ) {
			el = el.first();
		} else {
			el = $( "#toc" ).children( "li" ).first();
		}
	}
	if ( G.currentNode ) {
		$( G.currentNode ).removeClass( "current" );
		hideNode( G.currentNode );
	}
	$( el ).addClass( "current" );
	showNode( el );
	G.currentNode = el;
}

function updateLoc( url ) {
	alert( url );
}