// get current active image number
function activeImage2()
	{
	return $(".projectImages2 #scroll2 li.selected").prevAll().length;
}

//get total number of images
function totalImages2(){
	return $('.projectImages2 #scroll2 li').length;
}

//does the sliding
function slideshow2(num2)
{

	if ($('#scroll2:animated').length)
	{
		return;
	}
	
	if ($('#scroll:animated').length)
	{
		return;
	}	
	var viewport = 360;
	// About to loop
	if (num2 ==  0 && activeImage2() == totalImages2()-1)
	{
		// Clone the first element and add to the end
		$('#scroll2 li:first').addClass('selected').clone().appendTo($('#scroll2'));

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

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

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

	$(".projectNav2 .imagereadmore2").attr('href', $(".imagereadmore2", hidden).attr("href"));
	$(".projectNav2 .imagereadmore2").html($(".imagereadmore2", hidden).html());
	$(".projectNav2 .imagereadmore2").attr('title', $(".imagereadmore2", hidden).attr("title"));

}

//timer for index page
function timescroll2()
{
	current2 = activeImage2() + 1;
	if (current2 >= totalImages2())
	{
		current2 = 0;
	}
	if(totalImages2() > 1)
	{
		slideshow2(current2);
	}
}

$(function(){


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

		//slideshow($(this).parent().prevAll().length);
		
		return false;
	});
	
	// Left clicker
	$('#controlleft2').click(function(){
		var current2 = activeImage2();
		if (current2 !== 0)
		{
			slideshow2(current2-1);
		}
		else if (current2 == 0)
		{
			var current2 = totalImages2()-1;
			slideshow2(current2);
		}
		return false;
	});
	
	// Right clicker
	$('#controlright2').click(function(){
		var current2 = activeImage2();
		if (current2 !== totalImages2()-1)
		{
			slideshow2(current2+1);
		}
		else if (current2 == totalImages2()-1)
		{
			var current2 = 0;
			slideshow2(current2);
		}
		
		return false;
	});

});