// JavaScript Document

var navHeight = 0;

$( function () {
	var submenuClosed = true;
	var menuTimeout = setTimeout ('',0);
	var scrollTimeout = setTimeout ('',0);
	var openCompleted = false;
	var navHover = false;
	var linkHover = false;
	
	navHeight = $('.nav').outerHeight(true); // hauteur de la navigation
	
	var menuPos = $('.nav').offset().top;
	var submenuTop = $('.nav-deroulant').offset().top;
	var submenuHeight = $('.nav-deroulant').innerHeight();
	
	// initialisation du Menu
	var initializeMenu = function () {
		$('.nav-contenu li.hover').removeClass('hover');
		$('.nav-deroulant').find('.show-submenu').slideUp(300, function () {
			$(this).removeClass('show-submenu');
		});
		
		submenuClosed = true;
		openCompleted = false;
	};
	
	// Ouverture des sous menus
	var openSubmenu = function (menuIndexof) {
		$('.nav-contenu li').eq(menuIndexof).addClass('hover');
		$('.nav-deroulant > div').eq(menuIndexof).slideDown( 300, function () { 
			$(this).addClass('show-submenu'); 
			submenuHeight = $('.nav-deroulant').innerHeight();
			scrollWindow ();		
			openCompleted = true;					
		});				
		
		submenuClosed = false;
	};
	
	// Window scrolling
	var scrollWindow = function () {
		if ( $(window).scrollTop() < menuPos ) {

			var submenuTotalHeight = submenuTop + submenuHeight + 10;
			var windowHeightScroll = $(window).scrollTop() + $(window).height();
			
			//console.log ( $(window).scrollTop(), windowHeightScroll );
			
			if ( submenuTotalHeight > windowHeightScroll ) {
				$.scrollTo( $(window).scrollTop() + (submenuTotalHeight - windowHeightScroll), 500);
				//$(window).scrollTop( $(window).scrollTop() + (submenuTotalHeight - windowHeightScroll) );
			}
		}
	}
	
	// Menu Roll over
	$('.nav-contenu li').each ( function (indexMenu) {
		$('a', this).hover ( function () {
			clearTimeout (menuTimeout);
			clearTimeout (scrollTimeout);
			
			var $menuLink = $(this);
			
			$('.nav-contenu li.hover').removeClass('hover');
			$('.nav-deroulant > div').stop(true, true).hide();			
									
			if ( submenuClosed ) {
				menuTimeout = setTimeout ( function () {
					openSubmenu (indexMenu);
				}, 1000 );
				
			} else {
				$(this).parent('li').addClass('hover');
				$('.nav-deroulant').find('.show-submenu').hide();
				$('.nav-deroulant').find('.show-submenu').removeClass('show-submenu');
				$('.nav-deroulant > div').eq(indexMenu).show();
				$('.nav-deroulant > div').eq(indexMenu).addClass('show-submenu');
				
				submenuHeight = $('.nav-deroulant').innerHeight();
				scrollWindow ();
			}
		}, function () {
			if ( !submenuClosed ) {
				menuTimeout = setTimeout ( function () {
					initializeMenu ();
				},1500);
			} else {
				$(this).parent('li').removeClass('hover')
				clearTimeout (menuTimeout);
			}
		});
		
		$('a', this).click ( function () {
			/*if ( $(this).parent('li').is('.hover') && openCompleted ) {
				clearTimeout (menuTimeout);
				initializeMenu ();
			} else if ( $(this).parent('li').is('.hover') && !openCompleted ) {
				clearTimeout (menuTimeout);
				openSubmenu (indexMenu);
			}*/
			
			if (openCompleted ) {
				clearTimeout (menuTimeout);
				initializeMenu ();
			} else {
				clearTimeout (menuTimeout);
				openSubmenu (indexMenu);
			}
			
			return false;
		});
		
	});
	
	$('.nav-deroulant').hover ( function () {
		clearTimeout (menuTimeout);
	}, function () {
		menuTimeout = setTimeout ( function () {
			initializeMenu ();
		},2000);
	});
	
	$(window).scroll(function () {
		//console.log ($(window).scrollTop(), menuPos);
		
		//initializeMenu ();
		
		if ( $(window).scrollTop() >= menuPos ) {
			$('.nav-outter').addClass('nav-fixed');
			$('.logoscroll').fadeIn('fast');
		} else {
			$('.nav-outter').removeClass('nav-fixed');
			$('.logoscroll').fadeOut('fast');
		}
	});
	
	// menu emploi
	$('.emploi-tab').find('>li').each ( function (index) {
		$(this).find('a').click ( function () {
			if ( !$(this).parent('li').is('.active') ) {
				 
				$('.emploi-tab').find('>li.active').removeClass();
				$(this).parent('li').addClass('active');
				
				$('.layer-menu-content').find('.layer-item-show').removeClass('layer-item-show');
				$('.layer-item').eq(index).addClass('layer-item-show');
			}
			
			return false;
		});
	});
	
	$('.liste-news > li').each ( function () {
		$(this).hover( function () {
			$(this).addClass('hover');
		}, function () {
			$(this).removeClass('hover');
		});
	});
	
	$('.nav').hover ( function () {
		navHover = true;
	}, function () {
		navHover= false;
	});
	
	$(document).click ( function () {
		if ( !navHover ) {
			initializeMenu ();
		}
	});
});

