$(document).ready(function()
{
	//Number of pages
	var numpages = 5;
	
	//Variables
	var currentpage = 1;
	var timer;
	
	//Start automatically paging
	autoPage();
	
	//Switch to the next page
	$('#carouselNextPage').click(function()
	{
		//Stop automatic paging
		clearInterval(timer);
		
		//Switch the page
		changePage ('backward');
	});
	
	//Switch to the prev page
	$('#carouselPrevPage').click(function()
	{	
		//Stop automatic paging
		clearInterval(timer);
		
		//Switch the page
		changePage ('forward');
	});
	
	//Change the page to currentpage
	function changePage (direction) {
		current_page = $('.carouselPage_on');
		
		//Go forward to the next page or backwards to the prev
		if(direction == "forward"){
			//If there is a next page go to it, else go to the first page
			if(current_page.next('.carouselPage').length)
			{
				current_page.next('.carouselPage').addClass('carouselPage_on');
			}
			else
			{
				$('.carouselPage:first-child').addClass('carouselPage_on');
			}
		}
		else
		{
			//If there is a prev page go to it, else go to the last page
			if(current_page.prev('.carouselPage').length)
			{
				current_page.prev('.carouselPage').addClass('carouselPage_on');
			}
			else
			{
				$('.carouselPage:last-child').addClass('carouselPage_on');
			}
		}
		
		
		
		//Turn any pages that are on off
		current_page.removeClass('carouselPage_on');
	}	
	
	//Changes the pages on an interval
	function autoPage() {
		var intervalTime = 8000;
		
		if(numpages > 1)
		{
			timer = setInterval(function()
			{ 
				//Increment the current page
				++currentpage;
			
				//Switch the page
				changePage ();
			}
			,intervalTime
			);
		}
	}
})

