//<--
window.addEvent('domready', function() {
/*
	@author: Serge!Sokolov
	@url: http://sergeisokolov.com
	@version: 1.0.0
*/
	/* settings */
	var showDuration = 3000;
	var boxes = $$('.smena');
	var box;
	var images;
	var currentIndex = 0;
	var interval;
	boxes.each(function(box, i){
		images = box.getElements('img');
		images.each(function(img,i){ 
			if(i > 0) {
				img.setOpacity(0);
			}
		});
	});
	/* worker */
	var show = function() {
		boxes.each(function(box, i){
			images = box.getElements('img');
			images[currentIndex].fade('out');
			images[x = currentIndex < images.length - 1 ? currentIndex+1 : 0].fade('in');
		});
		currentIndex = currentIndex < images.length - 1 ? currentIndex+1 : 0
	};
	/* start once the page is finished loading */
	window.addEvent('load',function(){
		interval = show.periodical(showDuration);
		try {
			var s = new Slideshow({
				dir: '/images/slideshow/',
				count: 3,
				delay: 6000,
				playOn: 2,
				duration: 'long',
				img: $(document.body).getElement('.slideshow').getElements('img')[0]
			});
		} catch(E) {
			alert(E);
		}
	});


/*
	@author: SWAT (Rostovtsev Ruslan)
	@url: http://www.jsysteam.ru
	@version: 1.0.0
*/
var Slideshow = new Class({
	Implements: Options,
	options: {
		/* Директория с картинками с названиями N.jpg */
		dir: '/images/slideshow/',
		/* Количество картинок */
		count: 8,
		/* Задержка слайда*/
		delay: 2000,
		/* Скорость растворения */
		duration: 'normal',
		/* Начать слайдшоу если загружено N картинок. */
		playOn: 2,
		/* Объект изменяемого изображения */
		img: null
	},
	list: new Array(),
	loaded: 0,
	started: false,
	initialize: function(options) {
		this.setOptions(options);
		this.load();
		this.options.img.set('tween', {duration: this.options.duration})
	},
	load: function() {
		var o = this.options;
		var img = null;
		var ol = this.onload.bind(this);
		for(var i = 0; i < o.count; i++) {
			img = new Asset.image(o.dir + (i+1) + '.jpg', {onload: function() { ol(this) }});
			img.store('loaded', false);
			this.list.push(img);
		}
	},
	onload: function(img) {
		this.loaded++;
		img.store('loaded', true);
		if(this.loaded >= this.options.playOn && this.started == false) {
			this.started = true;
			this.play();
		}
	},
	rand: function() {
		var id = 0;
		while((id = $random(0, this.options.count)) > -1) {
			try {
				if($chk(this.list[id]) && this.list[id].retrieve('loaded') === true &&
					this.options.img.getProperty('src') != this.list[id].getProperty('src')) break;
			} catch(E) {
				continue;
			}
		}
		this.options.img.setProperty('src', this.list[id].getProperty('src'));
	},
	play: function() {
		var d = 0;
		if(this.options.duration == 'normal') {
			d = 800;
		} else if(this.options.duration == 'short') {
			d = 500;
		} else {
			d = 1000;
		}
		this.options.img.fade(0.0);
		(function() { this.rand() }).delay(d, this);
		(function() { this.options.img.fade(1.0); }).delay(d, this);
		this.play.delay(this.options.delay, this);
	}
});





});



//-->
