var ProtoFlash=Class.create ({
	initialize: function(elem,opt) {
		opt = opt || {};
		this.options = {
			interval: 4,
			animSpeed: 1,
			width: 222,
			height: 166,
			onClick: null
		};
		
		Object.extend(this.options, opt);
		
		this.elem = $(elem);
		if (!this.elem) return;
		
		this.imgStack=this.elem.select("img");
		this.imgCount=this.imgStack.length-1;
		this.imgAnim=0;
		
		this.imgStack.each(function(elem){
			var d=new Element("div", { 'style':'position: absolute; display: none;' });
			d.appendChild(elem);
			this.elem.appendChild(d);
			
			if (this.options.onClick) {
				elem.style.cursor='pointer';
				elem.onclick=this.options.onClick;
			}
		}.bind(this));
		
		new Effect.Appear( this.imgStack[this.imgAnim].parentNode, {
			from: 0,
			to: 1,
			duration: this.options.animSpeed,
			afterFinish: function() {
				new PeriodicalExecuter(function(pe) {
					this.canviImatge();
 					pe.stop();
				}.bind(this), this.options.interval);
			}.bind(this)
		});
		
		this.elem.style.width=this.options.width+"px";
		this.elem.style.height=this.options.height+"px";
		this.elem.show();
	},
	
	canviImatge: function() {
		new Effect.Fade(this.imgStack[this.imgAnim].parentNode, {
			from: 1,
			to: 0,
			duration: this.options.animSpeed
		});
		
		if (this.imgAnim==this.imgCount) {
			this.imgAnim=0;
		} else {
			this.imgAnim++;
		}
		
		new Effect.Appear( this.imgStack[this.imgAnim].parentNode, {
			from: 0,
			to: 1,
			duration: this.options.animSpeed,
			afterFinish: function() {
				new PeriodicalExecuter(function(pe) {
					this.canviImatge();
 					pe.stop();
				}.bind(this), this.options.interval);
			}.bind(this)
		});
	}
});
