$(document).ready( function() {
	var pageLimit = 10;
	var totalImages = $("#image-gallery li").length;
	var totalPages = Math.ceil(totalImages / pageLimit);
	
	var currentPageNum = 1;
	var firstThumbOnPage = 0;
	var lastThumbOnPage = 9;


	// on load lets set the gallery up
	updatePage();
	setPageTotal();
	
		
	// writes the total number of pages in to paging
	function setPageTotal() {		
		$("#gallery-paging #total").text(totalPages);
	}



	// remove or adds back and next links
	function checkLinks() {
		
		
		if (totalImages == pageLimit)
		{						
			$(".btnNext").hide();
			$(".btnBack").hide();
		}
		else if (lastThumbOnPage >= totalImages && totalImages <= pageLimit) {
			//alert('1');
			$(".btnNext").hide();
			$(".btnBack").hide();
		}

		else if (lastThumbOnPage >= totalImages) {
			//alert('2');
			$(".btnNext").hide();
			$(".btnBack").show();
		}

		else if (firstThumbOnPage <= 0) {
			//alert('3');
			$(".btnBack").hide();
			$(".btnNext").show();
		}		
		else {
			//alert('4');
			$(".btnBack").show();
			$(".btnNext").show();
		}
	}


	// sets the current page number
	function updatePageNumber(currentPageNum) {
		$("#gallery-paging #current").text(currentPageNum);		
	}	

	// update the page with new thumbnails
	function updatePage(direction) {
		$("#image-gallery > li").hide();

		if(direction == "forward") {
			firstThumbOnPage += 10;
			lastThumbOnPage += 10;
			currentPageNum += 1;
		}
		else if(direction == "backward") {
			firstThumbOnPage -= 10;
			lastThumbOnPage -= 10;
			currentPageNum -= 1;
		}

		else {
			//this is for the first time
		}

		// show all the thumbs for page
		for (i=firstThumbOnPage; i<=lastThumbOnPage; i++) {		
			$("#image-gallery > li:eq("+i+")").show();
		}
		
		
		
		updatePageNumber(currentPageNum);
		checkLinks();
		
	}


	// bind function to 'next' button
	$(".btnNext").click(function() {
		updatePage("forward");
	});


	// bind function to 'back' button
	$(".btnBack").click(function() {
		updatePage("backward");
	});
	
	
	
});




