//	DSlider jQuery slider.						//
//	Visit Dimics.com for the tutorial about this slider.	//

$(document).ready(function(){
	var slides = $(".slide").length;	
	var slideWidth = $(".slide").outerWidth();
	var currentSlide = 1;	
	var leftMax = slideWidth * -1 * slides + slideWidth;	
	var timeVar;		//timer variable
	
	$("#slides").css({"width" : slides * slideWidth});
	
	//function: animate to previous slide
	function Prev() {
		if (currentSlide == 1)				
		{
			$("#slides").animate({left : + leftMax + "px"}, 600);
			currentSlide = slides;								
		}
		else
		{
			$("#slides").animate({left : "+=" + slideWidth + "px"}, 600);
			currentSlide = currentSlide - 1;				
		};
	};
	
	//function: animate to next slide
	function Next() {
		if (currentSlide == slides)			
		{
			$("#slides").animate({left : "0px"}, 600);		
			currentSlide = 1;	
		}
		else
		{
			$("#slides").animate({left : "-=" + slideWidth + "px"}, 600);
			currentSlide = currentSlide + 1;
		};
	};
	
	$("#buttonPrev").click(function(){	
		Prev();		//when previous button is pressed run function Prev
	});
	
	$("#buttonNext").click(function(){			
		Next();		//when next button is pressed run function Next
	});
	
	//function: stop timer on hover
	$("#slider").hover(function(){
		clearTimeout(timeVar);		//when hover over #slider stop timer
	}, function() {
		Timer();		//after hover start timer again
	});
	
	//function: timer
	function Timer(){
		timeVar = setTimeout(function(){
			Next();		//animate after the specified time
			Timer();		//run timer again so it will create a loop
		}, 7000);		//time to delay in milliseconds
	};
	
	Timer();		//start the timer function the first time
	
	$(document).keydown(function(event){		//when a keyboardbutton is pressed, run an function and pass thru the "id" or unic number of that button
		switch(event.keyCode){		//switch on the id of the pressed button
			case 37:		//check it's the left arrow, and if animate
				Prev();
				break;
			case 39:		//check if right arrow, and if animate
				Next();
				break;
		};
		
	});
});
