/* global MenuEssentialOptions */

window.MenuEssential = {}
MenuEssential.slideOptions = { duration:400, easing:'easeOutQuart' } ;

/**
 * Basic menu functionality, such as expanding/collapsing menu items, and ensuring pages open with a 
 * reduced "Location" view just showing the applicable menu items for the current page.
 */
MenuEssential.init = function() {
	
	var $menu_options = MenuEssential.slideOptions;
	
	$('#menu dl > dd > dl').hide() ;
	
	// If the user has navigated down the hierarchy, show only where we are right now
	var openLocations = $('#menu dt.current') ;
	var currentLocation = $(openLocations).last() ;
	
	// Hide all items except those that lead to our current location
	// and those that sit underneath
	$(openLocations).each( function() {
		$(this).siblings( 'dt' ).hide() ;
		$(this).filter( '.group' ).addClass( 'expanded' ) ;
		$(this).next('dd').find('> dl').show() ;
	}) ;
	
	// If the last active group is the end of the menu hierarchy
	// we show its siblings, else hide them and show its descendents 
	$(currentLocation).next( 'dd:not(:has(dl))' ).siblings( 'dt' ).show() ;
	
	// Click a DT.group: Hide every DT except any belonging to all ancestor DLs.
	// Hide every DL except the sibling DD's contained DL.
	$('#menu dt.group').on('click', function(e) {
		if (!$(this).hasClass('expanded')) {
			// Show the extra subitems and hide siblings
			// $(this).siblings('dt').slideUp( $menu_options );
			
			$(this).siblings('dt').addClass('semitransparent').removeClass('expanded') ; 
			$(this).siblings('dt').next('dd').find('dl').slideUp( $menu_options ) ;
			$(this).siblings('dt').next('dd').find('dt').removeClass('expanded').removeClass('semitransparent') ;
			
			$(this).next('dd').find('> dl').slideDown( $menu_options );
			$(this).addClass('expanded').removeClass('semitransparent');
		} else {
			// Hide subitems and show siblings
			$(this).siblings('dt').removeClass('semitransparent').slideDown( $menu_options );
			$(this).next('dd').find('dl').slideUp( $menu_options );
			$(this).next('dd').find('dt').removeClass('expanded').removeClass('semitransparent').slideDown( $menu_options );
			$(this).removeClass('expanded');
		}
	});
	
	/********************************************************/
	/** Menu functionality                                  */
	/********************************************************/	
	
	
	var menu_toggle = function(){
		var c = $( '#menu' ).hasClass( 'expanded' ) ;
		if ( c ) {
			// If the menu is open, close and rotate the logo back
			$( '#menu' ).removeClass( 'expanded' ) ;
			$( '.menu_logo' ).not( '.spin' ).removeClass( 'rotate' ) ;
			
			// Collapse the menu completely, except for 'current' items
			$('#menu dt').removeClass('semitransparent');
			$('#menu dt:not(.current)').each( function(){
				$(this).removeClass('expanded');
				$(this).next('dd').find('dl').slideUp( $menu_options ) ;
			// Menu items collapsed
			
			}) ;
		/*	$( '#content' ).removeClass( 'nudgeright' ) ; */
		} else {
			// If the menu is closed, expand and rotate logo
			$( '#menu' ).addClass( 'expanded' ) ;
			$( '.menu_logo' ).not( '.spin' ).addClass( 'rotate' ) ;
	
		/*	$( '#content' ).addClass( 'nudgeright' ) ; */
		}
	} ;
	
	$( '#menu' ).hoverIntent(
		function() {
		/*  $( '#content' ).addClass( 'slideright' ) ;   */
			$( '.menu_logo' ).not( '.spin' ).addClass( 'rotate' ) ;
			$( '#menu' ).addClass( 'expanded' ) ; 
			$( '#link_search' ).removeClass( 'active' ) ;
			$( '#search input' ).blur() ;
			$( '#search' ).removeClass( 'reveal' ) ;
		} , function() {
		/*	$( '#content' ).removeClass( 'slideright' ) ; */
		/*	$( '.menu_logo' ).delay(1000).removeClass( 'rotate' ) ; */
		/*	$( '#menu' ).delay(1000).removeClass( 'expanded' ) ;*/
		}
	);
	
	$( '#logo_menu' ).on( "click.menu", menu_toggle ) ;
	$( '#menu h2' ).on( "click.menu", menu_toggle ) ;
	
	/* If menu is expanded, clicking on anything but the menu, its items
	 * or the site logo will close it.  Clicking on site log is already
	 * managed
	 */
	$(document).click(function(e) {
		var target = e.target;
		if ( !$(target).is('#menu') 
				&& !$(target).is('#logo_menu') 
				&& !$(target).parents().is('#menu') 
				&& $( '#menu' ).hasClass( 'expanded' ) ) {
			menu_toggle() ;
		}
	});
		
	/* 
	 * If the menu is expanded and the mouse moves beyond the right-most edge of
	 * the menu, we'll collapse it
	 */
	$( 'body' ).on( 'mousemove', '#content', function() {
		if ( $( '#menu' ).hasClass( 'expanded' ) 
		     && event.pageX > $('#menu').width()
		     // Prevent menu closing while the menu-search filter button is engaged (collapsed)
		     && !$('#menu_search > a').hasClass('collapsed')) {
			
			menu_toggle() ;
		}
	}) ;
}

window.showUnloadTransition = true;
window.unloadTransition = function() {
	$( '.menu_logo' ).addClass( 'spin' ) ;
};

/**
 * Spin the logo when we're leaving this url or refreshing
 */
$(window).on('beforeunload', function(){
	if (window.showUnloadTransition) {
		window.unloadTransition();
	}
});
