function instantiateScroller()
{
	var containerParent 	= $("#messaging .preview");
	var containers 			= $("#messaging .preview a");
	var subitems 			= $("#messaging li");
	var subitemsClickable	= $("#home #messaging li");
	var index 				= 0;
	var max 				= (subitems.length - 1);
	var containerWidth		= 0;
	var animationSpeed		= 800;
	var scrollDelay			= 6000;
	var animationEasing		= "easeInOutCubic";
	var animating			= false;
	var interval			= null;

	
	if (containers.length < 1 || max < 1)
		return;
	
	// Update length
	containers = $("#messaging .preview a");
	containers.each(function(i, container)
	{
		if (!containerWidth)
			containerWidth = $(this).width();
			
		if (!containerWidth)
			containerWidth = $(this).find("img").first().width();
	});
	
	// Next Button
	$("#messaging .last").click(function(e)
	{
		e.preventDefault();
		clearInterval(interval);
		
		if (!animating)
			nextImage();
	});
	
	// Back button
	$("#messaging .first").click(function(e)
	{
		e.preventDefault();
		clearInterval(interval);
		
		if (!animating)
			previousImage();
	});
	
	// Go to index/image
	subitemsClickable.each(function(i, subitem)
	{
		$(subitem).click(function(e)
		{
			e.preventDefault();
			clearInterval(interval);
			goToImage(i);
		});
	});
	
	function nextImage()
	{
		console.log('next');
		animating = true;
		removeHitAreas();
		
		// Clone, append, return
		var newItem = containers.first().clone().appendTo(containerParent).end();
		var newLeftPosition = 0 + containerWidth;
		var newIndex = updateIndex(++index);
		
		newItem.css("left", newLeftPosition);
		newItem.find("img").attr("src", "");
		
		// Update length, Move all items
		$("#messaging .preview a").each(function(i, container)
		{
			if ($(container).position().left !== 0)
			{
				loadImageAtIndex(newIndex, container);
				setHrefAtIndex(newIndex, container);
			}
			
			$(container).animate({left: '-=' + containerWidth}, animationSpeed, animationEasing, function()
			{				
				if ($(container).position().left !== 0)
				{
					$(container).remove();
					injectHitAreas();
				}
				
				animating = false;
			});
		});
	}
	
	function previousImage()
	{
		animating = true;
		removeHitAreas();
		
		// Clone, append, return
		var newItem = containers.first().clone().appendTo(containerParent).end();
		var newLeftPosition = 0 - containerWidth;
		var newIndex = updateIndex(--index);
		
		newItem.css("left", newLeftPosition);
		newItem.find("img").attr("src", "");
		
		// Update length, Move all items
		$("#messaging .preview a").each(function(i, container)
		{
			if ($(container).position().left < 0)
			{
				loadImageAtIndex(newIndex, container);
				setHrefAtIndex(newIndex, container);
			}
			
			$(container).animate({left: '+=' + containerWidth}, animationSpeed, animationEasing, function()
			{					
				if ($(container).position().left !== 0)
				{
					$(container).remove();
					injectHitAreas();
				}
				
				animating = false;
			});
		});
	}
	
	function goToImage(imageIndex)
	{
		if (!animating && imageIndex != index)
		{		
			if (imageIndex > index) 
			{
				updateIndex(imageIndex - 1);
				nextImage();
			}
			else
			{
				updateIndex(imageIndex + 1);
				previousImage();
			}
		}
	}
	
	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 setHrefAtIndex(imageIndex, container)
	{		
		$(container).attr("href", getHrefAtIndex(imageIndex));
		
		if ($(container).attr("href") == "#")
		{
			$(container).click(function(e)
			{
				e.preventDefault();
			}).addClass("disabled");;
		}
	}
	
	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;
	}
	
	function getHrefAtIndex(imageIndex)
	{
		var href = "";
		
		subitems.each(function(i, subitem)
		{			
			if (i == imageIndex)
				href = $(subitem).find("a").first().attr("href"); 
		});
		
		return href;
	}
	
	function preloadImages()
	{
		subitems.each(function(i, subitem)
		{
			$("#container").append("<img style='display:none;' src='" + $(subitem).attr("data-preview") + "' />");
		});
	}
	
	function injectHitAreas()
	{
		var hitAreasContainer = $(subitems[index]).find(".hitareas");
		
		if (hitAreasContainer.length > 0)
			containerParent.prepend(hitAreasContainer.first().clone());
	}
	
	function removeHitAreas()
	{
		try {
			containerParent.find(".hitareas").remove();
		} catch(e){}
	}
	
	
	interval = setInterval(nextImage, scrollDelay); // Automatically scroll through
	preloadImages();
}

// DOM READY
$(function()
{
	instantiateScroller();
});
