$(document).ready(function(){
	var objDefaultCSS = {
				width: '',
				top: '',
				left: '',
				marginLeft: '',
				marginRight: '',
				opacity: '',
				display: ''
			};
			
	// Cufon
	Cufon.refresh();
	
	// Formulaire
		
	$("#Formulaire form :submit").live('click',function(){
		var contactForm = $("#Formulaire form");
		
		var options = {
			type: 'POST',
			beforeSubmit: function(){
				$("#contact-wait-message").fadeIn("fast")
			},
			success: function(data){
				$("#message-result").html($(data).filter('#flashMessage')).show('slow').delay(5000).hide('slow');
				if($(data).filter('#flashMessage').hasClass('successMessage')){
					$("#Formulaire form :submit").attr('value', 'Merci !').attr('disabled','disabled');
					$("#Formulaire form input, #Formulaire form textarea").attr('disabled','disabled');
				}
				$("#contact-wait-message").hide();
			}
		};
		
		contactForm.ajaxSubmit(options);
		return false;
	});
	
	
	
	// Nav
	$("#TLtimeline > .date > ul > li").live('hover',function(){
		$(this).children('ul.sub-nav').stop(true,true).slideDown("fast");
	});
	$("#TLtimeline > .date > ul > li").live('mouseleave',function(){
		$(this).children('ul.sub-nav').stop(true,true).delay(1000).slideUp("slow");
	});
	
	// add Tabs
	$("#contact-paginate, #about-paginate, #cv-paginate").tabs({
		load: function(event, ui){
			Cufon.refresh();
		},
    	fx: {opacity: 'toggle'},
    	cache: true
	});
	
	// Show banner if the browser does not support CSS3 animations and transitions.
	if(!Modernizr.cssanimations || !Modernizr.csstransitions){
		$("#banner").delay(1000).fadeIn('slow');
		$("#banner").click(function(){$(this).fadeOut();});
	}
	
	// Show/Hide the profil box
	if(Modernizr.opacity){
		$("#v-card img").hover(function(){
			$("#profil-box").css(objDefaultCSS).css('display', 'block');
			$("#profil-box").stop().animate({opacity: 1,top: '-=10px'});
		},function(){
			$("#profil-box").stop().animate({opacity: 0},function(){
				$(this).css(objDefaultCSS);
			});
		});
	}
	
	// On Click, hide help.
	$("#contact-paginate ul li a").click(function(){
		$("#arrow-click").fadeOut("slow");		
	});
	
	// Main animation
	$("#slider ul li").click(function(){
	
		// Hide help if not already hidden.
		$("#arrow-click").hide();
		
		/** In our case, :animated of jQuery would not work.
			That is why we need to create a class for this animation. */
		if(! $("#slider").is(".animated")){
		
			$("#slider").addClass('animated');
			if(! $(this).is(".selected")){
				var currentId = $(this).attr('class');
				$("#slider ul li.selected").removeClass('selected');
				$(this).addClass('selected');
				
				var pages = $("#pages > .page");
				pages.filter(".third-page.moveToLast").removeClass('moveToLast');
				pages.filter(".first-page.moveToFirst").removeClass('moveToFirst');
				switch ( $("#pages > .page#"+currentId).attr('class') ) {
					case 'page second-page':
						backwardPage(pages);
						break;
					case 'page third-page':
						forwardPage(pages);
						break;
					default:
				}
			}
		}
	});
	
	
	/**
	* Animate pages to place the third page in front.
	*/
	function forwardPage(pages){
		
		pages.filter(".first-page").children().filter(".content").slideUp('slow',function(){
			if(!Modernizr.csstransitions){
				pages.filter(".third-page").animate({
					top: '-=40px',
					opacity: 0
				},function(){
					$(this).switchClass('third-page', 'first-page').css({top: '-40px'}).animate({
						top: '+=40px',
						opacity: 1
					}, function(){
						$(this).css({opacity: '', top: ''});
						$(this).children().filter(".content").slideDown("slow",function(){
							$("#slider").removeClass("animated");
						});
					});
				});
			}else{
				pages.filter(".third-page")
					.switchClass('third-page', 'first-page')
					.addClass('moveToFirst');
			}
			
			pages.filter(".second-page")
				.switchClass('second-page', 'third-page');
			
			pages.filter(".first-page:not(.moveToFirst)")
				.switchClass('first-page', 'second-page');
			
			if(Modernizr.csstransitions)
				setTimeout('$("#pages > .page.first-page").children().filter(".content").slideDown("slow");	$("#slider").removeClass("animated");',1600);
		});	
	}
	
	/**
	* Animate pages to place the first page in the back.
	*/
	function backwardPage(pages){
		pages.filter(".first-page").children().filter(".content").slideUp('slow',function(){
			if(!Modernizr.csstransitions){
				pages.filter(".first-page").animate({
					top: '+=40px',
					opacity: 0
				},function(){
					$(this).switchClass('first-page', 'third-page').css({top: '-80px'}).animate({
						top: '+=40px',
						opacity: 0.4
					}, function(){
						$(this).css({opacity: '', top: ''});
						$("#pages > .page.first-page").children().filter(".content").slideDown("slow",function(){
							$("#slider").removeClass("animated");
						});
					});
				});
			}else{
				pages.filter(".first-page")
				.switchClass('first-page', 'third-page')
				.addClass('moveToLast');
			}
			
			pages.filter(".second-page")
				.switchClass('second-page', 'first-page');
				
			pages.filter(".third-page:not(.moveToLast)")
				.switchClass('third-page', 'second-page');

			if(Modernizr.csstransitions)
				setTimeout('$("#pages > .page.first-page").children().filter(".content").slideDown("slow"); $("#slider").removeClass("animated");',1200);
		});
		
	}
});
