$(document).ready( function(){
	
	// ajoute une classe aux derniers éléments du sous menu
	$(".nav li li:last-child").addClass("lastChild");
	
	// affiche le contenu a travers un fondu
	$(".content").hide().fadeIn(750);
	
	// page collectif - permet un nouveau layout [img]<->[p]
	$(".collectif .articleDetail .alignleft").parent().parent().css({
		padding: "0 80px 20px 500px",
		position: "relative"
	});
	
	
	// Menu animation
	$(".nav ul ul li").css("opacity","0");
	$(".nav ul ul").css("display","none");
	
	$(".nav ul li").hover(function() {
		$(this).find("ul").css("display","block");
		$(this).find("li").animate({opacity: "1"}, 1000);
	} , function() {
	// permettre le verou par click
		if( $(this).attr("id") != "clicked" ) {
			$(this).find("li").stop().animate({opacity: "0"}, 500);
			$(this).find("ul").css("display","none");
		}
	});
	
	$(".nav li a").click( function(){
		if( $(this).parent().attr("id") != "clicked" ) {
			$(this).parent().attr("id", "clicked");
		} else {
			$(this).parent().attr("id", "");
		}
	});
	
	$("nav ul ul li a").hover(function() {
		$(this).parent().parent().parent().addClass("hover");
	}, function() {
		$(this).parent().parent().parent().removeClass("hover");
	});
	
	
	// Articles dans Works et Collectif
	$('.articleDetail').hide();
	$('.articleDetail:nth(0)').show().addClass('active');
	$('.articleList li:nth(0)').addClass('hover');
	
	$('.thumbnailLink').click( function(event){
		event.preventDefault();
		
		var id = $(this).attr('title');
		//afficher l'actif
		//var actived = $('#'+id).attr('style');
		
		if( $('#'+id).css('display') == 'none' ){
			$('.articleDetail').hide().removeClass('active');
			$('#'+id).addClass('active');
			verticalAlign();
			$('#'+id).show("slide", { direction: "right" }, 1000);
		}
		
		//effet hover souligné
		var hover = $(this).parent().attr('class');		
		if(hover != 'hover'){
			$('.thumbnailLink').parent().removeClass('hover');
			$(this).parent().addClass('hover');
		}
		
	});
	
	
	// vérifier si la page n'est pas une sous page de Works -> laisser le menu ouvert
	if ($(".content").hasClass("works") == true) {
		$(".nav a").each( function(){
			
			var works = $(this).attr("title");
			
			if (works == "Works") {
				$(this).trigger('click');
				$(this).parent().children().css("display","block");
				$(this).parent().children().children().animate({opacity: "1"}, 1000);
			}
			
		});
	}
	
	// end click articles

	// next
	$('.index .nextPost').click( function(event){
		
	 	if ( !$('.articleDetail').is(':animated') ) {
			event.preventDefault();
			var current = $('.active').attr('id');
			var next = $('#'+current).next().attr('id');
			var slider = $('.thumbnailLink').attr('title');
			var li = $('.thumbnailLink[title|="'+current+'"]').parent().position().left;
			
			if(next != undefined){
				$('.thumbnailLink').parent().removeClass('hover');
				$('.thumbnailLink[title|="'+current+'"]').parent().next().addClass('hover');
				
				$('.active').hide().removeClass('active');
				$('#'+current).next().addClass('active');
				verticalAlign();
				$('#'+current).next().show("slide", { direction: "right" }, 1000);
				
				if(li > 720 && li < 820){
					$('.nextPostSlider, .prevPostSlider').trigger('click');
				}
				
			} else {
				$('.thumbnailLink').parent().removeClass('hover');
				$('.articleList li:nth(0) a').parent().addClass('hover');
				
				$('.active').hide().removeClass('active');
				
				$('.articleDetail:nth(0)').addClass('active');
				verticalAlign();
				$('.articleDetail:nth(0)').show("slide", { direction: "right" }, 1000);
				
				$('.nextPostSlider, .prevPostSlider').trigger('click');
			}
		}
	});
	//prevent doubleclick
	$('.index .nextPost').dblclick();
	//end next
	
	
	
	// prev
	$('.index .prevPost').click( function(event){
	 	if ( !$('.articleDetail').is(':animated') ) {	 		
			event.preventDefault();
			var current = $('.active').attr('id');
			var prev = $('#'+current).prev().attr('id');
			var slider = $('.thumbnailLink').attr('title');
			var li = $('.thumbnailLink[title|="'+current+'"]').parent().position().left;
			
			if(prev != undefined){
				$('.thumbnailLink').parent().removeClass('hover');
				$('.thumbnailLink[title|="'+current+'"]').parent().prev().addClass('hover');
				
				$('.active').hide().removeClass('active');
				
				$('#'+current).prev().addClass('active');
				verticalAlign();
				$('#'+current).prev().show("slide", { direction: "left" }, 1000);
				
								
				if(li == 825){
					$('.nextPostSlider, .prevPostSlider').trigger('click');
				}
								
			} else {
				$('.thumbnailLink').parent().removeClass('hover');
				$('.articleList li:last a').parent().addClass('hover');
				
				$('.active').hide().removeClass('active');
				
				$('.articleDetail:last').addClass('active');
				verticalAlign();
				$('.articleDetail:last').show("slide", { direction: "left" }, 1000);
				
				$('.nextPostSlider, .prevPostSlider').trigger('click');
			}
		}
	});
	//end prev
	
	//IPAD SWIPE
	$(".articleDetail p, .articleDetail .wp-caption, .articleDetail h1, .articleDetail h2, .articleDetail h3, .articleDetail h4, .articleDetail h5, .articleDetail h6").touchwipe({
		
		wipeLeft: function() {
			$('.index .nextPost').trigger('click');
		},
		
		wipeRight: function() {
			$('.index .prevPost').trigger('click');
		},
		min_move_x: 50
	});
	
	

	
	// slider next
	var articleListLenght = $('.articleList').width();
	
	// masquer les flèches si moins d'une ligne d'articles thumbs
	if(articleListLenght < 800){
		$('.nextPostSlider, .prevPostSlider').hide();
		$('.articleList').css('padding','0');
	
	} else {
	
		$('.nextPostSlider, .prevPostSlider').click( function(event){
			event.preventDefault();
			
			var articleListLenght = $('.articleList').width();
			var leftSlide = 0;
			
			if(articleListLenght < 829){
				leftSlide = leftSlide - 790;
				$('.articleList').animate({
					left: leftSlide
				}, 500, function(){
					$(this).stop();
				});
				
			} else {
				leftSlide = 0;
				$('.articleList').animate({
				    left: '0'
				}, 500, function(){
				    $(this).stop();
				});
			}
		});
	}
	
	// centrer verticalement
	function verticalAlign(){
		$(".articleDetail.active:not(:first)").each( function(){
			var articleHeight = $(this).height();
			var imageHeight = $(this).find("img","iframe").height();
			
			var myPaddingTop = (articleHeight - imageHeight) / 2;
			
			if( myPaddingTop < 260 ) {
				$(this).css({
					paddingTop: myPaddingTop,
					paddingBottom: myPaddingTop,
					height: 520-(myPaddingTop*2) 
				});
			}
		});
	}
	
	
	
});

