(function(ns){
	ns.ImageTrans = new Class({
		Implements:[Events,Options],
		options : {
			images : [],
			container : '',
			delay : 2000,
			styles : ''
		},
		initialize : function(options){
			this.setOptions(options);
			this.images = this.options.images;
			this.container = $(this.options.container) || document.body;
			this.img = new Element('img',{
				styles : this.options.styles
				}
			);
			this.img1 =  new Element('img',{'border':'0','alt':'',
				styles : this.options.styles});
			this.num = 0;
			this.timer = null;
			this.create();
			this.selectedItem = null;
		},
		create : function(){
			this.img.src = this.images[this.num];
			this.img1.src = this.images[this.num];
			this.img1.setStyle('opacity','0');
			this.container.appendChild(this.img);
			this.container.appendChild(this.img1);
			
			this.fx = new Fx.Style(this.img,'opacity',{wait: false, duration: this.options.delay});
			this.fx1 = new Fx.Style(this.img1,'opacity',{wait: false, duration: this.options.delay});
			this.trans();
			window.addEvent('blur',function(){clearTimeout(this.timer);}.bind(this));
			window.addEvent('focus',function(){this.trans()}.bind(this));
		},
		trans : function(){
			this.timer = this.changImage.delay(this.options.delay,this);
		},
		getNext : function(){
			this.img.src = this.images[this.num];
			this.img.set('opacity', 1);
			if(++ this.num >= this.images.length)  this.num = 0;
			this.img1.src = this.images[this.num];
			this.img1.set('opacity', 0);
			return this;
		},
		changImage : function(){
			var s = this.getNext();
			this.fx.start(1, 0);
			this.fx1.start(0,1).chain(s.trans.bind(s));
		}
	});

})(window);
