
window.addEvent('domready', function(e) {
	var largeImage = $$('#lgImage img');
	if (largeImage.length > 0) {
		new jp_ImageSwapper(largeImage[0], $$('#gallery ul a'));
	}
});


var jp_ImageSwapper = new Class({
	mainImage: null,
	thumbs: [],
	initialize: function(mainImage, thumbs) {
		this.mainImage = $(mainImage);
		if (typeof thumbs.length != 'undefined') {
			thumbs.each(function(e) {
				e = $(e);
				if (e) {
					this.thumbs.push(e);
					new Image().src = e.get('href'); //preload in cache
				}
			}, this);
		}
		if (!this.mainImage || this.mainImage.tagName.toLowerCase() != 'img') {
			return;
		}
		this.setEvents();
	},
	setEvents: function() {
		this.thumbs.each(function(e) {
			e.addEvent('click', function(evt) {
				evt.stop();
				this.swap(e.get('href'));
			}.bindWithEvent(this));
		}.bind(this));
	},
	swap: function(src) {
		this.mainImage.set('src', src);
	}
});
