(function($)
{
	$(function()
	{
		var item_class = '.bio';
		var image_wrapper_class = '.bio_images';
		var color_image_class = '.bio_color_image';
		var bw_image_class = '.bio_bw_image';
		var text_wrapper_class = '.bio_text_wrapper';
		var text_class = '.bio_text';
		
		var container = $('#employee-slider');
		var slide = $('#employee-slider-slide', container);
		var color_images = $(color_image_class, slide);
		var qty = $(item_class, slide).size();
		var dudes_on_the_right = 2;
		var speed = 500;
		var text_padding = 20;
		var allow_animation = true;
		
		function transition( dir, item )
		{
			var first_sibling_width = 0;
			var second_sibling_width = 0;
			var visible_images_width = 0;
			var left_over_width = 0;
			var built_up_img_width = 0;

			if( allow_animation )
			{
				allow_animation = false;
				
				if( dir=='>' )
				{
					var index = item.index();
					var text_wrapper = $(text_wrapper_class, item);
					var next_buildup_item = item.next();

					for( var ii=0;ii<dudes_on_the_right;ii++ )
					{
						built_up_img_width = built_up_img_width + $(image_wrapper_class, next_buildup_item).outerWidth();
						next_buildup_item = next_buildup_item.next();
					}

					visible_images_width = $(image_wrapper_class, item).outerWidth() + built_up_img_width;
					left_over_width = container.innerWidth() - visible_images_width;

					$(text_class, item).css({width:left_over_width - (text_padding*2)});
					$(color_image_class, slide).not($(color_image_class, item)).fadeOut();
					$(color_image_class, item).fadeIn();

					item.prevAll().each(function()
					{
						var _item = $(this);
						var img_width = $(image_wrapper_class, _item).outerWidth();

						$(image_wrapper_class, _item)
							.stop()
							.animate({width:0}, speed-10 );

						$(bw_image_class, _item)
							.stop()
							.animate({left:0-img_width}, speed-10, function()
							{
								$(bw_image_class, _item).attr('style','');
							});

						$(color_image_class, _item)
							.stop()
							.animate({left:0-img_width}, speed-10, function()
							{
								$(color_image_class, _item).attr('style','');
							});

						_item
							.stop()
							.animate({width:0}, speed, function()
							{
								_item.appendTo(slide).attr('style','');
								$(image_wrapper_class, _item).attr('style','');
							});
					});

					$(text_wrapper_class, slide)
						.not( text_wrapper )
						.stop()
						.animate({width:1}, speed );

					text_wrapper
						.stop()
						.animate({width:left_over_width}, speed, function()
						{
							allow_animation = true;
						});
				}
				else if( dir=='<' )
				{
					var _item = $(item_class, slide).last();
					var img_width = $(image_wrapper_class, _item).outerWidth();

					for( var ii=0;ii<dudes_on_the_right;ii++ )
					{
						built_up_img_width = built_up_img_width + $(image_wrapper_class + ':eq('+ii+')', slide).outerWidth()
					}

					visible_images_width = img_width + built_up_img_width;
					left_over_width = container.innerWidth() - visible_images_width;

					$(color_image_class, slide).not($(color_image_class, _item)).fadeOut();

					$(text_class, _item).css({width:left_over_width - (text_padding*2)});

					$(text_wrapper_class, _item)
						.stop()
						.css({
							width:1,
							left:0-img_width
						});

					_item.prependTo(slide);

					$(bw_image_class, _item)
						.stop()
						.css({left:0-img_width})
						.animate({left:0}, speed-10 );

					$(color_image_class, _item)
						.stop()
						.css({left:0-img_width})
						.show()
						.animate({left:0}, speed-10 );

					$(text_wrapper_class, _item).animate({
						width:left_over_width,
						left:0
					}, speed, function()
					{
						allow_animation = true;
					});

					$(text_wrapper_class, slide)
						.not( $(text_wrapper_class, _item) )
						.stop()
						.animate({width:1}, speed );
				}
			}
		}
		
		$(item_class, slide).each(function()
		{
			$(this).click(function()
			{
				transition( '>', $(this) );
			});
		});
		
		color_images.hide();
		
		$('#employee-slider-left-arrow').click(function()
		{
			transition( '<', $(item_class, slide).last() );
		});
		
		$('#employee-slider-right-arrow').click(function()
		{
			transition( '>', $(item_class + ':eq(1)', slide) );
		});
		
		slide.css({'width':qty*container.innerWidth()});
		
		$(bw_image_class, slide).each(function(i)
		{
			var img_src = $(this).attr('src');
			var mem_img = new Image();
			mem_img.onload = function()
			{
				if( qty == (i+1) )
				{
					transition( '>', $(item_class + ':eq(0)', slide) );
				}
			};
			mem_img.src = img_src;
		});		
	});

})(jQuery);
