function Gallery(options) {
	this.images = options.images;
	this.thumbnailContainer = j(options.thumbnailContainer);
	this.imagesContainer = j(options.imagesContainer);
	this.descriptionContainer = j(options.descriptionContainer);
	this.nextImageButtonElement = j(options.nextImageButtonElement);
	this.prevImageButtonElement = j(options.prevImageButtonElement);
	this.nextThumbsButtonElement = j(options.nextThumbsButtonElement);
	this.prevThumbsButtonElement = j(options.prevThumbsButtonElement);
	this.imageWidth = options.imageWidth;
	this.thumbnailWidth = options.thumbnailWidth;
	this.thumbnailsVisible = options.thumbnailsVisible;
	
	this.currentImage = 0;
	this.currentThumbs = 0;

	this.render();
	this.addEventHandlers();
	this.displayCurrentImage();
}

Gallery.prototype.addEventHandlers = function() {
	this.nextImageButtonElement.bind('click', {self: this}, function(event) {
		event.data.self.nextImage();
		return false;
	});	
	this.prevImageButtonElement.bind('click', {self: this}, function(event) {
		event.data.self.prevImage();
		return false;
	});	

	this.nextThumbsButtonElement.bind('click', {self: this}, function(event) {
		event.data.self.nextThumbs();
		return false;
	});	
	this.prevThumbsButtonElement.bind('click', {self: this}, function(event) {
		event.data.self.prevThumbs();
		return false;
	});	

};

Gallery.prototype.getLeftValueForThumbs = function(thumbsIndex) {
	return (-thumbsIndex * this.thumbnailWidth) + 'px';
};

Gallery.prototype.animateCurrentThumbsIntoView = function() {
	this.thumbnailContainer.animate({
		left: this.getLeftValueForThumbs(this.currentThumbs)
	}, 1000);
};



Gallery.prototype.nextThumbs = function() {
	if (this.currentThumbs < this.images.length - this.thumbnailsVisible) {
		this.currentThumbs += this.thumbnailsVisible;
		this.animateCurrentThumbsIntoView();
	}
};

Gallery.prototype.prevThumbs = function() {
	if (this.currentThumbs > 0 ) {
		this.currentThumbs -= this.thumbnailsVisible;
		this.animateCurrentThumbsIntoView();
	}
};

Gallery.prototype.setCurrentImage = function(imageIndex) {
	this.currentImage = imageIndex;
	this.displayCurrentImage();
};

Gallery.prototype.nextImage = function() {
	if (this.currentImage < this.images.length - 1) {
		this.currentImage++;
		this.animateCurrentImageIntoView();
	}
};

Gallery.prototype.prevImage = function() {
	if (this.currentImage > 0 ) {
		this.currentImage--;
		this.animateCurrentImageIntoView();
	}
};

Gallery.prototype.getLeftValueForImage = function(imageIndex) {
	return (-imageIndex * this.imageWidth) + 'px';
};

Gallery.prototype.animateCurrentImageIntoView = function() {
	this.imagesContainer.animate({
		left: this.getLeftValueForImage(this.currentImage)
	}, 1000);
	this.displayCurrentImageDescription();
};

Gallery.prototype.displayCurrentImage = function() {
	var targetLeft = this.getLeftValueForImage(this.currentImage);
	if (targetLeft != this.imagesContainer.css('left')) {
		this.imagesContainer.fadeOut(250);
		var self = this;
		this.imagesContainer.queue(function() {
			j(this).css('left', targetLeft);
			self.displayCurrentImageDescription();
			j(this).dequeue();
		});
		this.imagesContainer.fadeIn(250);
	} else {
		this.displayCurrentImageDescription();
	}
};

Gallery.prototype.displayCurrentImageDescription = function() {
	this.descriptionContainer.text(this.images[this.currentImage].desc);
	
};

Gallery.prototype.render = function() {
	for (var i = 0; i < this.images.length; i++) {
		var img = this.images[i];

		var thumbHtml = '<a><img src="/' + img.thumb + '"></a>';
		img.thumbEl = j(thumbHtml).appendTo(this.thumbnailContainer);
		img.thumbEl.attr('title', img.name);
		img.thumbEl.bind('click', {imageIndex: i, self:this}, function(event) {
			event.data.self.setCurrentImage(event.data.imageIndex);
			return false;
		});
		
		var imageHtml = '<div class="imageContainer"><img src="/' + img.large + '"></div>';
		img.imageEl = j(imageHtml).appendTo(this.imagesContainer);
		img.imageEl.attr('title', img.name);
	}	
};
