var CSlideShow = Class.create();
CSlideShow.prototype = {
	holder: null, images: [], imgToShow: [],
	counter: 0,
	visibleImagesCount: 1, imagesCount: 0,
	imageChangeTime: 2000, effectTime: 1,
	
	initialize: function(albumLayer) {
		this.holder = albumLayer;
		this.images = this.holder.getElementsBySelector('.album-item');
		this.imagesCount = this.images.length;
		var arr = this.holder.id.split('-');
		// для слайдшоу должны быть указаны параметры: "кол-во картинок" и "время задержки"
		if(arr.length < 4) return false;
		this.visibleImagesCount = arr[2] * 1;
		this.counter = this.visibleImagesCount;
		this.imageChangeTime = arr[3] * 1000;
		
		this.rotateImages();
	},//---------------------------------------------------------------------
	
	timerAppear: null, timerFade: null, timerRotate: null,
	rotateImages: function() {
		var i, N,
			showFrom  = this.counter % this.imagesCount,
			showTo = showFrom + this.visibleImagesCount,
			reverse = false;
		if(showTo > this.imagesCount) {
			showTo -= this.imagesCount;
			reverse = true;
		}
		this.imgToShow = Array();
		for (i = 0, N = this.imagesCount; i < N; i++) {
			if (reverse) {
				if ((i < showTo) || (i >= showFrom)) this.imgToShow[ this.imgToShow.length ] = this.images[i];
				else Effect.Fade(this.images[i], { duration: this.effectTime });
			}
			else {
				if ((i >= showFrom) && (i < showTo)) this.imgToShow[ this.imgToShow.length ] = this.images[i];
				else Effect.Fade(this.images[i], { duration: this.effectTime });
			}
		}
		this.counter += this.visibleImagesCount;
		
		// гашение, задержка
		this.timerFade = window.setTimeout( function(obj){
			return function() {
				// гашение выполнено - появление
				var i, N;
				for(i=0, N=obj.imgToShow.length; i<N; i++) Effect.Appear(obj.imgToShow[i], { duration: obj.effectTime });
			
				// задержка, затем смена картинок
				obj.timerAppear = window.setTimeout( function(obj){
					return function() {
							obj.timerRotate = window.setTimeout( function(obj){
							return function(){
								obj.rotateImages();
							}
						}(obj), obj.imageChangeTime);
					}
				}(obj), obj.effectTime * 1000);
			}
		}(this), this.effectTime * 1000);
	}//----------------------------------------------------------------------
};
/////////////////////////////////////////////////////////////////////////////

Event.onReady( function(){
	$('content').getElementsBySelector('.album').each( function(album){
		new CSlideShow(album);
	} );
} );
//---------------------------------------------------------------------------
