var Fader = new Class({
	Implements: Options,
	options: {
		pause: 3000,
		duration: 3000,
		loop: true,
		onComplete: Class.empty,
		onStart: Class.empty,
		nstart: 0
	},
	initialize: function(container, options) {
		this.setOptions(options);
		this.container = $(container);
		this.imgs = this.container.getElements('img');
		this.next = this.options.nstart;
		this.giss = this.container.getParent('div#galleryImage').getElements('.gi');
		this.numb = 0;
		for (var i = 0; i < this.giss.length; i++) {
			if (this.container == this.giss[i]) {
				this.numb = i;
			}
		}
		var thumbss = $(document.body).getElements('.gallery-thumbs');
		this.logos = thumbss[this.numb].getElements('a');
		//this.start();
	},
	start: function() {
		this.periodical = this.show.bind(this).periodical(this.options.pause);
	},
	stop: function() {
		$clear(this.periodical);
	},
	show: function() {
		if (!this.options.loop && this.next==this.imgs.length-1)
			this.stop();
		this.next = (this.next==this.imgs.length-1)?0:this.next+1;
		var prev = (this.next==0)?this.imgs.length-1:this.next-1;
		var cur_g = 0;
		for (var i = 0; i < this.giss.length; i++) {
			if (this.giss[i].hasClass('active')) {
				cur_g = i;
			}
		}
		/*if (cur_g == this.numb) {
			var w_img = this.logos[0].getSize().x + 12;
			var left = parseInt(this.logos[0].getParent('table').getStyle('left'));
			if (this.next * w_img + left + w_img > this.logos[0].getParent('.gallery-thumbs').getSize().x) {
				var myFx = new Fx.Tween(this.logos[0].getParent('table'), {duration: 2500, transition: Fx.Transitions.linear});
				var new_left = -(this.next * w_img + w_img - this.logos[0].getParent('.gallery-thumbs').getSize().x + 12);
				var razn = left - new_left;
				myFx.setOptions({duration: razn*3.7, transition: Fx.Transitions.linear})
				myFx.start('left', new_left);
			}
			if (this.next * w_img < -left) {
				var myFx = new Fx.Tween(this.logos[0].getParent('table'), {duration: 2500, transition: Fx.Transitions.linear});
				var new_left = - (this.next * w_img);
				var razn = -(left - new_left);
				myFx.setOptions({duration: razn*3.7, transition: Fx.Transitions.linear})
				myFx.start('left', new_left);
			}
		}*/
		/*for (i = 0; i < this.imgs.length; i++) {
			if (i != this.next || prev) {
				this.imgs[i].setStyle('opacity', 0);
			}
		}*/
		this.imgs[this.next].fade('in');
		this.imgs[this.next].addClass('active');
		/*if (cur_g == this.numb) {
			this.logos[this.next].fade(1);
			this.logos[this.next].addClass('active');
		}*/
		this.imgs[prev].fade('out');
		this.imgs[prev].removeClass('active');
		/*if (cur_g == this.numb) {
			this.logos[prev].fade(0.6);
			this.logos[prev].removeClass('active');
		}*/
	}
});
