function instantiatePortfolioScroller()
{
	var animationSpeed		= 500;
	var subitemsPerPage		= 6;
	var animationEasing		= "easeInOutCubic";
	

	var containerParent 	= $("#IcingPortfolio .preview");
	var containers 			= $("#IcingPortfolio .preview div");
	var subitems 			= $("#IcingPortfolio li");
	var subitemBackButton	= $("#IcingPortfolio .back");
	var subitemNextButton	= $("#IcingPortfolio .next");
	var subitemContainer	= $("#IcingPortfolio .wrapper");
	var index 				= 0;
	var pages				= 0;
	var currentPage			= 0;
	var max 				= (subitems.length - 1);
	var containerWidth		= 0;
	var animating			= false;
	var interval			= null;

	
	if (containers.length < 1 || max < 1)
		return;
	
	// Update length
	containers.each(function(i, container)
	{
		if (!containerWidth)
			containerWidth = $(this).width();
	});
	
	function nextImage()
	{
		animating = true;
		
		// Clone, append, return
		var newItem = containers.first().clone().appendTo(containerParent).end();
		var newLeftPosition = containerWidth;
		var newIndex = updateIndex(++index);
		
		newItem.css("left", newLeftPosition);
		newItem.attr("src", "");
		newItem.find("img").attr("src", "");
		
		// Update length, Move all items
		$("#IcingPortfolio .preview div").each(function(i, container)
		{
			if ($(container).position().left === containerWidth)
				loadImageAtIndex(newIndex, container);
			
			$(container).animate({left: '-=' + containerWidth}, animationSpeed, animationEasing, function()
			{				
				if ($(container).position().left !== 0)
					$(container).remove();
				
				animating = false;
			});
		});
	}
	
	function previousImage()
	{
		animating = true;
		
		// Clone, append, return
		var newItem = containers.first().clone().appendTo(containerParent).end();
		var newLeftPosition = 0 - containerWidth;
		var newIndex = updateIndex(--index);
		
		newItem.css("left", newLeftPosition);
		newItem.attr("src", "");
		newItem.find("img").attr("src", "");
		
		// Update length, Move all items
		$("#IcingPortfolio .preview div").each(function(i, container)
		{
			if ($(container).position().left === (0 - containerWidth))
				loadImageAtIndex(newIndex, container);
			
			$(container).animate({left: '+=' + containerWidth}, animationSpeed, animationEasing, function()
			{					
				if ($(container).position().left !== 0)
					$(container).remove();
				
				animating = false;
			});
		});
	}
	
	function updateIndex(newIndex)
	{
		index = newIndex;
		
		if (index < 0)
			index = max;
		else if (index > max)
			index = 0;
		
		return index;
	}
	
	function loadImageAtIndex(imageIndex, container)
	{		
		$(container).find("img").attr("src", getImagePathAtIndex(imageIndex));
	}
	
	function getImagePathAtIndex(imageIndex)
	{
		var previewPath = "";
		
		subitems.each(function(i, subitem)
		{		
			$(subitem).removeClass("active");
			
			if (i == imageIndex)
			{
				$(subitem).addClass("active");
				previewPath = $(subitem).attr("data-preview"); 
			}
		});
		
		return previewPath;
	}
	
	subitems.each(function(i, subitem)
	{
		$(subitem).click(function(e)
		{
			if (!$(subitem).hasClass("active"))
			{
				if (i > index)
				{
					updateIndex(i - 1);
					nextImage();
				}
				else
				{
					updateIndex(i + 1);
					previousImage();
				}
			}
		});
	});	
	subitems.first().addClass("active");
	
	
	// Pagination
	if (subitemContainer.length > 0 && subitemBackButton.length > 0 && subitemNextButton.length > 0)
	{
		var subitemWrapper = subitemContainer.find("ul").first();
		var subitemLength = subitems.length;
		
		// Disable pagination controls if unnecessary
		if (subitemLength < subitemsPerPage)
		{
			subitemBackButton.addClass("disabled");
			subitemNextButton.addClass("disabled");
			
			subitemBackButton.click(function(e)
			{
				if (e) e.preventDefault();
			});
			
			subitemNextButton.click(function(e)
			{
				if (e) e.preventDefault();
			});
		}
		else
		{
			var newWrapperWidth = subitemLength * subitems.first().outerWidth();
			subitemWrapper.css("width", newWrapperWidth);
			pages = Math.ceil(subitemLength / subitemsPerPage);
			
			subitemBackButton.click(function(e)
			{
				if (e) e.preventDefault();
				
				if (!animating && currentPage > 0)
				{
					subitemWrapper.animate({left: '+=' + subitemContainer.width()}, animationSpeed, animationEasing, function()
					{
						updateCurrentPage(--currentPage);
						animating = false;
					});
				}
			});
			
			subitemNextButton.click(function(e)
			{
				if (e) e.preventDefault();
				
				if (!animating && currentPage < pages - 1)
				{
					subitemWrapper.animate({left: '-=' + subitemContainer.width()}, animationSpeed, animationEasing, function()
					{
						updateCurrentPage(++currentPage);
						animating = false;
					});
				}
			});
			
			subitemBackButton.addClass("disabled");
			
			function updateCurrentPage(newPage)
			{
				if (newPage === 0)
				{
					subitemBackButton.addClass("disabled");
					subitemNextButton.removeClass("disabled");
				}
				else if (newPage === pages - 1)
				{
					subitemNextButton.addClass("disabled");
					subitemBackButton.removeClass("disabled");
				}
				else
				{
					subitemNextButton.removeClass("disabled");
					subitemBackButton.removeClass("disabled");
				}
			}

		}
	} // end pagination
}

jQuery(function(){
   instantiatePortfolioScroller();
});

