/**
 * @author imac
 * Document Jquery
 */

$(document).ready(function(){
	
	var i=0;
	
	var agent=navigator.userAgent.toLowerCase();
	
	presenceFrise = $(document).find('.frise').html();
	presenceSlideshow = $(document).find('#slideshow').html();
	
	codeRegex = /mobile/i;
	
	if(codeRegex.test(agent) == true){
		menuMobile();
	}
	
	else{
	}
	
	$('li.bloc .nom-project').next().css('float','left').css({
		marginTop:'7px',
		width:'80px'
	});
	
	if($('li.bloc').css('display') == 'block'){
		$('ul.frise').append('<div style="clear:both"></div>')
	}
	
	
	heightCadreFrise = $('#cadre-frise').height();
	
	windowWidth = $(window).width();
	
	placeFrise();
	
	$(window).resize(placeFrise);
	
	function placeFrise(){
		
		heightFrise = $('#cadre-frise').height()
		heightListe = $('#cadre-frise ul').height();
		
		if(heightListe <= heightFrise){
			$('.navigation-frise > *').css('visibility','hidden');
		}
		
		else{
			$('.navigation-frise > *').css('visibility','visible');
		}
		
		windowWidth = $(window).width();
	
		toolbarProjectsWidth = $('.toolbar-projects').width();

		friseWidth = windowWidth - toolbarProjectsWidth;
		
		$('.toolbar-projects').css({
			position:'absolute',
			left:'0'
		})
		
		$('.cadre-frise').css({
			width: friseWidth + 'px',
			marginTop:'55px',
		});
		
		if(toolbarProjectsWidth != null){
			$('.cadre-frise').css({
				float:'right',
			});
		}
		
		$('.navigation-frise').css({
			width: friseWidth + 'px',
			position:'absolute',
			right:'0'
		});
		
		
		
		friseHeight = $('.cadre-frise').height();
		
	}
	
	
	heightListe = $('#cadre-frise ul').height();

	function boutonHorizontal(){
		
		$('.vertical .btn-navigate .legend').css({
			opacity:'0'
		})

		$('.horizontal .btn-navigate').append('<div class="separation"></div>').find('.legend').css({
			opacity:'0'
		})
		
		$('.horizontal .btn-navigate .separation').css({
			opacity:'0'
		})

	}
	
	boutonHorizontal();
	

	function showButton(){
		if($(this).parent().hasClass('horizontal')){
			$(this).addClass('hovered').find('.legend').animate({
				width:'190px',
				height:'20px',
				marginLeft:'-6px',
				marginTop:'-26px',
				color:'#6f6f6f',
				padding:'0 10px',
				opacity:'1',
				backgroundColor:'#fff',
			},250);
			
			$(this).find('.separation')
				.css({display:'block'})
				.animate({
					marginTop:'-5px',
					opacity:'1',
					backgroundPosition:'0px -2px'
				},250);
		}
		
		if($(this).parent().hasClass('vertical')){
			$(this).addClass('hovered').find('.legend').animate({
				padding:'0',
				width:'220px',
				height:'30px',
				opacity:'1',
			},250);
			
			$(this).find('.separation')
				.css({display:'block'})
				.animate({
					marginTop:'-5px',
					opacity:'1',
					backgroundPosition:'0px -2px'
				},250);
		}
		
		else{
			$(this).addClass('hovered')
		}
		
	}
	
	function hideButton(){
		if($(this).parent().hasClass('horizontal')){
			
			$(this).find('.legend').animate({
				width:'0',
				height:'20px',
				padding:'0',
				color:'#666',
				marginTop:'5px',
				backgroundColor:'#666',
				//opacity:'0',
			},250, function(){
				$(this).parent().removeClass('hovered');
							
			})

			$(this).find('.separation').animate({
				marginTop:'0px',
				opacity:'0',
			},250, function(){
				$(this).delay(250).css({display:'none'})
			});
		}
		
		if($(this).parent().hasClass('vertical')){
			$(this).find('.legend').animate({
				width:'0',
				height:'30px',
				padding:'0',
				marginLeft:'30px',
				marginTop:'-1px',
				opacity:'0',
			},250, function(){
				$(this).parent().removeClass('hovered');			
			})
		}
		
		
		else{
			$(this).removeClass('hovered')
		}
		
	}
	
	//footerFrise();
	
 	$('.btn-navigate').mouseenter(showButton);
	
/*
	$('.btn-navigate').each(function(){
		$(this).trigger('mouseenter')
	})
*/

	$('.btn-navigate').mouseleave(hideButton);
	

	
	$('.zone-search input[type=text]').focus(function(){
		if ($(this).attr('value') == 'Search'){
			$(this).attr('value','');
		}
	});
	
	$('.zone-search input[type=text]').blur(function(){
		if ($(this).attr('value') == ''){
			$(this).attr('value','Search');
		}
	});
	
	function showSousTitre(){
		
		$('.menu-top li .sous-titre').fadeOut(200);
		
		if ($(this).next().css('display') == 'block'){
			$(this).next().css('display','block');
		}
		
		else{
			$(this).delay(200).next().fadeIn(200);
		}
			
	}
	
	function hideSousTitre(){
		$(this).find('.sous-titre').fadeOut(200);
	}
	
	$('.menu-top li:not(.active) a').mouseenter(showSousTitre);
	
	$('.menu-top li:not(.active)').mouseleave(hideSousTitre);
	
	$('.menu-top ul').mouseleave(function(){
		$('.menu-top li.active .sous-titre').fadeIn(200);
	});
	
	function centerPhotos(){
		$('.cadre-img img').each(function(){
			imgHeight = $(this).height();
			cadreHeight = $(this).parent().height();
			
			imgMarginHeight = (cadreHeight - imgHeight)/2;
			
			//alert('Hauteur Image: ' +imgHeight+ '\n Hauteur Cadre:' +cadreHeight+ '\n Margin-top:'+imgMarginHeight )
			
			$(this).css({
				marginTop: imgMarginHeight +'px',
			})
		})
	}
	
	$(window).load(function(){
		centerPhotos();	
	});
	

/*
	
	else{
		$('.navigation-frise .btn-nav-frise').wrap('<span/>')
	}
*/
		
	function scrollFrise(el, direction){
		
		
		
		
		heightListeB = heightListe - 327;

		
		switch(direction){
			case "descendre":
				if ((heightCadreFrise * (i+1)) < heightListe){
					i=i+1;
/*
					$('.navigation-frise .prev').animate({
						opacity:'1'
					},250).removeClass('inactive');
*/
				}
				break;

			case "remonter":
				if (i > '0'){
					i=i-1;
/*
					$('.navigation-frise .next').animate({
						opacity:'1'
					},250).removeClass('inactive');
*/
				}
				break;
			
		}
		
		scrolling = heightCadreFrise * i;
		scrollingB = heightCadreFrise * (i+1);
		
		$('#cadre-frise').animate({
			scrollTop: scrolling,
		}, 500, 'swing');
	}
	
	$('.navigation-frise .next').click(function(){
		scrollFrise($('#cadre-frise'), "descendre");
	});
	
	$('.navigation-frise .prev').click(function(){
		scrollFrise($('#cadre-frise'), "remonter");
	});



	function autorizeShortcutsFrise(){
		presenceLonguePage = $('.cadre-text-long').html();
		
		if(presenceLonguePage == null){
		   	shortcut.add("Down",function() {
				scrollFrise($('#cadre-frise'), "descendre");
			});
			
			shortcut.add("Up",function() {
				scrollFrise($('#cadre-frise'), "remonter");
			});
		}
		
	}
	
	autorizeShortcutsFrise();

	
	function animBoutonsHome(){
		
		if($(this).attr('class') == ''){
			$(this).find('.sous-titre').fadeOut(500, 'swing');
		
			$(this).find('.bouton-menu').animate({
				opacity:'0',
			},500, 'swing')
		
			$(this).find('.visuel').animate({
				marginTop:'0',
			},500, 'swing')
			
			$(this).find('.shadow-visuel').fadeIn(500, 'swing');
			
		}
		
	}
	
	function resetBoutonsHome(){
		if ($(this).attr('class') == '') {
			$(this).find('.sous-titre').fadeIn(500, 'swing');
			
			
			$(this).find('.bouton-menu').animate({
				opacity: '1',
			}, 500, 'swing')
			
			$(this).find('.visuel').animate({
				marginTop: '-100px',
			}, 500, 'swing')
			
			$(this).find('.shadow-visuel').fadeOut(500, 'swing');
			$.data(this, 'hover', false);
		}
	}
	

	//$('.zone-big-boutons li').mouseenter(animBoutonsHome);
	
	//$('.zone-big-boutons li').mouseleave(resetBoutonsHome);

	
	var indice = 0;
	
	var indiceAv = 0;

	function menuMobile(){
		setInterval(function(){
			
			
			elAv=$('.zone-big-boutons li:not(.disabled):eq('+indiceAv+')');
			elAv.trigger('mouseleave');
			
			el=$('.zone-big-boutons li:not(.disabled):eq('+indice+')');
			el.trigger('mouseenter');
			
			if(indice<$('.zone-big-boutons li:not(.disabled)').length-1){
				indiceAv=indice;
				indice++;
			}else{
				indiceAv=$('.zone-big-boutons li:not(.disabled)').length-1;
				indice=0;
			}
			
		},2000);
	}
	
});

