(function($)
{
	$(function()
	{
		
		var portfolioFeatureBar = $('.portfolioFeatureBar');
		var portfolioFeatureItem = $('.portfolioFeatureBarContentItem').clone();
		
		$('#portfolioNavigationIcons').hide();
		$('.portfolioFeatureBarContentItem').remove();
		
		var activeItem = false;
		var generatedItems = {};
		var animating = false;
		
		
		$('.portfolioItem').mouseenter(function(e)
		{
			$('.portfolioTitle', this).stop().animate({
				'width': '285px',
				'height': '138px'
			}, {
				'duration': 160,
				'easing': 'easeOutCirc'
			});
			
			$('.portfolioService', this).stop().animate({
				'marginTop': '0px'
			}, {
				'duration': 160,
				'easing': 'easeOutCirc'
			});
		});
		
		
		
		$('.portfolioItem').mouseleave(function(e)
		{
			$('.portfolioTitle', this).stop().animate({
				'width': '205px',
				'height': '50px'
			}, {
				'duration': 160,
				'easing': 'easeOutCirc'
			});
			
			$('.portfolioService', this).stop().animate({
				'marginTop': '-40px'
			}, {
				'duration': 160,
				'easing': 'easeOutCirc'
			});
		});
		
		
		$('.portfolioItem').click(function(e) {
			e.preventDefault();
			
			if(animating || $(this).hasClass('active'))
			{
				return false;
			}
			
			animating = true;
			
			$('.portfolioItem').removeClass('active');
			$(this).addClass('active');
			
			var id = $(this).attr('id');
			
			if(!generatedItems[id])
			{
				var item = portfolioFeatureItem.clone().attr('id', id + '_item');
				var title = $(this).find('.portfolioTitle').text();
				var images = $(this).find('.portfolioImages').text().split(',');
				var description = $(this).find('.portfolioDescription').text();
				var services = $(this).find('.portfolioServices').text().split(',');
				var extlink = $(this).find('.portfolioExtLink').text();
				
				for(i in images)
				{
					if(!isNaN(i))
					{
						var img = $('<img id="' + id + '_image' + i + '" src="' + images[i] + '" width="600" height="277" alt="" />');
						var imgSelector = $('<a href="#' + id + '_image' + i + '">' + i + '</a>');
						
						item.find('.portfolioFeatureBarImage').append(img);
						item.find('.portfolioFeatureBarImageSelector').append(imgSelector);
						
						imgSelector.click(function(e)
						{
							e.preventDefault();
							
							if($(this).hasClass('active'))
							{
								return;
							}
							
							var activeImgSelector = item.find('.portfolioFeatureBarImageSelector a.active');
							
							var activeImg = item.find('.portfolioFeatureBarImage img.active');
							
							var newImgSelector = $(this);
							var newImg = $(newImgSelector.attr('href'));
							
							activeImgSelector.stop().removeClass('active');
							newImgSelector.stop().addClass('active');
							
							var imgHeight = activeImg.height();
							
							newImg.css({
								'top': imgHeight + 'px'
							});
							
							newImg.addClass('active');
							
							activeImg.stop().animate({
								'top': '-' + imgHeight + 'px'
							}, {
								'duration': 800,
								'easing': 'easeOutExpo',
								'complete': function()
								{
									$(this).removeClass('active');
								}
							});
							
							newImg.stop().animate({
								'top': '0px'
							}, {
								'duration': 800,
								'easing': 'easeOutExpo'
							});
							
						});
					}
				}
				
				for(i in services)
				{
					if(!isNaN(i))
					{
						var service = $('<li>' + services[i] + '</li>');
						
						item.find('.portfolioFeatureBarText ul').append(service);
					}
				}
				
				item.find('.portfolioFeatureBarText .portfolioFeatureBarTitle').text(title);
				item.find('.portfolioFeatureBarText .portfolioFeatureBarDescription').text(description);
				
				if(!extlink)
				{
					item.find('.portfolioFeatureBarExtLink').remove();
				}
				else
				{
					item.find('.portfolioFeatureBarExtLink a.portfolioViewSiteButton').attr('href', extlink);
				}
				
				generatedItems[id] = item;
				
				portfolioFeatureBar.append(item);
				
			}
			else
			{
				var item = generatedItems[id];
			}
			
			item.find('.portfolioFeatureBarImage img').removeClass('active').stop().css({ 'top': '0px' });
			item.find('.portfolioFeatureBarImageSelector a').removeClass('active');
			
			item.find('.portfolioFeatureBarImage img:first').addClass('active').stop().css({ 'top': '0px' });
			item.find('.portfolioFeatureBarImageSelector a:first').addClass('active');
			
			item.css('left', $(window).width() + (item.width() / 2));
			
			
			var animateInProject = function(item)
			{
				item.animate({
					'left': ($(window).width() / 2)
				}, {
					'duration': 800,
					'easing': 'easeInOutExpo',
					'complete': function()
					{
						activeItem = item;
						animating = false;
					}
				});
			}
			
			
			if(activeItem)
			{
				if($(window).scrollTop() > 200)
				{
					$.scrollTo($('#content'), {
						'axis': 'y',
						'duration': 500,
						'easing': 'easeOutExpo',
						'onAfter': function()
						{
							activeItem.animate({
								'left': - ($(window).width() / 2)
							}, {
								'duration': 800,
								'easing': 'easeOutExpo'
							});
							
							animateInProject(item);
						}
					});
				}
				else
				{
					activeItem.animate({
						'left': - ($(window).width() / 2)
					}, {
						'duration': 800,
						'easing': 'easeOutExpo'
					});
					
					animateInProject(item);
				}
			}
			else
			{
				if($(window).scrollTop() > 200)
				{
					$.scrollTo($('#content'), {
						'axis': 'y',
						'duration': 500,
						'easing': 'easeOutExpo',
						'onAfter': function()
						{
							$('#portfolioNavigationIcons').fadeIn();
							$('.portfolioFeatureBarMenu').slideUp(600, 'easeOutExpo', function()
							{
							});
							
							portfolioFeatureBar.animate({
								'height': item.height()
							}, {
								'duration': 800,
								'easing': 'easeInOutExpo',
								'complete': function()
								{
									animateInProject(item);
								}
							});
						}
					});
				}
				else
				{
					$('#portfolioNavigationIcons').fadeIn();
					$('.portfolioFeatureBarMenu').slideUp(600, 'easeOutExpo', function()
					{
					});
					
					portfolioFeatureBar.animate({
						'height': item.height()
					}, {
						'duration': 800,
						'easing': 'easeInOutExpo',
						'complete': function()
						{
							animateInProject(item);
						}
					});
				}
			}
			
		});
		
		
		
		$('#portfolioNavigationIcons a').tipsy({
			fade: true,
			gravity: 's',
			opacity: 1,
			title: function()
			{
				return $(this).text();
			},
			trigger: 'mouseenter'
		});
		
		
		
	});
	
	
})(jQuery);

/*

(function($)
{
	$(function()
	{
		
		
		
		
		
		
		var itemList = $('#portfolioItemList');
		var itemListClone = itemList.clone();
		var itemListShowall = $('.portfolioFeatureBarMenu a.showall');
		var itemListFilters = $('.portfolioFeatureBarMenu a');
		
		
		
		function getItemTypes(buttons)
		{
			var selectedButtons = buttons.filter('.active');
			var types = [];
			
			selectedButtons.each(function()
			{
				var classNames = $(this).attr('class').split(' ');
				
				for(var i = 0; i < classNames.length; i++)
				{
					if(classNames[i].indexOf('type-') == 0)
					{
						types.push(classNames[i].split('-')[1]);
					}
				}
			});
			
			return "." + types.join(', .');
		};
		
		
		
		function sortItems()
		{
			var type = getItemTypes(itemListFilters);
			var filteredItemList = itemListClone.find(type);
			
			itemList.quicksand(filteredItemList,
			{
				duration: 800,
				easing: 'easeOutCubic',
				useScaling: false,
				attribute: 'id',
				enhancement: function()
				{
					Cufon.refresh();
				}
				
			});
		}
		
		
		$(itemListShowall).click(function(e)
		{
			e.preventDefault();
			
			itemListFilters.addClass('active');
			
			sortItems();
		});
		
		$(itemListFilters).click(function(e)
		{
			e.preventDefault();
			
			itemListFilters.removeClass('active');
			$(this).addClass('active');
			
			sortItems();
		});
		
		sortItems();
		
		
	});
})(jQuery);

*/
