$(document).ready(function(){

	/*
	Background disappear when clicking on a submenu link
	Supmenu clear previous text, letting background appearing
	*/

	var navColors = new Array();
	navColors.push('#bfbb11'); // green
	navColors.push('#ffc200'); // yellow
	navColors.push('#e88801'); // orange
	navColors.push('#c93c00'); // red
	
	$('nav > a').eq(0).css('background', navColors[0]);
	$('nav > a').eq(1).css('background', navColors[1]);
	$('nav > a').eq(2).css('background', navColors[1]);
	$('nav > a').eq(3).css('background', navColors[1]);
	$('nav > a').eq(4).css('background', navColors[2]);
	$('nav > a').eq(5).css('background', navColors[3]);
	
	$('div#contents div').fadeIn(2000);
	
	$('nav ul').each(function(i){
		var parentColor = $(this).prev().css('background-color');
		$(this).css({'border-bottom':'2px solid '+parentColor});
		$(this).children('li.slide').css('background-color', parentColor);
	});
 
	$('nav ul').not(':nth-child(2)').hide();
	
	$('nav ul li.slide').animate({'margin-top': '0'},800);
	$('nav ul li:nth-child(2)').addClass('active');
	
	$('nav > a').click(function(){
		$(this).next().css('height', $(this).next().height());
			
		$('nav ul li.slide').hide();
		$('nav ul li').removeClass('active');
		$(this).next().children('li').eq(1).addClass('active');
		$(this).next().children('li.slide').css('margin-top','-26px');
		// "26"px to go into a variable

		if( $(this).next().is(':hidden') ){
			$('nav > a').next().slideUp(800);
			$(this).next().slideDown(1000, 'easeOutQuint', function(){
				$(this).children('li.slide').animate({'margin-top': '0'},300);
			});
		}
		
		$(this).next().children('li.slide').show();
		
		menuColor = $(this).css('background-color');
		$('div#contents').animate({borderColor: menuColor, backgroundColor: menuColor}, 1000);
		$('header').animate({backgroundColor: menuColor}, 1000);
		
		// load first page
		$.ajax({
			type: 'POST',
			url: 'xhr.php?controller=get-page.php',
			data: 'page='+hex_md5($(this).text()+'-'+$(this).next().children('li:nth-child(2)').text()),
			dataType: 'json',
			cache: false,
			success: function(e){
				$('div#contents div').fadeOut(400, function(){
					if (e) {
						$(this).html(e.contents).fadeIn(400);
					} else {
						$(this).html('La page n\'existe pas encore.').fadeIn(400);
					}
				});
	
			}
		});
	});
	 
	 
	//submenu 
	$('nav ul li').hover(
		function(){
			var slideTo = ($(this).index()-1)*26;
			$('li.slide').stop().animate({'margin-top': slideTo+'px'}, 300, 'easeOutQuint');
		}
	);
	$('nav ul').mouseleave(function(){
		var indexActiveItem = $('nav ul li[class="active"]').index()-1;
		var slideTo = (indexActiveItem)*26;
		$('li.slide').stop().animate({'margin-top': slideTo+'px'}, 300, 'easeOutQuint');
	});
	
	$('nav ul a').click(function(){
		$('nav ul li').removeClass('active');
		$(this).parent('li').addClass('active');
		
		var slideTo = ($(this).parent('li').index()-1)*26;
		$('li.slide').stop().animate({'margin-top': slideTo+'px'}, 300, 'easeOutQuint');

		// animation for loading
		$(this).animate({'margin-left':'10px'}, 300);
		
		// pages xhr load
		$.ajax({
			type: 'POST',
			url: 'xhr.php?controller=get-page.php',
			data: 'page='+hex_md5($(this).parent().parent().prev().text()+'-'+$(this).text()),
			dataType: 'json',
			cache: false,
			success: function(e){
				$('div#contents div').fadeOut(400, function(){
					if (e) {
						$(this).html(e.contents).fadeIn(400);
						$('nav ul a').animate({'margin-left':'0px'}, 300);
					} else {
						$(this).html('La page n\'existe pas encore.').fadeIn(400);
					}
				});
	
			}
		});
	});

});
