
// Just a 'closure' $ becomes jQuery
(function($)
{
	// extend a new function (fn)
	$.fn.fadeElements = function()
	{
		// Do your library code here :)
		var siblings = $(this).slice(1);
		
		$(this).eq(0).fadeIn('400');
		if (siblings.length)
		{				
			setTimeout(function(){ siblings.fadeElements();}, 75);
		}
		
		// Make it chainable!
		return $(this);
	}
})(jQuery);


// get current active image number
function activeImage()
	{
	return $(".projectImages #scroll li.selected").prevAll().length;
}

//get total number of images
function totalImages(){
	return $('.projectImages #scroll li').length;
}


//does the sliding
function slideshow(num)
{
	var viewport = 360;
	//debugger;

	if ($('#scroll:animated').length)
	{
		return;
	}
	
	if ($('#scroll2:animated').length)
	{
		return;		
	}

	// About to loop
	if (num ==  0 && activeImage() == totalImages()-1)
	{
		// Clone the first element and add to the end
		$('#scroll li:first').addClass('selected').clone().appendTo($('#scroll'));

		// Scroll the 'first' (cloned) element into the viewport
		$("#scroll").stop().animate({left:-(viewport*$('#scroll li').prevAll().length)},1000, 'easeInOutCubic', function()
		{
			// Move to the real first element instantly and remove the cloned element
			$("#scroll").stop().css({left:0}).find('li:last').remove();
			$(".projectImages #scroll li").removeClass('selected').eq(0).addClass('selected');
		});
	
	}
	else
	{	
		if (activeImage() == 0 && num == totalImages() -1)
		{
			// Clone the first element and add to the end
			$('#scroll li:last').addClass('selected').clone().prependTo($('#scroll'));

			// Scroll the 'first' (cloned) element into the viewport
			$("#scroll").css('left', -viewport).stop().animate({left:0},1000, 'easeInOutCubic', function()
			{
				// Move to the real first element instantly and remove the cloned element
				$("#scroll").stop().css({left:-(viewport*($('#scroll li').prevAll().length-1))}).find('li:first').remove();
				
				$(".projectImages #scroll li").removeClass('selected').eq(num).addClass('selected');
	
			});
		}
		else
		{
			// Width of the viewport and also the images in the portfolio slideshow pages.
			$("#scroll").stop().animate({left:-(viewport*num)},1000, 'easeInOutCubic');
		}
	}

	// Return array of all these a tags
	$(".projectImages #scroll li")
	
	// Remove .selected from all of them
	.removeClass('selected')
	
	// grab just the element at index[num]
	.eq(num)
	
	// Make this one selected
	.addClass('selected');
	
	var hidden = $("#hiddencontent li:eq("+num+")");

	$(".projectNav .imagereadmore").attr('href', $(".imagereadmore", hidden).attr("href"));
	$(".projectNav .imagereadmore").html($(".imagereadmore", hidden).html());
	$(".projectNav .imagereadmore").attr('title', $(".imagereadmore", hidden).attr("title"));

}

//timer for index page
function timescroll()
{
	current = activeImage() + 1;
	if (current >= totalImages())
	{
		current = 0;
	}
	if(totalImages() > 1)
	{
		slideshow(current);
	}
}

$(function(){


	// Bind click events too the buttons so that they update
	$('.projectNav ul li a').click(function(){

		//slideshow($(this).parent().prevAll().length);
		
		return false;
	});
	
	// Left clicker
	$('#controlleft').click(function(){
		var current = activeImage();
		if (current !== 0)
		{
			slideshow(current-1);
		}
		else if (current == 0)
		{
			var current = totalImages()-1;
			slideshow(current);
		}
		return false;
	});
	
	// Right clicker
	$('#controlright').click(function(){
		var current = activeImage();
		if (current !== totalImages()-1)
		{
			slideshow(current+1);
		}
		else if (current == totalImages()-1)
		{
			var current = 0;
			slideshow(current);
		}
		
		return false;
	});

});
