;(function($){
	
	$.fn.position = function(opacity)
	{
		var $this = $(this);
		var offset = $this.offset();

		// Get the slider in the middle of the text. 
		var left = offset.left + ($this.parent().width() / 2) - ($('#slider').width() / 2);

		// And a bit above it, 15 is just a number that works well
		var top = offset.top + 15;
					
		return {
			top: top,
			left: left,
			opacity: opacity
		};
	}
	
	$.fn.slider = function()
	{
		var $slider = $('<div id="slider"></div>');
		
		return this.each(function() 
		{	
			// Don't show it on word wraps
			if ($(this).height() / 2 < $(this).children('li:first').height())
			{
				// Stick it after
				$(this).after($slider);

				// And re-position
				$('#slider').css($(this).find('a:first').position(0));

				$(this).find('a').click(function(e) 
				{
					var $this = $(this);

					// Relocate the slider to the proper offset
					$('#slider').animate($this.position(1));

					// Prevent page jump
					return false;
				});
			}
		});
	}	
	
})(jQuery);

;(function($) {
	
	$.profiles = 
	{
		init: function()
		{
			// Hide all of the profiles
			$('.profile:not(#default), .employee').hide();
			
			// Attach the click handler
			$('#profiles a').click(this.click);	
			
			// Start the slider
			$('#profiles').slider();
			
			// Tabs
			this.tabify();
			
			// Load jquery color
			$.getScript('/javascripts/plugins/jquery.color.js');
			
			// Check for an inital value
			if (window.location.hash) 
			{
				$('#profiles a[href=' + window.location.hash + ']').click();
			}
			
			// Overload main navigation
			$('#careers ul a').click(this.navigation);
		},
		
		click: function()
		{
			var $this = $(this);
			var $target = $($this.attr('href'));
			var $current = $('.profile:visible');
			
			// Only select this
			$('#profiles .selected').removeClass('selected');
			$this.addClass('selected');
			
			// Switch tabs
			$current.fadeOut('fast', function() 
			{
				$current.hide();
				$target.fadeIn();
			});
			
			return false;
		}, 
		
		tabify: function()
		{
			$('.profile').each(function() 
			{
				var $this = $(this);
				var $headers = $this.children('.employee').children('h2').clone();

				// Move the headers around
				$this.children('.employee:first').before($headers);
				$headers.append('<span class="learn-more">&gt; en savoir plus</span>');
				
				// Manipulate the headers a bit
				$headers
					.addClass('profile-link')
					.click(function() 
					{
						var index = $headers.index(this);
						
						// Select all
						$headers.removeClass('selected');
						$(this).addClass('selected');
						
						// Hide all but the matching index
						$this
							.children('.employee')
							.hide()
							.eq(index)
							.show();	
						
						return false;		
					})
					.hover(function() {
						$(this).animate({backgroundColor: '#F6F6F6'}, 'fast');
					}, function() {
						$(this).animate({backgroundColor: '#FFF'}, 'fast');
					})
					.eq(0)
					.addClass('first');
			});
		},
		
		navigation: function()
		{
			$('#profiles a[href=' + this.hash + ']').click();
			return false;
		}
	};
	
})(jQuery);