
function Gallery() {
	this.images = [];
	this.currentImage = 0;
	this.cycle = false;
	this.showHovers = true;
}

Gallery.prototype = {
	
	addImage: function(src, alt, title, link) {
		this.images.push([src, alt, title, link]);
	},

	init: function() {
		if (this.showHovers) {
			$('gallery_hover_next').onclick = this.nextImage.bind(this);
			$('gallery_hover_prev').onclick = this.prevImage.bind(this);
		} else {
		//	if ($('gallery_hover_next')) $('gallery_hover_next').hide();
		//	if ($('gallery_hover_prev')) $('gallery_hover_prev').hide();
		}
			
		for (var c=0; c<this.images.length; c++) {
			$('gallery_nav_'+c).onclick = this.showImage.bind(this, c);
		}
	},

	showImage: function(c) {
		this.currentImage = c;
		if (!this.cycle) {
			if (this.images.length > 1) {
				if (this.showHovers) {
					if (this.currentImage == 0) $('gallery_hover_prev').hide();
					else $('gallery_hover_prev').show();
					if (this.currentImage+1 == this.images.length) $('gallery_hover_next').hide();
					else $('gallery_hover_next').show();
				}
			}
		}
		
		var image = this.images[c];
		
		$('gallery_img').src = image[0];
		if ($('gallery_caption')) {
			$('gallery_caption').update(image[2]);
		}
		
		$('gallery_img').alt = image[1];
		$('gallery_img').title = image[2];
		
		if (image[3] == '') {
			$('gallery_more').hide();
		} else {
			$('gallery_more').show();
			$('gallery_more').childNodes[0].href = image[3];
		}
		
		$$('#gallery_nav a').each(function (s) { s.removeClassName('active'); });
		$('gallery_nav_'+c).addClassName('active');
		return false;
	},
	
	nextImage: function() {
		if (!this.cycle) {
			if (this.currentImage+1 == this.images.length) return false;
			return this.showImage(this.currentImage+1);
		} 
		return this.showImage((this.currentImage + 1) % this.images.length);
	},

	prevImage: function() {
		if (!this.cycle) {
			if (this.currentImage == 0) return false;
			return this.showImage(this.currentImage-1);
		} 
		return this.showImage((this.images.length + this.currentImage - 1) % this.images.length);
	}
}


