/*
* Gallery
* @Author: Alexander Gavazov
* @Site: www.studio.bg
*/


var Gallery = function (galleryNode) {
	this.scroll = galleryNode.select('.scroll')[0];
	this.photosCnt = galleryNode.select('li').length;
	this.ctrLeft = galleryNode.select('.left')[0];
	this.ctrRight = galleryNode.select('.right')[0];

	this.currentPage = 1;
	this.perPage = 8;
	this.pageOffcete = 5;
	this.leftOffcete = 96;

	this.setBehavior();
}

Gallery.prototype.setBehavior = function() {
	this.ctrLeft.hide();
	this.ctrRight.hide();

	if (this.photosCnt > this.perPage) {
		this.ctrRight.show();
	}

	this.ctrLeft.observe('click', this.menuGoPrev.bind(this));
	this.ctrRight.observe('click', this.menuGoNext.bind(this));
}

Gallery.prototype.menuGoNext = function() {
	this.ctrLeft.show();

	this.currentPage++;

	if (this.currentPage + 1 >= this.photosCnt / (this.pageOffcete - 1)) {
		this.ctrRight.hide();
	}

	this.movePage(this.currentPage);
}

Gallery.prototype.menuGoPrev = function() {
	this.ctrRight.show();

	this.currentPage--;

	if (this.currentPage <= 1) {
		this.ctrLeft.hide();
	}

	this.movePage(this.currentPage);
}

Gallery.prototype.movePage = function(pageId) {
	if (this._effect) {
		this._effect.cancel();
	}

	var leftOffcet = (this.pageOffcete - 1) * this.leftOffcete;
	var newLeft = -(pageId - 1) * leftOffcet + 'px';

	this._effect = new Effect.Morph(this.scroll, {
		style: {left: newLeft},
		duration: .5
	});
}



document.observe('dom:loaded', function() {
	$$('.photo_gallery').each(function(node) {
		new Gallery(node);
	});
});