$(function()
{
	/******** Gestion du survol des logos dans la barre de menu ********/
	$('div.menu div.partenaires a img').mouseenter(function(){
		$(this).animate({
			opacity : 0.5
		}, 200, 'swing');
	});
	$('div.menu div.partenaires a img').mouseleave(function(){
		$(this).animate({
			opacity : 1
		}, 200, 'swing');
	});
	
	
	/******** Gestion du contenu ********/
	var pageOuverte = '';
	var transitionPage = false;
	
	/* Accueil */
	$('div.menu a#accueil').click(function(){
		if(transitionPage == false)
		{
			transitionPage = true;
			$.address.value('');
			$('div.menu ul li a').removeClass('actif');
			var pageAOuvrir = $(this);
			if(pageOuverte == ''){
				deplacerPhotos(pageAOuvrir);
				pageAOuvrir.animate({
					left : 300
				}, 500, 'easeOutBack', function(){
					pageOuverte = pageAOuvrir;
					transitionPage = false;
				});
			}
			else{
				pageOuverte.animate({
					left : -135
				}, 500, 'easeOutBack', function(){
					deplacerPhotos(pageAOuvrir);
					pageAOuvrir.animate({
						left : 300
					}, 500, 'easeOutBack');
					pageOuverte = pageAOuvrir;
					transitionPage = false;
				});
			}
		}
		return false;
	});
	
	/* Ouverture d'une partie */
	$('div.menu ul li a').click(function(){
		if(transitionPage == false)
		{
			transitionPage = true;
			$.address.value($(this)[0].id);
			$('div.menu ul li a').removeClass('actif');
			$(this).addClass('actif');
			var pageAOuvrir = $('div.contenu div#'+$(this)[0].id);
			if(pageOuverte == ''){
				deplacerPhotos(pageAOuvrir);
				pageAOuvrir.animate({
					left : 300
				}, 500, 'easeOutBack', function(){
					pageOuverte = pageAOuvrir;
					transitionPage = false;
				});
			}
			else{
				pageOuverte.animate({
					left : -135
				}, 500, 'easeOutBack', function(){
					deplacerPhotos(pageAOuvrir);
					pageAOuvrir.animate({
						left : 300
					}, 500, 'easeOutBack');
					pageOuverte = pageAOuvrir;
					transitionPage = false;
				});
			}
		}
		return false;
	});
	
	/* Fermeture de la partie ouverte */
	$('div.contenu div div.fermer').click(function(){
		$.address.value('');
		$('div.menu ul li a').removeClass('actif');
		var pageAfermer = $($(this).parent('div'));
		pageAfermer.animate({
			left : -135
		}, 500, 'easeInBack', function(){
			pageOuverte = '';
		});
		return false;
	});
	
	
	/******** Gestion de la gallerie photos ********/
	var nbPhoto = 0;
	var posPhotos = 0;
	$('div.galleriePhotos div.photos div.photo').each(function(){ nbPhoto++; });
	$('div.galleriePhotos').width($(window).width()-400);
	$('div.galleriePhotos div.photos').width($('div.galleriePhotos').width()*nbPhoto);
	$('div.galleriePhotos div.photos div.photo').width($(window).width()-400);
	$('div.galleriePhotos').css('left',400);
	
	/* Déplacement vers la gauche */
	$('div.fleches div.flecheGauche').click(function(){
		posPhotos--;
		if(posPhotos < 0) posPhotos = nbPhoto-1;
		$('div.galleriePhotos div.photos').animate({
			left : -posPhotos*$('div.galleriePhotos').width()
		}, 500, 'easeInOutExpo');
		return false;
	});
	/* Déplacement vers la droite */
	$('div.fleches div.flecheDroite').click(function(){
		posPhotos++;
		if(posPhotos == nbPhoto) posPhotos = 0;
		$('div.galleriePhotos div.photos').animate({
			left : -posPhotos*$('div.galleriePhotos').width()
		}, 500, 'easeInOutExpo');
		return false;
	});
	/* Survol */
	$('div.fleches div').mouseenter(function(){
		$(this).animate({
			opacity : 0.7
		}, 300, 'easeOutExpo');
		return false;
	});
	/* Désurvol */
	$('div.fleches div').mouseleave(function(){
		$(this).animate({
			opacity : 1
		}, 300, 'easeInExpo');
		return false;
	});
	
	
	/* Dimensionnement des photos */
	$('div.galleriePhotos div.photos div.photo').each(function(){
		$(this).children('img').load(function(){
			var ratioDiv = $(this).parent('div.photo').width()/$(this).parent('div.photo').height();
			var ratioImg = $(this).width()/$(this).height();
			if(ratioDiv < ratioImg)
			{
				$(this).css('height','100%');
				var marginLeft = ($(this).parent('div.photo').width()-$(this).width())/2;
				$(this).css('margin-left', marginLeft);
			}
			if(ratioDiv >= ratioImg)
			{
				$(this).css('width','100%');
				var marginTop = ($(this).parent('div.photo').height()-$(this).height())/2;
				$(this).css('margin-top', marginTop);
			}
		});
	});
	
	
	/******** Déplace les photos en fonctions de la partie cliquée ********/
	function deplacerPhotos(pageAOuvrir){
		if(pageAOuvrir[0].id == 'accueil') posPhotos = nbPhoto-1;
		if(pageAOuvrir[0].id == 'apropos') posPhotos = 0;
		if(pageAOuvrir[0].id == 'menu') posPhotos = 1;
		if(pageAOuvrir[0].id == 'boissons') posPhotos = 2;
		if(pageAOuvrir[0].id == 'heures') posPhotos = 3;
		if(pageAOuvrir[0].id == 'coordonnees') posPhotos = 4;
		$('div.fleches div.flecheDroite').trigger('click');
	}
	
	
	/******** Calcul de la position et taille des éléments ********/
	$(window).bind('resize', function(){
		$('div.galleriePhotos').width($(window).width()-400);
		$('div.galleriePhotos div.photos div.photo').width($(window).width()-400);
		
		$('div.galleriePhotos div.photos div.photo').each(function(){
			var ratioDiv = $(this).width()/$(this).height();
			var ratioImg = $(this).children('img').width()/$(this).children('img').height();
			if(ratioDiv < ratioImg)
			{
				$(this).children('img').css('height','100%');
				$(this).children('img').css('width','auto');
				var marginLeft = ($(this).width()-$(this).children('img').width())/2;
				$(this).children('img').css('margin-left', marginLeft);
				$(this).children('img').css('margin-top', 0);
			}
			if(ratioDiv >= ratioImg)
			{
				$(this).children('img').css('width','100%');
				$(this).children('img').css('height','auto');
				var marginTop = ($(this).height()-$(this).children('img').height())/2;
				$(this).children('img').css('margin-top', marginTop);
				$(this).children('img').css('margin-left', 0);
			}
		});
		
		$('div.galleriePhotos div.photos').css('left', -posPhotos*$('div.galleriePhotos').width());
	});
	
	
	/******** Gestion de l'ouverture des parties au chargement de la page ********/
	$.address.init(function(){
		var idPage = $.address.value();
		idPage = idPage.substring(1, idPage.length);
		if(idPage != ''){
			transitionPage = true;
			$('div.menu ul li a#'+idPage).addClass('actif');
			var pageAOuvrir = $('div.contenu div#'+idPage);
			deplacerPhotos(pageAOuvrir);
			pageAOuvrir.animate({
				left : 300
			}, 500, 'easeOutBack', function(){
				pageOuverte = pageAOuvrir;
				transitionPage = false;
			});
		}
	});
	
	
	/******** Gestion du logo Motion In Design ********/
	/* Survol */
	$('div.logoMID').mouseenter(function(){
		$(this).animate({
			opacity : 1
		}, 300, 'easeOutExpo');
		return false;
	});
	/* Désurvol */
	$('div.logoMID').mouseleave(function(){
		$(this).animate({
			opacity : 0.5
		}, 300, 'easeInExpo');
		return false;
	});
	
	
	
	$(document).ready(function() {
		
	});
	
});

