$(document).ready(function() {
	var animatespeed = 600;
	var eachWidth = $('#divPhotoThumbs div.photoColumn:first').width();
	$('.moreLinkRight span,.moreLinkLeft span').hide(); // hide the text with javascript.
	
	$('span#pageNumber').html('Page <span id="currentNumber">1</span> of <span id="totalNumber">1</span>').css({'margin':'15px 0 0','font-size':'11px'});;
	$('div#pagerButtons').html('<div class="pagerRight"><p class="moreLinkLeft"><span>&laquo; More</span></p></div><div class="pagerLeft"><p class="moreLinkRight"><span>More &raquo;</span></p></div>');
	$('div#pagerButtons p.moreLinkLeft').hide();
	
	totalPages = $('#divPhotoThumbs div.photoColumn').size();
	
	var position = 0;
	var itemNumber = 1;
	
	$('#pageNumber #totalNumber').text(totalPages);
	
	$('#divPhotoThumbs div.photoColumn').each(function() {
		$(this).css({'float':'left','width':'250px'});
	});
	
	var theSize = parseInt(eachWidth * totalPages);
	
	// Sets the current page number and pager forward / backward buttons:
	function updatePager(num) {
		$('#pageNumber #currentNumber').text(num);
	}
	
	function updatePrevNext(num) {
		if (num == 1) {
			//alert('num == 1');
			$('#pagerButtons p.moreLinkLeft').fadeOut(100);
			$('#pagerButtons p.moreLinkRight').fadeIn();
		} else if (num == totalPages) {
			//alert('num == totalPages');
			$('#pagerButtons p.moreLinkRight').fadeOut(100);
			$('#pagerButtons p.moreLinkLeft').fadeIn();
		} else if (num < totalPages) {
			//alert('num < totalPages');
			$('#pagerButtons p.moreLinkLeft,#pagerButtons p.moreLinkRight').fadeIn();
		}
	}
	
	
	//alert(theSize);
	$('#divPhotoThumbs div.thumbs').width(theSize + 'px');
	
	$('p.moreLinkRight').click(function(event) {
		position = position + eachWidth;
		itemNumber++;
		updatePrevNext(itemNumber);
		$('.thumbs').animate({'marginLeft':'-' + position}, animatespeed,
			function() {
				updatePager(itemNumber);
			}
		);
	});
	
	$('p.moreLinkLeft').click(function() {
		position = position - eachWidth;
		itemNumber--;
		if (position <= 0) {
			updatePrevNext(itemNumber);
			$('.thumbs').animate({'marginLeft':'0'}, animatespeed,
				function() {
					updatePager(itemNumber);
				}
			);
		} else {
			updatePrevNext(itemNumber);
			$('.thumbs').animate({'marginLeft':'-' + position}, animatespeed,
				function() {
					updatePager(itemNumber);
				}
			);
		}
	});					
	
	$('.moreLinkRight,.moreLinkLeft').hover(function() {
		$(this).css('background-color','#d02e14');
	}, function() {
		$(this).css('background-color','#fff');
	});
});