
(function($){
 	$(document).ready(function(){

		$('#pagination').click(function(){
			//? $(this).parent().children('a').click();
			document.location = $(this).parent().children('a').attr('href');
		});

		// cache this for use inside the 
		// pagination helpers
		var thumbs = $('#thumbs div.thumb');

		$('#pagination span').click(function(){
			var currentIndex = Number($('#preview').attr('showing')),
				 nextIndexMod = $(this).is('.next') ? 1 : -1,
				 nextIndex = currentIndex + nextIndexMod;

		   //protect against walking below bounds
			if (nextIndex < 0) 
				nextIndex = thumbs.length-1;

			showPreview(nextIndex % thumbs.length);
			return false;
		});

		thumbs.mouseover(function(){
			showPreview(thumbs.index(this));
		});

		function showPreview(n) {
			var thumb = thumbs.eq(n),
				 targetUrl = thumb.children('a').attr('href'),
				 previewUrl = thumb.find('a img').attr('src');

			$('#preview').children('a').attr('href', targetUrl);
			$('#preview').find('a img').attr('src', previewUrl.replace('max=50', 'size=xlg'));
			$('#preview').attr('showing', n);
		}

	});
 })(jQuery);
