/**
 * @author Gorfdo
 */
var Fader = new Class({
		Implements: Options,
		options : {
			pause:5000,
			duration:2000,
			element:'.toppp',
			buttons:'.ppick',
			images:''
		},
		initialize: function(options){
			this.setOptions(options);
			this.fxbox = new Array;
			this.boxarray = new Array;
			this.clickbox = new Array;
			this.pactive = 0;
			this.newbox = '';
			this.addPhotos();
			this.addButtons();
			this.getPhotos();
			this.start();				
		},
		addPhotos: function(){
			$$(this.options.element).each(function(item){
				
				var boxid = item.id.split("-");
				//alert(item.id);
				this.boxarray.push(boxid[1]);
				this.fxbox[item.id] = new Fx.Morph(item.id,{duration:this.options.duration,link:'chain'});
			}.bind(this))
		},
		addButtons: function(){

			
		},
		getPhotos: function(){
				this.options.images.each(function(item,index){
					if(index > 1)
					{
						$('topimage-'+index).setProperty('src',item);
					}					
				})
		},
		start: function(){
			this.periodical = this.show.bind(this).periodical(this.options.pause);			
		},
		stop: function(){
			$clear(this.periodical);				
		},
		clickshow: function(clickbox){
			var box = clickbox.split("-")	
			this.stop();
			this.show(box[1]);
			this.start();
		},
		show: function(box){
			if(typeof box == 'undefined'){			
				this.newbox = this.pactive*1 + 1;
				if(this.newbox == this.boxarray.length) this.newbox = 0;	
			}
			if(typeof this.fxbox["p-"+this.boxarray[this.pactive]] != 'undefined')	this.fxbox['p-'+this.boxarray[this.pactive]].cancel();
			this.fxbox["p-"+this.boxarray[this.pactive]].start({
				opacity:[1,0]
				}).start({
					'display':'none'
					})
			this.fxbox['p-'+this.boxarray[this.newbox]].set({
						"opacity":0,"display":"block"
						}).start({
							opacity:[0,1]
						})
			this.pactive = this.newbox
			$$('.ppick.active').removeClass('active');
			
		
		}
	})
		