(function($) {
	$(document).ready( function() {
		
		/* Click area manager */
		$('.clickarea').click(function(){
	    window.location=jQuery(this).find('a').attr("href");
	    return false;
	  });
	
		$('.home header>a').click(function(){
			window.location=SITE_URL + "/vision";
	    return false;
		});
		
	   $(".entry-content h3").next("p").addClass('clear');
	
		/* Home customers carrousel manager*/
		
		$('#customers_carousel ul').jcarousel({
	    scroll: 2,
			auto : 5,
			wrap : 'circular'
	    });
		
		/* Case studies boostrap */

		$('.gallery_count .total_elements').text(nbItem);
		$('.controls a.next').click(function(){next_gallery_item();});
		$('.controls a.previous').click(function(){previous_gallery_item();});
		
		/* Case studies gallery fancyboxing */
		$('#thumbnails li a').fancybox({'overlayColor'	:	'#333'});
		$('a.enlarge').click(function(){$('#thumbnails li:first-child a').trigger('click');});
		
		
		/* Jobs box_details swith buttons*/
		$('a.why_button,a.offer_button').click(function(){
			if($(this).hasClass('open')){ return false; }
			
			restore_display();
			
			$(this).css('background-position','top left');
			$(this).addClass('open');
			$('div.why,div.offer').fadeOut();
			$(this).parent().siblings('div.'+$(this).attr('target')).fadeIn();
			
			$(this).animate({
			    top:'+=60'
			  },800,function(){
			  });
				return false;
			});
		
		$('.more_infos a.close').click(function(){
			restore_display();
			$(this).parents('.why,.offer').fadeOut();
			return false;
		});
		
		/* Jobs scrolling Nav */
		
		$(window).scroll(function(){
			
			
			var advice = $("a[name=advice]");
			var h_advice = advice.position();
			
			var listen = $("a[name=listen]");
			var h_listen = listen.position();
			
			var narrate = $("a[name=narrate]");
			var h_narrate = narrate.position();
			
			var engage = $("a[name=engage]");
			var h_engage = engage.position();
			
			console.log("engage top" + h_engage.top);
			
						
    	if($(window).scrollTop() > 800){
				$('#top_nav').fadeIn('slow');
    	}
    	if($(window).scrollTop() < 800){
				$('#top_nav').fadeOut('slow');
    	}
	
			if($(window).scrollTop() > h_advice.top && $(window).scrollTop() < h_listen.top){
				$('#top_nav a').removeClass('current');
				$('#top_nav a:nth-child(1)').addClass('current');
	    	}
			if($(window).scrollTop() > h_listen.top && $(window).scrollTop() < h_narrate.top){
				$('#top_nav a').removeClass('current');
				$('#top_nav a:nth-child(2)').addClass('current');
				}
			if($(window).scrollTop() > h_narrate.top && $(window).scrollTop() < h_engage.top){
				$('#top_nav a').removeClass('current');
				$('#top_nav a:nth-child(3)').addClass('current');	    	}
			if($(window).scrollTop() > h_engage.top){
				$('#top_nav a').removeClass('current');
				$('#top_nav a:nth-child(4)').addClass('current');	    	}
		});
		
		/* Google Maping Manager */
		if(MAP){
		$('#map').gMap({ markers: [{ address: "50 rue de Montreuil, Paris,France",
		                              html: "Spintank<br />50 rue de Montreuil<br />Paris 11ème",
		                              icon: { image: TEMPLATE_URL + "/images/spintank_map_point.png",
		                                      iconsize: [100, 56],
		                                      iconanchor: [52,56],
		                                      infowindowanchor: [12, 0] } }],
		                  address: "Paris, France",
		                  zoom: 12 });
		}
	});
	
	/*
	FUNCTIONS ####################################################
	*/
	
	function restore_display(){
		
		$('nav.switch a.open').animate({
		    top: '-=60'
		  }, 800, function(){
			$(this).css('z-index','11');
			$(this).css('background-position','bottom left');
			$(this).removeClass('open');
		 });
		
		
	}
	
	var imgActive = 0;
	var galleryArray = $('ul#thumbnails li');
	var legendArray = $('.legend p');
	
	var nbItem = $('ul#thumbnails li').length;
	
	function next_gallery_item(){
		// Définition de la nouvelle image à afficher
		imgActive++;
		if(imgActive == nbItem) imgActive = 0;

		// Affichage de l'image active et masquage des autres
		galleryArray.each(function(i, el){
			(imgActive == i) ? $(galleryArray[i]).fadeIn(800) : $(galleryArray[i]).fadeOut(800);
		});
		legendArray.each(function(i, el){
			(imgActive == i) ? $(legendArray[i]).fadeIn(800) : $(legendArray[i]).fadeOut(800);
		});
		
		$('.gallery_count .current').text(imgActive+1);
		
	}
	
	function previous_gallery_item(){
		// Définition de la nouvelle image à afficher
		imgActive = imgActive - 1;
		if(imgActive < 0) imgActive = nbItem-1;

		// Affichage de l'image active et masquage des autres
		galleryArray.each(function(i, el){
			(imgActive == i) ? $(galleryArray[i]).fadeIn(800) : $(galleryArray[i]).fadeOut(800);
		});
		legendArray.each(function(i, el){
			(imgActive == i) ? $(legendArray[i]).fadeIn(800) : $(legendArray[i]).fadeOut(800);
		});
		$('.gallery_count .current').text(imgActive+1);
		
	}
	
})(jQuery);
