var ImgShow = Class.create();

ImgShow.prototype = {
	initialize: function(element, options)
	{
		this.element = $(element);
		this.options = Object.extend(options);
		this.elems = $A(document.getElementsByClassName(this.options.className, this.element));
		
		this.prepare();
		this.registerCallback();
	},
	
	prepare: function()
	{
		this.currentnode = this.elems.first();
		this.element.style.position = 'relative';
		this.element.style.height = this.elems.max(function(node)
		{
			var visible = Element.visible(node), height;
			Element.setStyle(node, {position: 'absolute', left: '0px'});
			if (!visible) Element.show(node);
			height = Element.getHeight(node);
			Element.hide(node);
			return height;
		}).toString() + 'px';
		Element.show(this.currentnode);
	},
	
	nextnode: function()
	{
		return this.elems[(this.elems.indexOf(this.currentnode) + 1) % this.elems.length];
	},
	
	registerCallback: function()
	{
		window.setTimeout(this.tick.bind(this), this.options.duration * 1000);
	},
	
	tick: function()
	{
		var currentnode = this.currentnode, nextnode = this.nextnode();
		
		new Effect.Parallel([
			new Effect.Fade(currentnode, {sync: true}),
			new Effect.Appear(nextnode, {sync: true})
		], {
			duration: 2,
			afterFinish: (function(effect) {
				this.currentnode = nextnode;
				this.registerCallback();
			}).bind(this)
		})
	}
}
