var Scroller = new Class({	
	visible_boxes: 1,  anim_speed: 1800, anim_pause: 4000, 
	
	active: 0, //circle
	last_active: 0, // circle
	circles: null,
	busy: false,	
	
	initialize: function () {
		
		//element przesuwany
		this.cont = $('baners-box');
		this.count = this.cont.getElements('.baner-photos').length;
		this.width = this.left_offset = this.cont.getElements('.baner-photos')[0].getStyle("width").toInt() + this.cont.getElements('.baner-photos')[0].getStyle("margin-right").toInt();
		$('baners-all').setStyle('width', (this.width*this.count));
		
		this.fx_cont = new Fx.Scroll(this.cont, {
			duration: this.anim_speed,
			wheelStops: false,
			transition: Fx.Transitions.Quad.easeInOut,
			onComplete: function () {
				var offset = this.cont.getScroll().x + (this.left_offset * this.visible_boxes);
				var width =  this.width * this.count;
				if (offset >= width)
					this.toStart();
				else if (this.cont.getScroll().x == 0)
					this.toEnd();
				this.busy = false;
				
				// Clear, co kazde przesuniecie odczekaj ponowny czas Pausy zanim nastapi kolejne
				this.clear();
					 			
			}.bind(this)
		});
		
		
		var scroll_circles = $('baner-radios')
		if ($chk(scroll_circles)) {
			this.circles = scroll_circles.getElements('.radio');
			this.circles[this.active].addClass('active');
			
			this.circles.each(function(item, i){
				item.addEvent("click", function () {
					this.moveTo(i);
				}.bind(this))
			}.bind(this))
		}		
		
		
		this.toStart();
		
		//usaw samoczynne przesuwanie sie w prawo		
		this.timer = this.right.periodical (this.anim_pause, this)
		//this.timer = this.right.delay (this.anim_pause, this)
		 
		
	},
	
	left: function () {
		if (!this.busy) {
			this.busy = true;
			var x = this.cont.getScroll().x;			
			this.fx_cont.start (x - this.left_offset,0)
			
			if (this.circles) {
			
				var k = (( x - this.left_offset) / this.left_offset).toInt() - 1;
				
				if (k == -1)
					k = (this.count -2);
								
			
				this.last_active = this.active;
				this.circles[this.last_active].removeClass('active');
				
				this.circles[k].addClass('active');
				this.active = k;			
			
			}			
		}

	},
	
	right: function () {
		if (!this.busy) {
			this.busy = true;
			var x = this.cont.getScroll().x;
			this.fx_cont.start(x + this.left_offset, 0)
			
			
			if (this.circles) {
			
				var k = ((x + this.left_offset) / this.left_offset).toInt() - 1;
				if (k == (this.count-2)) 
					k = 0;
				
				
				this.last_active = this.active;
				this.circles[this.last_active].removeClass('active');
				
				this.circles[k].addClass('active');
				this.active = k;
			}
			
		}
		

	},
	
	toStart: function () {
		new Fx.Scroll(this.cont, {
			duration: 0
		}).start(this.left_offset*this.visible_boxes);	
	},
	
	toEnd: function () {
		new Fx.Scroll(this.cont, {
			duration: 0
		}).start(this.left_offset*(this.count-(this.visible_boxes * 2)));		
	},
	
	moveTo: function (k) {
		
		if (!this.busy) {
			
			if (k != this.active) {
				this.busy = true
				this.fx_cont.start((k + 1).toInt() * this.left_offset, 0)
				
				if (this.circles) {
					this.last_active = this.active;
					this.circles[this.last_active].removeClass('active');
					
					this.circles[k].addClass('active');
					this.active = k;
				}
			}
		}		
	},	
	
	clear: function () {
		clearInterval (this.timer);
		this.timer = this.right.periodical (this.anim_pause, this)
	}	
})


window.addEvent("load", function () {
	if ($chk($('baners-box'))) {
		new Scroller();
	}
})

