StrozzisGalleryOptions = Object.extend({
	container: 'background'
}, window.StrozzisGalleryOptions || {});

var StrozzisGallery = Class.create();

StrozzisGallery.prototype = {
	container: null,
	options: null,
	stack: $H(),
	moving: false,
	currentimage: null,
	initialize: function(el, options)
	{
		this.options = Object.extend(StrozzisGalleryOptions, (options || {}));
		this.options.container = $(this.options.container);
		this.container = el;
		this.container.select('a').each(function(el) {
			el.observe('click',  this.onImageClick.bind(this));
		}, this);
	},
	onImageClick: function(event)
	{
		if (this.moving) {
			event.stop();
			return;
		}
		var img = Event.element(event).up('a').readAttribute('href');
		this.moving = true;
		if (!(this.currentimage = this.stack.get(img))) {
			var imgPreloader = new Image();
			imgPreloader.onload = function() {

				i = $(this.options.container.appendChild(this.currentimage));
				this.options.container.setStyle({'width': (this.options.container.getWidth()*1+i.getWidth()*1)+'px'});
				this.stack.set(img, i);
				this.currentimage = i;
			
				this.applyEffect();
			}.bind(this);
			imgPreloader.src = img;
			this.currentimage = imgPreloader;
			
		} else {
			this.applyEffect();
		}
		event.stop();
	},
	applyEffect: function()
	{
		var i = this.currentimage;
		new Effect.Move(this.options.container, {
			duration: 1.0,
			x:-1*i.positionedOffset().left, 
			mode: 'absolute',
			transition: Effect.Transitions.sinoidal,
			afterFinish: function() { this.moving = false; }.bind(this)
		});
	}
};

