var slideShow = new Class({
	options: {
		slidetime: 1.2,
		swaptime: 5,
		transition: Fx.Transitions.linear,
		path: '',
		imgs: [],
		random: false,
		copyrights: [],
		copyright_element: false
	},
	initialize: function(obj, options){
		this.obj = $(obj);
		this.setOptions(options);
		this.wrapper = new Element('div',{id:this.obj.getProperty('id'),'class':this.obj.getProperty('class')}).injectBefore(this.obj);
		this.obj.setProperty('id','');
		if(this.wrapper.getStyle('position')!="absolute"){
			this.wrapper.setStyle('position','relative');
		}
		this.obj.setProperty('class','');
		this.obj.injectInside(this.wrapper);
		this.wrapper.setStyles(this.obj.getStyles('width','height'));
		this.obj2 = new Element('img').injectInside(this.wrapper);
		this.obj.setStyles({
			position:'absolute',
			top:0,
			left:0
		});
		this.obj2.setStyles({
			position:'absolute',
			top:0,
			left:0
		});
		this.act_number = 0;
		this.slider = new Fx.Style(this.obj2, 'opacity', {duration:this.options.slidetime*1000,transition:this.options.transition});
		this.slider.set(0);
		this.inter = false;
		this.obj2.addEvent('load',this.imgOnLoad.bind(this));
		this.obj2.setProperty('src',this.obj.getProperty('src'));
		this.was_random = new Array();
	},
	run: function(){
		this.inter = false;
		if(this.options.random){
			if(this.options.imgs.length>=2){
				for(i=0;i<200;i++){
					var r=Math.floor(Math.random()*(this.options.imgs.length));
					if(r!=this.act_number){
						var flag = false;
						for(j=0;j<this.was_random.length;j++){
							if(r==this.was_random[j]){
								flag = true;
							}
						}
						if(!flag){
							break;
						}
					} 
				}
				this.was_random.push(r);
				if(this.was_random.length>=this.options.imgs.length){
					this.was_random = new Array();
				}
				this.act_number = r;
			}
			else{
				this.act_number = 0;
			}
		}
		else{
			this.act_number++;
			if(this.act_number>this.options.imgs.length-1){
				this.act_number = 0;
			}
		}
		this.obj2.setProperty('src',this.options.path+this.options.imgs[this.act_number]);
	},
	setCopyright: function(){
		if(this.options.copyright_element){
			if(this.options.copyrights[this.act_number] && this.options.copyrights[this.act_number]!=""){
				this.options.copyright_element.setHTML(this.options.copyrights[this.act_number]);
				this.options.copyright_element.setStyle('visibility','visible');
			}
			else{
				this.options.copyright_element.setStyle('visibility','hidden');
				this.options.copyright_element.setHTML('');
			}
		}
	},
	afterRun: function(){
		this.obj.setProperty('src',this.obj2.getProperty('src'));
		this.slider.set.delay(100,this.slider,0);
	},
	imgOnLoad: function(){
		if(this.inter==false){
			this.inter = this.run.delay(this.options.swaptime*1000,this);
			this.slider.start(1);
			this.afterRun.delay((this.options.slidetime*1000)+100,this);
			this.setCopyright.delay(this.options.slidetime*1000/2,this);
		}
	}
});
slideShow.implement(new Options);
