var subHeight = 0;
var currentSub  = '';
var currentSubHeight = 0;
var currentHash = '';
var currentButton = '';

var currentSelection = '';
var currentMenu = '';
var currentPage = '';
var currentFile = '';

function update( e )
{
	if( location.hash )
	{
		currentSelection = location.hash;
		currentSelection = currentSelection.slice( 1 );
		
		var c = currentSelection.split( "/", 4 );
		
		currentHash = c.join( "." );
		currentSelection = c[1];
		currentMenu = c[2];
		currentPage = c[3];
		
		setTimeout( loadMenu, 30 );
		setTimeout( loadContent, 40 );
	}
	else
	{
		currentSelection = currentHash = "home";
		setTimeout( loadMenu, 10 );
		setTimeout( loadContent, 20 );
	}
}

function loadMenu()
{
	if( currentSub != currentSelection )
	{
		if( currentButton )
		{
			currentButton.find('a').removeClass( 'selected' );
		}
		
		// set top-level menu
		currentButton = $( '#nav-' + currentSelection );
		currentButton.find('a').addClass( 'selected' );
		
		if( currentSelection )
		{
			$( '#sub-menu' ).unload().load( 'xhr_submenu.php?p=' + currentSelection );
		}
	}
}

function loadContent()
{
	if( currentHash )
	{
		$( 'body' ).addClass( 'loading' );
		
		if( currentSelection == 'onderwijs' && ! currentMenu )
		{
			$( '#main-content' ).unload().load( 'landing/n2m/index.html', function(){ $( 'body' ).removeClass('loading'); } );
		}
		else
		{
			$( '#main-content' ).unload().load( 'xhr_content.php?p=' + currentHash, function(){ $( 'body' ).removeClass('loading'); } );
		}
		_gaq.push(['_trackPageview', 'XHR/'+currentHash]);
	}
}

function openSubmenu( h, u )
{
	subHeight = h;
	
	if( u != currentSub )
	{
		$( '#sub-menu' ).css( 'opacity', '1' );
		$( '#sub-menu' ).css( 'height', h+'px' );
		$( '#sub-menu' ).unload().load( 'xhr_submenu.php?p=' + currentSelection );
		
		currentSub = u;
		currentSubHeight = h;
	}
}

function closeSubmenu()
{
	$( '#sub-menu' ).css( 'height', '0px' );
	$( '#sub-menu' ).css( 'opacity', '0' );
	currentSub = '';
}

function init()
{
	$( window ).bind( 'hashchange', update );
	window.scrollTo( 0, 1 );
	update();
}

$( function()
{
	$( window ).hashchange( function(){} );
	$( window ).hashchange();
} );

$( document ).ready( init );

