//	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});
	
	

				
				$('.boxgrid.captionfull').hover(function(){
					$(".cover", this).stop().animate({bottom:'100px'},{queue:false,duration:150});
				}, function() {
					$(".cover", this).stop().animate({bottom:'0px'},{queue:false,duration:150});
				});
				
			
	
	//function: animate to previous slide
	function Prev() {
		if (currentSlide == 1)				
		{
			$("#slides").animate({left : + leftMax + "px"}, 550);
			currentSlide = slides;								
		}
		else
		{
			$("#slides").animate({left : "+=" + slideWidth + "px"}, 550);
			currentSlide = currentSlide - 1;				
		};
	};
	
	//function: animate to next slide
	function Next() {
		if (currentSlide == slides)			
		{
			$("#slides").animate({left : "0px"}, 550);		
			currentSlide = 1;	
		}
		else
		{
			$("#slides").animate({left : "-=" + slideWidth + "px"}, 550);
			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
	$("#latest").hover(function(){
		clearTimeout(timeVar);		//when hover over #slider stop timer
	}, function() {
		Timer();		//after hover start timer again
	});
	
	$("#buttonNext").hover(function(){
		clearTimeout(timeVar);		//when hover over #slider stop timer
	}, function() {
		Timer();		//after hover start timer again
	});
	
	$("#buttonPrev").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
		}, 10000);		//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();
		clearTimeout(timeVar);		//when hover over #slider stop timer
				break;
				
			case 39:		//check if right arrow, and if animate
				Next();
		clearTimeout(timeVar);		//when hover over #slider stop timer
				break;
				
		};
		
	});
});