var Loop = new Class({

	loopCount: 0,
	isStopped: true,
	isLooping: false,
	loopMethod: $empty,

	setLoop: function(fn,delay){
		if(this.isLooping) {
			this.stopLoop();
			var wasLooping = true;
		} else {
			var wasLooping = false;
		}
		this.loopMethod = fn;
		this.loopDelay = delay || 3000;
		if(wasLooping) this.startLoop();
		return this;
	},

	stopLoop: function() {
		this.isStopped = true;
		this.isLooping = false;
		$clear(this.periodical);
		return this;
	},

	startLoop: function(delay) {
		if(this.isStopped){
			var delay = (delay) ? delay : this.loopDelay;
			this.isStopped = false;
			this.isLooping = true;
			this.periodical = this.looper.periodical(delay,this);
		};
		return this;
	},

	resetLoop: function(){
		this.loopCount = 0;
		return this;
	},

	looper: function(){
		this.loopCount++;
		this.loopMethod(this.loopCount);
		return this;
	}

});

var bgswitch = new Class({
	Implements: (Loop),
	initialize: function(opcje){
	this.elem = opcje.elem;
	this.setLoop(this.start, 3000);
	this.i = 1;
	},
	start: function(){
		this.setBg(this.i);
		this.i++;
		if(this.i>3)
			this.i=0;
	},
	setBg: function(iter){
		//alert(iter);
		this.elem.setStyle('background-image', 'url(\'tl_files/img/menu_left'+iter+'.png\')');

	}

});
var fadeImg = new Class({
	Implements: [Loop, Chain],
	initialize: function(opcje){
		this.implement
		this.cont = opcje.cont;
		this.duration = opcje.duration;
		this.pause = opcje.pause || 0;
		this.imgArr = this.cont.getElements('img');
		this.i = 0;
		this.next = 1;
		this.hideImg();
		this.separate = opcje.separate;
		this.setLoop(this.start, this.pause+this.duration*2);
	},
	hideImg: function(){
		this.imgArr.each(function(elem,i){
		if(i>0)
			elem.setStyles({'opacity': '0', 'filter': 'alpha(opacity=0)'});
		});
	},
	//z tym trzeba zrobic pozadek
	start: function(){
		this.swapImg();
		if(this.i<this.imgArr.length-1)
			this.i=this.i+1;	
		else
			this.i=0;	
		
		if(this.i<this.imgArr.length-1)
			this.next=this.i+1;	
		else
			this.next=0;
	},
	swapImg: function(){
		if(this.i<this.imgArr.length-1)
			this.next = this.i+1;
		else
			this.next = 0;
		this.imgOut = new Fx.Morph(this.imgArr[this.i], {duration: this.duration});
		this.imgIn = new Fx.Morph(this.imgArr[this.next], {duration: this.duration});
		if(this.separate==true){
			this.imgOut.onComplete = function(){
					//this.imgArr[this.i-1].setStyle('display','none');
					//this.imgArr[this.next-1].setStyle('display','block');
					this.imgIn.start({'opacity':1});
				}.bind(this);
		}
		else{
			this.imgIn.start({'opacity':1});
		}
		this.imgOut.start({'opacity' : 0});
		
	}
})

	
	







