var CarouselLite = Class.create();

CarouselLite.prototype = {

	scrollPos: 0,
	scrollStep: 252,
	
	mainHandle: null,
	ulHandle: null,
	
	pictures: null,
	picCount: 0,
	picDisplay: 4,
	
	effect: null,
	effectStatus: 0,
	executer: null,
	executerPeriod: 2,
	
	circleStatus: 0,
	
	initialize: function(mainHandle) {
		this.mainHandle = $(mainHandle);
		if(typeof this.mainHandle == 'undefined')
			this.showLog("Ошибка инициализации");
		this.ulHandle = Selector.findChildElements(this.mainHandle, ["ul"])[0];
		this.pictures = new Array();
	},
	
	scrollPrev: function() {
		if(this.picCount <= this.picDisplay)
			return -1;
		
		// Если первый элемент, не даем переключиться назад
		if(this.scrollPos == 0  && this.circleStatus == 0)
			return -1;
		
		else if(this.scrollPos == 0) {
			this.scrollPos = this.picCount - this.picDisplay;
			var px = this.scrollPos * this.scrollStep;
			$(this.ulHandle).setStyle({
				left: '-'+px+'px'
			});
		}
		
		this.showLog("Перемещение карусели влево");
		
		var nextPosX = this.scrollPos * this.scrollStep;
		
		if(this.effectStatus == 1)
			this.effect.cancel();
		
		this.scrollPos --;
		if(this.scrollPos != 0)
			this.effect = new Effect.Morph(this.ulHandle,{style: "left: -" + (this.scrollPos * this.scrollStep) + "px;", duration: 0.5, afterFinish: this.effectEnd.bind(this)});
		else
			this.effect = new Effect.Morph(this.ulHandle,{style: "left: 0px;", duration: 0.5, afterFinish: this.effectEnd.bind(this)});
		this.effectStatus = 1;
		
		this.showImages();
	},
	
	scrollNext: function() {
		if(this.picCount <= this.picDisplay)
			return -1;
		
		// Если последний элемент, не даем переключиться вперед
		if(this.scrollPos == (this.picCount - this.picDisplay) && this.circleStatus == 0) 
			return -1;
		
		else if(this.scrollPos == (this.picCount - this.picDisplay)) {
			this.scrollPos = 0;
			$(this.ulHandle).setStyle({
				left: '0px'
			});
		}
		
		this.showLog("Перемещение карусели вправо");
		
		if(this.effectStatus == 1)
			this.effect.cancel();
		
		this.scrollPos ++;
		if(this.scrollPos != 0)
			this.effect = new Effect.Morph(this.ulHandle,{style: "left: -" + (this.scrollPos * this.scrollStep) + "px;", duration: 0.5, afterFinish: this.effectEnd.bind(this)});
		else
			this.effect = new Effect.Morph(this.ulHandle,{style: "left: 0px;", duration: 0.5, afterFinish: this.effectEnd.bind(this)});
		this.effectStatus = 1;
		
		this.showImages();
	},
	
	addImage: function(imgAddressSmall, imgAddressBig) {
		
		if(this.circleStatus == 1)
			return -1;
		
		this.picCount ++;
		
		var listIndexEl = document.createElement('li');
		
		$(listIndexEl).setStyle({
			overflow: 'hidden',
			float: 'left',
			width: '246px',
			height: '188px'
		});
		
		var ancherEl = document.createElement('a');
		ancherEl.className = 'highslide';
		ancherEl.href = imgAddressBig;
		$(ancherEl).stopObserving('click');
		$(ancherEl).observe('click',function(e){e.preventDefault(); hs.expand($(ancherEl))});
		
		var imgEl = document.createElement('img');
		$(imgEl).setStyle({
			width: '246px',
			height: '188px'
		});
		imgEl.src = "/templates/prof/images/ajax-loader.gif";
		
		var spanEl = document.createElement('span');
		spanEl.className = 'b-title';
		spanEl.innerHTML = '<span>&nbsp;</span>';
		
		ancherEl.appendChild(imgEl);
		ancherEl.appendChild(spanEl);
		listIndexEl.appendChild(ancherEl);
		this.ulHandle.appendChild(listIndexEl);
		
		this.pictures[this.picCount-1] = {
			imgHandle: imgEl,
			addressSmall: imgAddressSmall,
			addressBig: imgAddressBig,
			imgShow: 0
		};
		
		this.showLog("Картинка <"+imgAddressSmall+"> добавлена в список");
		
		this.showImages();
	},
	
	showImages: function() {
		var indexLast = this.picDisplay > this.picCount ? this.picCount : this.picDisplay;
		indexLast += this.scrollPos;
		for(var i = this.scrollPos; i < indexLast; i ++)
			if(this.pictures[i].imgShow == 0) {
				$(this.pictures[i].imgHandle).observe("load", this.imgEndDownloading.bind(this, this.pictures[i].addressSmall));
				this.showLog("Запрос на загрузку картинки <"+this.pictures[i].addressSmall+">");
				this.pictures[i].imgHandle.src = this.pictures[i].addressSmall;
				this.pictures[i].imgShow = 1;
			}
	},
	
	imgEndDownloading: function(address) {
		this.showLog("Картинка <"+address+"> загружена");
	},
	
	effectEnd: function() {
		this.effectStatus = 0;
		if(this.circleStatus == 1) {
			// Достигнуто начало
			if(this.scrollPos==0) {
				this.scrollPos = this.picCount - this.picDisplay;
				var px = this.scrollPos * this.scrollStep;
				$(this.ulHandle).setStyle({
					left: '-'+px+'px'
				});
			}
			// Достигнут конец
			else if(this.scrollPos == (this.picCount - this.picDisplay)) {
				$(this.ulHandle).setStyle({
					left: '0px'
				});
				this.scrollPos = 0;
			}
		}
	},
	
	startAutoScrollPrev: function() {
		if(this.executer != null)
			this.executer.stop();
		this.scrollPrev();
		this.executer = new PeriodicalExecuter(this.scrollPrev.bind(this), this.executerPeriod);
	},
	
	startAutoScrollNext: function() {
		if(this.executer != null)
			this.executer.stop();
		this.scrollNext();
		this.executer = new PeriodicalExecuter(this.scrollNext.bind(this), this.executerPeriod);
	},
	
	stopAutoScroll: function() {
		if(this.executer != null)
			this.executer.stop();
	},
	
	makeCircle: function() {
		for(var i = 0; i < this.picDisplay; i++)
			this.addImage(this.pictures[i].addressSmall, this.pictures[i].addressBig);
		this.circleStatus = 1;
	},
	
	showLog: function(tlog) {
		if(typeof console != 'undefined') {
			console.log(tlog);
		}
	}
}
