/* Author: 

*/

function slideShow() {
    var current = $('#header-slides img.active');
    var next = current.next().length ? current.next(): current.parent().children(':first'); 
    current.fadeOut(1000).removeClass('active');
    next.fadeIn(1000).addClass('active');  
    setTimeout(slideShow, 5000);
}

$(function() {

	$('#header-slides img').hide();
	
    slideShow();
    
    $('a.arrow').click(function(e){
    	e.preventDefault();
    	var url = $(this).attr('href');
    	$(this).animate({
    	marginLeft:'400px',
    	opacity:0
    	}, 1000, 'swing', function(){window.location = url;});
    	
    })
    
});



// Move Slides Left
function goLeft(){
	$("#featured-slides li:first-child").stop(true,false).animate(
		{
			marginLeft : '-=605px'
		}, 600, function(){
		$("#featured-slides li:first-child").appendTo("#featured-slides").css({marginLeft : "1px"});
		})
}


// Move Slides Right
function goRight(){
	$("#featured-slides li:last").prependTo("#featured-slides").css({marginLeft : "-605px"});
	
		$("#featured-slides li:first-child").stop(true,false).animate(
		{
		marginLeft : '+=605px'
		}, 600 )
}



var $keys = "false";


$(document).ready(function()
{	

	//Auto running slides
	window.setInterval(function() {
	
		if ($keys == "false") // Checks to make sure the keys have not been recently used
		{
	 		goLeft();
	 	} 
	 	else if($keys == "true") // If keys have been used
	 	{
	 		$keys = "false"; // Sets the value to false and will run next time around
	 	}
	}, 5000);
	
	
	// Keyboard Controls

	$(document).keydown(function(e) 
	{	// Left key pressed	
	
			$keys = "true";	// Sets the keys variable to stop the auto running interfering
		if (e.keyCode == 37) 
		{	
    		goLeft();		
		}
	
		// Right key pressed		
		else if (e.keyCode == 39) 
		{	
			goRight();		
		}
	})


})


















