$(document).ready(function() { // on page load
	$('.gallery_next').click(function(e) { e.preventDefault(); showPhoto(photoAfter(currentPhoto));$(this).css({textDecoration:'none',cursor:'default',color:'#999'}); return false; });
	$('.gallery_previous').click(function(e) { e.preventDefault(); showPhoto(photoBefore(currentPhoto));$(this).css({textDecoration:'none',cursor:'default',color:'#999'}); return false; });
	$('.gallery_thumb a').click(function(e) { e.preventDefault(); showPhoto(parseInt($(this).children().eq(0).attr('id').replace('img',''))); return false; });
	if($('#sidebar').length) { // if there's a sidebar, so there's no room for the photo next to the thumbs
		$('#gallery_image').css({float:'none'}); // bump the thumbs below the photo
	}
	if($('.gallery_thumb').length>15) { // if there are more than 15 thumbs
		$('#gallery_thumbnails').after('<div id="gallery_thumbpages"><div id="gallery_slider"><ul class="gallery_thumbnails"/></div></div>');
		if($('#sidebar').length) { // if there's a sidebar
			$('#gallery_thumbpages').css({marginTop:0,marginRight:0}); // set the thumb panel margin appropriately
		}
		$('.gallery_thumb').each(function() { // paginate the thumbs
			var page = $('.gallery_thumbnails:last');
			if(page.children().length==15) {
				page = $('<ul class="gallery_thumbnails"/>').appendTo('#gallery_slider');
				page.css('left',480*($('.gallery_thumbnails').length-1));
			}
			page.append(this);
		});
		$('#gallery_thumbnails').remove();
		$('#gallery_thumbpages').after('<div id="gallery_thumbnav">Page: </div>');
		for(var i = 0;i<$('.gallery_thumbnails').length;i++) {
			$('<a href="#">'+(i+1)+'</a>').data('id',i).appendTo('#gallery_thumbnav');
		}
		$('#gallery_thumbnav a').click(function(e) {
			e.preventDefault();
			$('.gallery_thumbnails').eq($(this).data('id')).find('a:first').click();
		});
		$('.gallery_thumb a.selected').click();
	}
});


function showPhoto(newPhoto) {
	$('#gallery_image').prepend('<div id="nextlayer"><div class="gallery_nav"><a href="?id='+galleryid+'&p='+photoBefore(newPhoto)+'" class="gallery_previous">&laquo; Previous</a><a href="?id='+galleryid+'&p='+photoAfter(newPhoto)+'" class="gallery_next">Next &raquo;</a></div><img src="/generated/'+photolocation+'/'+galleryphotos[newPhoto].id+'_lg.jpg" alt="Gallery Photo"/><div class="gallery_caption"><p>'+(galleryphotos[newPhoto].caption ? galleryphotos[newPhoto].caption : '')+'</p></div></div>');
	$('#nextlayer img').one('load',function() {
		var newHeight = $('#nextlayer').height();
		var newWidth = $('#nextlayer').width();
		if(photoAfter(newPhoto)===false) $('#nextlayer .gallery_next').remove();
		if(photoBefore(newPhoto)===false) $('#nextlayer .gallery_previous').remove();
		$('#nextlayer').css({width:'100%',height:'100%',visibility:'visible',display:'none'}).fadeIn(400,function() {
			$('#gallery_image').html($('#nextlayer').html());
			$('#gallery_image .gallery_next').click(function() { showPhoto(photoAfter(newPhoto));$(this).css({textDecoration:'none',cursor:'default',color:'#999'}); return false; });
			$('#gallery_image .gallery_previous').click(function() { showPhoto(photoBefore(newPhoto));$(this).css({textDecoration:'none',cursor:'default',color:'#999'}); return false; });
		});
		$('#gallery_image').css({height:newHeight+'px'});
	});
	$('#img'+currentPhoto).parent().removeClass('selected');
	$('#img'+newPhoto).parent().addClass('selected');
	if($('.gallery_thumb').length>15) { // if there are more than 15 thumbs
		var page = $('.gallery_thumb .selected').parents('.gallery_thumbnails').eq(0);
		$('#gallery_slider').animate({left:-1*page.position().left},500);
		$('#gallery_thumbnav a').removeClass('active');
		$('#gallery_thumbnav a').eq(page.index()).addClass('active');
	}
	currentPhoto = newPhoto;
}

function photoAfter(thisPhoto) { // given a photo position, returns the next photo
	if(thisPhoto+1>=galleryphotos.length) return false;
	else return (thisPhoto+1);
}

function photoBefore(thisPhoto) {
	if(thisPhoto-1<0) return false;
	else return (thisPhoto-1);
}
