var timerSlideUp = 1500;
var timerSlideDown = 1500;
var timerScroll = 1500;

var timerFadeOut = 1000;
var timerFadeIn = 2000;
var isOneItemOpen = false;

$(function(){
	initPage();
	
	/********** SOCIAL HOVER ***********/
	$('#social-link li span').each(function(i, el){
		$(el).clone().addClass('hover').appendTo($(el).parent());
	});
	$('#social-link li a').hover(function(){
		$(this).find('span:not(.hover)').stop(true, true).animate({marginTop:-22}, 300, 'easeOutQuint');
	}, function(){
		$(this).find('span:not(.hover)').stop(true, true).animate({marginTop:0}, 300, 'easeOutQuint');
	});
	
	/********** ITEM HOVER ***********/
		$('.item-section').find('h2').each(function(i, el){
			$(el).find('a').append('<span>');
			$(el).find('span').css({opacity:0});
			
			var $elParent = $(el).parent();
			var currentColors
			switch ($elParent.attr('id')) {
				 case 'hello':
				 currentColors = '#00a8b5';
				 break;
				 case 'agency':
				 currentColors = '#388c3b';
				 break;
				 case 'book':
				 currentColors = '#292728';
				 break;
				 case 'client':
				 currentColors = '#a23129';
				 break;
				 case 'blog':
				 currentColors = '#011d29';
				 break;
				 case 'contact':
				 currentColors = '#dd7b34';
				 break;
				 default: 
				 currentColors = '#000000';
				 break;
			}
			$(el).hover(function(){
			    $(this).stop().animate({
			        backgroundColor: "#ffffff"
			    }, 500 );
				$(this).find('span').stop().animate({
			        opacity:1
			    }, 500 );
			}, function(){
			    $(this).stop().animate({
			        backgroundColor: currentColors
			    }, 400 );
				$(this).find('span').stop().animate({
				     opacity:0
				}, 400 );					
			});
		});

});

//Init Page
function initPage(){
	var ajaxContent = '<div class="ajax-content"></div>'
	$('.content-item').css({display:'none'});
	$('.ajax-content').css({display:'none'});
	$('#content-section').find('.current-item').find('.content-item').css({display:'block'});
	$('#content-section').find('.current-item').find('.ajax-content').css({display:'block'});
}

$("#header h1").live("click", function(e){
			$('.current-item .ajax-content').slideUp(timerSlideUp, 'easeOutQuint', function(){
				$(this).find('.content-item').remove();	
				$('#content-section').addClass('enter');
				$('.item-section').removeClass('current-item');	
			});	
		e.preventDefault();
});

//ANIM AU CLICK
$("#content-section").on("click", ".item-section h2 a", function(event){
	var myUrl = $(this).attr('href');
	var myItem = $(this).parent().parent();
	var myIdItem = myItem.attr('id');
	var myContentItem = myItem.find('.content-item');
	var myAjaxItem = myItem.find('.ajax-content');
	
	if(myItem.is('.current-item') && isOneItemOpen){
		//return false;
		$('.current-item .ajax-content').slideUp(timerSlideUp, 'easeOutQuint', function(){
			isOneItemOpen = false;
		});
		//$(this).find('.content-item').remove();
		//$('.item-section').removeClass('current-item');
		
	}
	else{
		isOneItemOpen = true;
		if($("#content-section").is('.enter')){
			loadContent();
			myItem.addClass('current-item');
			$("#content-section").removeClass('enter');
		}
		else{
			$('.current-item .ajax-content').slideUp(timerSlideUp, 'easeOutQuint', function(){
				$(this).find('.content-item').remove();
				$('.item-section').removeClass('current-item');
				myItem.addClass('current-item');
				loadContent();		
			});	
			$('.item-section').removeClass('current-item');
			myItem.addClass('current-item');			
		}
	}
	function loadContent(){
		myAjaxItem.load(myUrl+' #'+myIdItem+' .content-item', function(e){
			myAjaxItem.slideDown(timerSlideDown);
			var targetScroll = $('#'+myIdItem).offset().top;
			$('html,body').animate({scrollTop: targetScroll}, timerScroll, 'easeOutQuint');
				if(myIdItem == 'hello'){hello();}
				if(myIdItem == 'book'){book();}
				if(myIdItem == 'client'){clients();}
				if(myIdItem == 'contact'){contact();}				
				
		});	
	}	
	event.preventDefault();
});

// easing
jQuery.extend( jQuery.easing, {
	easeOutQuint: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t*t*t + 1) + b;
	}
});

//gestion tu timer carousel
var timerCarousel = 5000
// -------Function a l ouverture de HELLO
function hello(){
	//on declare les carousels
	$('#carousel-hello').bxSlider({
		auto: true, 
    	controls: true,
		autoStart: true,
		pager: true,
		pause: timerCarousel, //gestion du timer entre les slides
		autoHover: true,
		easing: 'easeOutQuint'
	});

	$('.link-movie').click(function(){
		var myVid = $('.video-view').html();
		$('#hello-content .bx-wrapper').css({display:'none'});
		$('#hello-content').append('<div class="my-new-vid"><div class="close-vid"></div>'+myVid+'</div>');
		return false;
	});
	$(".close-vid").live("click", function(e){
		$('#hello-content .bx-wrapper').css({display:'block'});
		$('#hello-content .my-new-vid').remove();		
	});
}

function book(){
	//on declare les carousels
	var slider1 = $('#carousel-book-1').bxSlider({
		auto: false,
    	controls: false,
		autoStart: false,
		pager: true,
		pause: timerCarousel,
		prevSelector: '#bookPrev1',
		nextSelector: '#bookNext1',
		autoHover: true,
		easing: 'easeOutQuint'
	});
	$('#bookPrev1').click(function(){
        slider1.goToPreviousSlide();
    });
    $('#bookNext1').click(function(){
		slider1.goToNextSlide(); 
    });
	
	var slider2 = $('#carousel-book-2').bxSlider({
		auto: false,
    	controls: false,
		autoStart: false,
		pager: true,
		pause: timerCarousel,
		prevSelector: '#bookPrev2',
		nextSelector: '#bookNext2',
		autoHover: true,
		easing: 'easeOutQuint'
	});
	$('#bookPrev2').click(function(){
        slider2.goToPreviousSlide();
    });
    $('#bookNext2').click(function(){
		slider2.goToNextSlide(); 
    });
	
	var slider3 = $('#carousel-book-3').bxSlider({
		auto: false,
    	controls: false,
		autoStart: false,
		pager: true,
		pause: timerCarousel,
		prevSelector: '#bookPrev3',
		nextSelector: '#bookNext3',
		autoHover: true,
		easing: 'easeOutQuint'
	});
	$('#bookPrev3').click(function(){
        slider3.goToPreviousSlide();
    });
    $('#bookNext3').click(function(){
		slider3.goToNextSlide(); 
    });
	
	var slider4 = $('#carousel-book-4').bxSlider({
		auto: false,
    	controls: false,
		autoStart: false,
		pager: true,
		pause: timerCarousel,
		prevSelector: '#bookPrev4',
		nextSelector: '#bookNext4',
		autoHover: true,
		easing: 'easeOutQuint'
	});
	$('#bookPrev4').click(function(){
        slider4.goToPreviousSlide();
    });
    $('#bookNext4').click(function(){
		slider4.goToNextSlide(); 
    });
	
	var slider5 = $('#carousel-book-5').bxSlider({
		auto: false,
    	controls: false,
		autoStart: false,
		pager: true,
		pause: timerCarousel,
		prevSelector: '#bookPrev5',
		nextSelector: '#bookNext5',
		autoHover: true,
		easing: 'easeOutQuint'
	});
	$('#bookPrev5').click(function(){
        slider5.goToPreviousSlide();
    });
    $('#bookNext5').click(function(){
		slider5.goToNextSlide(); 
    });
}

function detailProjet(){
	$('.detail-projet').each(function(){
		var myHeight = $(this).find('.media').height();
		$(this).find('.right-detail').css({minHeight : (myHeight-21)});
	});
}

//VOIR LE DETAIL DU PROJET

$("#book-content .view-project a").live("click", function(e){
		var myUrl = $(this).attr('href');
		var myItem = $(this).parents('.item-section')
		var myIdItem = myItem.attr('id');
		var myContentItem = myItem.find('.content-item');
		var myAjaxItem = myItem.find('.ajax-content');

			$(this).parents('.ajax-content').fadeOut(timerFadeOut, 'easeOutQuint', function(){
				$(this).find('.content-item').remove();
				myAjaxItem.load(myUrl+' #book .content-item', function(e){
					myAjaxItem.fadeIn(timerFadeIn);
					//var targetScroll = $('#'+myIdItem).offset().top;
					//$('html,body').animate({scrollTop: targetScroll}, timerScroll, 'easeOutQuint');
					//detailProjet();
				});			
			});	
//			$('.item-section').removeClass('current-item');
	//		myItem.addClass('current-item');

		e.preventDefault();
});

//bouton retour projet
$("#projet-content .back-projet a").live("click", function(e){
		var myUrl = $(this).attr('href');
		var myItem = $(this).parents('.item-section')
		var myIdItem = myItem.attr('id');
		var myContentItem = myItem.find('.content-item');
		var myAjaxItem = myItem.find('.ajax-content');

			$(this).parents('.ajax-content').slideUp(timerSlideUp, 'easeOutQuint', function(){
				$(this).find('.content-item').remove();
				myAjaxItem.load(myUrl+' #book .content-item', function(e){
					myAjaxItem.slideDown(timerSlideDown);
					var targetScroll = $('#'+myIdItem).offset().top;
					$('html,body').animate({scrollTop: targetScroll}, timerScroll, 'easeOutQuint');
					book();
				});			
			});	
			$('.item-section').removeClass('current-item');
			myItem.addClass('current-item');

		e.preventDefault();
});


function clients(){
	$('#clients-content ul li').hover(function(){
		$(this).find('img').stop().animate({width:160, height:110}, 250);
	}, function(){
		$(this).find('img').stop().animate({width:150, height:104}, 250);
	});
}

function contact(){
	$('.inp-text input, .inp-text textarea').click(function(){
			if(this.value == this.defaultValue) {
				this.value = '';
			}
		})
	.blur(
		function(){
			if(this.value == '') {
				this.value = this.defaultValue;
			}
	});
	resizeMap();
	navMap();
	$(window).resize(resizeMap);
	
	$("#submit-button").click(function(){

		var nom = encodeURIComponent($("#nom").val());
		var prenom = encodeURIComponent($("#prenom").val());
		var email = $("#email").val();		
		var objet = encodeURIComponent($("#objet").val());
		var message = encodeURIComponent($("#message").val());
		var abonnement = $("#abonnement").is(':checked');
		
		var reg = new RegExp('^[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*@[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*[\.]{1}[a-z]{2,6}$', 'i');
		
		var data = "nom=" + nom + "&prenom=" + prenom + "&email=" + email + "&abonnement=" + abonnement + "&objet=" + objet + "&message=" + message;
		
		if(nom == "" || nom.toLowerCase() == "nom"){
			
			$("#error-nom").fadeIn(700).show();
			$("#loading").fadeOut(100).hide();
			
		}else if(prenom == "" || prenom.toLowerCase() == "prénom"){
			
			$("#error-prenom").fadeIn(700).show();
			$("#loading").fadeOut(100).hide();
						
			
		}else if(email == ""){
			
			$("#error-email").fadeIn(700).show();
			$("#loading").fadeOut(100).hide();

		}else if(reg.test(email) == false){
			
			$("#error-email").fadeIn(700).show();
			$("#loading").fadeOut(100).hide();
			
		}else if(objet == ""){
			
			$("#error-objet").fadeIn(700).show();
			$("#loading").fadeOut(100).hide();
			
		}else if(message == ""){
			
			$("#error-message").fadeIn(700).show();
			$("#loading").fadeOut(100).hide();
			
		}else{
			
			$("#loading").fadeIn(100).show();
		
			$.ajax({
				type: "POST",
				url: "contactsend.asp",
				data: data,
				success: function(){
					
					$("#loading").fadeOut(100).hide();
					$("#nom").fadeOut(100).hide();
					$("#prenom").fadeOut(100).hide();
					$("#email").fadeOut(100).hide();
					$("#objet").fadeOut(100).hide();
					$("#message").fadeOut(100).hide();
					$("#error-nom").fadeIn(100).hide();					
					$("#error-prenom").fadeIn(100).hide();										
					$("#error-email").fadeIn(100).hide();					
					$("#error-objet").fadeIn(100).hide();										
					$("#error-message").fadeIn(100).hide();										
					$("#submit-button").fadeOut(100).hide();
					$("#abonnement").fadeOut(100).hide();
					$("#labelabonnement").fadeOut(100).hide();					
					
					$('#message-sent').fadeIn(500).show();
					
				}
			});
			
		}
		
	});	
	
}
function resizeMap(){
	var windowWidth = $(window).width();
	$('#map').width(windowWidth-20);
}
function navMap(){
	$('#menu-map').find('li').click(function(){
		if($(this).is('.selected')){
			return false;
		}
		else{
			$('#menu-map li').removeClass('selected');
			$(this).addClass('selected');
			var myIdMap= $(this).attr('id').substring(5);
			$('#map img').fadeOut();
			$('#map img#img-map-'+myIdMap).fadeIn('fast');	
		}
		return false
	});
	$('#menu-map').find('li').hover(function(){
		$(this).addClass('hover');
	}, function(){
		$(this).removeClass('hover');
	});
}


