var Gallery = {
	current_img: 0,
	total_imgs: null,
	animating: false,
	margin: 10,
	
	returnCurrentImageHeight: function() {
		return $('#galleryimages li:eq('+this.current_img+')').height() + parseInt($('#galleryimages li').css("margin-bottom"));
	},
	
	setCurrentImageNumber: function() {
		$('#galleryimages li').addClass("off");
		$('#galleryimages li:eq('+this.current_img+')').removeClass("off");
		$('p#gallerycontrols span').empty().append((this.current_img + 1) + '/' + (this.total_imgs + 1));
	},
	
	scrollNext: function() {
		if(this.current_img < this.total_imgs && !this.animating) {
			this.animating = true;
			this.current_img++;
			this.setCurrentImageNumber();
			
			this.animateScroll();
			
			if(this.current_img == this.total_imgs) {
				return "last";
			}
		}
	},
	
	scrollPrevious: function() {
		if(this.current_img > 0 && !this.animating) {
			this.animating = true;
			this.current_img--;
			this.setCurrentImageNumber();
			
			this.animateScroll();
			
			if(this.current_img == 0) {
				return "first";
			}
		}
	},
	
	animateScroll: function() {
		$('#galleryimages').animate({top: - (this.current_img) * this.returnCurrentImageHeight() }, "normal", function() { Gallery.animating = false; });
	}
}

// DOM ready Events
$(function () {
	// setup gallery
	Gallery.total_imgs = $('#galleryimages img').length - 1;
	$('a#back').css("visibility", "hidden");
	Gallery.setCurrentImageNumber();
	// events
	$('a#forward').click(function() {
		if(Gallery.scrollNext() == "last") {
			$(this).css("visibility", "hidden");
		} else {
			$('a#back').css("visibility", "visible");
		}
		return false;
	});
	$('a#back').click(function() {
		if(Gallery.scrollPrevious() == "first") {
			$(this).css("visibility", "hidden");
		} else {
			$('a#forward').css("visibility", "visible");
		}
		return false;
	});
});
