$(document).ready( function() {
	var pageLimit = 12;
	var totalImages = $("#image-gallery li").length;
	var totalPages = Math.ceil(totalImages / pageLimit);
	
	var currentPageNum = 1;
	var firstThumbOnPage = 0;
	var lastThumbOnPage = 11;


	// retrieves link from querystring and opens corresponfing lightbox
	linkID = gup("linkID");
	linkID = linkID.replace("%20", " ");
	 $("a[id='"+linkID+"']").trigger('click');

	
	// for links to pdfs go to confirm box first
	$("#image-gallery a.pdf").click(function() {
		var pdfPath = $(this).attr("href");
		downloadPdf(pdfPath);
		return false;
	});

	
	// 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 += 12;
			lastThumbOnPage += 12;
			currentPageNum += 1;
		}
		else if(direction == "backward") {
			firstThumbOnPage -= 12;
			lastThumbOnPage -= 12;
			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");
	});
	
});




// returns querystring value
function gup(name) {
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null )
	  return "";
	else
	  return results[1];
}


// ask for confirmation before allowing download of pdf
function downloadPdf(pdfPath) {
	var answer = confirm("By downloading this image you acknowledge that you have read and understood the Terms & Conditions limiting its usage")
	if (answer){
		window.open(pdfPath,'mywindow','width=500,height=500,toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,copyhistory=yes,resizable=yes');
	}
	else{
		//alert("Maybe next time");
	}
}
