/*
* BI Estates Gallery
* @Author: Alexander Gavazov
* @Site: www.creative.bg & www.studio.bg
*/


var Gallery = function(gallery, pagingContainer, pages, stepMove)
{
	this.gallery = gallery;
	this.pagingContainer = pagingContainer;
	this.pages = pages;
	this.stepMove = stepMove;
	this.Eingine = new VE;
	this.currPage = 0;
	this.links = [];

	this.createPaging();
}

Gallery.prototype.createPaging = function()
{
	if(this.pages > 1)
	{
		// Create code
		var source = '<ul class="pages">';
		source += '<li class="no_border"><a class="prev"></a></li>';
		for(var i = 1; i <= this.pages; i++)
		{
			source += '<li><a>' + i + '</a></li>';
		}
		source += '<li class="no_border"><a class="next"></a></li>';
		source += '</ul>';
		this.pagingContainer.innerHTML += source;


		// Set actions
		var elements = this.pagingContainer.getElementsByTagName('LI');
		for(var i = 0, r = 0; i < elements.length; i++)
		{
			var link = elements[i].getElementsByTagName('A')[0];
			if(i == 0)
			{
				link.onclick = this.goToPage.bind(this, 'prev');
			}
			else if(i >= elements.length - 1)
			{
				link.onclick = this.goToPage.bind(this, 'next');
			}
			else
			{
				this.links[r] = link;
				link.onclick = this.goToPage.bind(this, i - 1);
				r++;
			}
		}

		// Set active page
		this.links[this.currPage].className = 'active';
	}
}

Gallery.prototype.goToPage = function(pageId)
{
	if(pageId == 'next')
	{
		pageId = (this.currPage + 1 < this.pages) ? this.currPage + 1 : 0;
	}
	else if(pageId == 'prev')
	{
		pageId = (this.currPage - 1 < 0) ? this.pages - 1 : this.currPage - 1;
	}

	var start = parseInt(this.gallery.style.left) || 0;
	var end = pageId * this.stepMove;
	var _gallery = this.gallery;

	this.Eingine.stop();
	this.Eingine.init(start, end, Easing.expoOut);
	this.Eingine.onChange = function(position)
	{
		_gallery.style.left = position + 'px';
	};
	this.Eingine.start();

	// Set active page
	this.links[this.currPage].className = '';
	this.currPage = pageId;
	this.links[this.currPage].className = 'active';
}