/**
 * @author Gorfdo
 */
var Fader = new Class({
		Implements: Options,
		options : {
			pause:6000,
			duration:3000,
			element:'.topimage',
			images:''
		},
		initialize: function(options){

			this.setOptions(options);
			this.running = false;
			this.once = false;
			this.imagelist = new Array;
			this.fxbox = new Array;
			this.myimage = new Array;
			this.boxarray = new Array;
			this.pactive = 0;
			this.next = 0;
			this.addPhotos();			
		},
		addPhotos: function(){	
			this.myimage = new Asset.images(this.options.images,{
				onProgress : function(index,cnt){
					
					this.imagelist[index] = $('topimage0').clone().setProperty('src' , this.myimage[index].src).setProperty('id','p-'+index).setStyle('invisibility','hidden').injectBefore('topimage0');		
					this.fxbox[index] = new Fx.Morph('p-'+index,{duration:this.options.duration,link:'chain'});
					this.boxarray.push(index);
					if(cnt == 2)
					{
						this.firstfade();
						this.running = true;
					}
				}.bind(this)
			});
			},
			
			start: function(){
				this.periodical = this.show.bind(this).periodical(this.options.pause);			
			},
			stop: function(){
				$clear(this.periodical);				
			
			},
			firstfade : function(){
				this.fxbox[this.boxarray[this.pactive]].start({opacity:[0,1]});
				new Fx.Morph('topimage0',{duration:this.options.duration,link:'chain'}).start({'opacity':[1,0]}).start({'display':'none'}).chain(this.start());

				this.pactive++;
			},
			show: function(){		
			this.next = this.pactive + 1;
			if(this.next == this.boxarray.length) this.next = 0
			this.fxbox[this.boxarray[this.next]].start({opacity:[1,0]})
			this.fxbox[this.boxarray[this.pactive]].start({opacity:[0,1]})
			this.pactive = this.next;
		}

	})
		