// Javascript slideshow
// Delete slideshow js version
// @author SpoonKid Julien French Baguette

$(document).ready(function() {
	/*--- Public var, set up if needed-----------------------------------------------*/
	var chrono						= 3000;
	var sliderList					= $('#viewport').children(); // children()[0];
	var bt							= $('#playpause');
	var btNext						= $('#next');
	var btPrev						= $('#prev');

	/*---Internal var-----------------------------------------------------------------------------*/
	var increment					= $(sliderList).children().outerWidth("true");
	var elmnts						= $(sliderList).children();
	var numElmts		 			= elmnts.length;
	var sizeFirstElmnt				= increment;
	var shownInViewport 			= Math.round($(this).width() / sizeFirstElmnt);
	var firstElementOnViewPort 		= 1;
	var isAnimating 				= false;
	var playpause					= true;
	var timer;

	function init()
	{
		for (i = 0; i < shownInViewport; i++)
		{
			$(sliderList).css('width',(numElmts+shownInViewport)*increment + increment + "px");
			$(sliderList).append($(elmnts[i]).clone());
		}
		
		timerNexSlide();
		//Initialize the linkChange function to get correct URL on first element
		updateLink();
		playpauseBt (bt);
		clickNext(btNext);
		clickPrev(btPrev);
	}
	
	function updateLink()
	{
		var weblink = $('.link');
		$("#link-container").attr( 'href', weblink[firstElementOnViewPort-1].href);
		
		// Set the URL of the button to the correct one
		var namelink = $('#viewport ul li img');
		$('#launch-site').text(namelink[firstElementOnViewPort-1].alt);
	}
	
	function playpauseBt (target)
	{
		target.click(function(event)
		{
			if (playpause)
				playpause = false,
				clearTimeout(timer),
				bt.css({'background-image': 'url(images/slideshow/asset/play.jpg)'});
			else
				playpause = true,
				bt.css({'background-image': 'url(images/slideshow/asset/pause.jpg)'}),
				nextSlide();
		})
	}
	
	/*------ assign Buttons to enable actions ------------------------------------------*/
	function clickNext(target)
	{
		target.click(function(event)
		{
			clearTimeout(timer)
			nextSlide();
		})
	}
	
	function clickPrev(target)
	{
		target.click(function(event)
		{
			clearTimeout(timer)
			prevSlide();
		})
	}
				   
	/*--- Internal Function ------------------------------------------ */
	function nextSlide()
	{
		if (!isAnimating) 
		{
			if (firstElementOnViewPort > numElmts) 
			{
				firstElementOnViewPort = 2;
				$(sliderList).css('left', "0px");
			}
			else 
			{
				firstElementOnViewPort++;
			}
			
			updateLink();
			$(sliderList).animate({left: "-=" + increment,y: 0,queue: true }, "slow	", function(){isAnimating = false;});
			isAnimating = true;
			
		}
		
		timerNexSlide ();
	}
	
	function prevSlide()
	{
		if (!isAnimating)
		{
			if (firstElementOnViewPort == 1)
			{
				$(sliderList).css('left', "-" + numElmts * sizeFirstElmnt + "px");
				firstElementOnViewPort = numElmts;
			}
			else
			{
				firstElementOnViewPort--;
			}
			
			updateLink();
			$(sliderList).animate({left: "+=" + increment, y: 0, queue: true }, "slow", function(){isAnimating = false;});
			
			isAnimating = true;
			
		}
		
		timerNexSlide ();
	}
	
	/*--- Trigger the Slide animation ------------------------------------------*/
	function timerNexSlide ()
	{
		if(playpause){ timer = setTimeout(nextSlide, chrono)};
	}
	
	init();
// Ends Javascript slideshow

});
