/*
* Mag Studio - Home Slider
* @Author: Alexander Gavazov
* @Site: www.creative.bg & www.studio.bg
*/


var HomeSlider = function(parameters)
{
	// Pubclick
	this.totalPages = parameters.totalPages;
	this.pages = parameters.pages;
	this.pagerDot = parameters.pagerDot;
	this.container = parameters.container;
	this.prev = parameters.prev;
	this.next = parameters.next;

	this.dontMove = false;

	// Protected
	this._Eingine = new VE;
	this._moveFunction = Easing.expoOut;
	this._currentPage = 1;
	this._pageStepDistance = 634;
	this._dotLefOffset = 28;
	this._dotStepDistance = 16;
	this._start = 0;
	this._end = 0;
	this._duration = 2;
	this._dotMoveCoefficient = -3.8;

	// Private
	this.onFinish = function(){};

	this.setBehaviour();
}

HomeSlider.prototype.setBehaviour = function()
{
	this.container.style.left = 0;
	this.pagerDot.style.left = this._dotLefOffset + 'px';

	this.prev.onclick = this.goToPrev.bind(this);
	this.next.onclick = this.goToNext.bind(this);

	this.pages.each(function(node, i){		
		node.onclick = this.goTo.bind(this, i + 1);
	}.bind(this));

	try{
	$$('.project_listing').each(function(elem){
		var link = elem.select('a.image-link');
		var border = elem.select('span.border');
		border[0].observe('click', function(){window.location = link[0].href});
	});
	} catch(e) {}

}

HomeSlider.prototype.goToPrev = function()
{
	if(this._currentPage == 1) {
		this.goTo(this._currentPage + 2);
	} else {
		this.goTo(this._currentPage - 1);
	}
}

HomeSlider.prototype.goToNext = function()
{
	if(this._currentPage == 3) {
		this.goTo(this._currentPage - 2);
	} else {
		this.goTo(this._currentPage + 1);
	}
}

HomeSlider.prototype.goTo = function(page)
{
	if(this.dontMove)
	{
		return false;
	}

	if(page == this._currentPage)
	{
		return false;
	}
	else if(page == -1)
	{
		page = 0;
	}
	else if(page > this.totalPages)
	{
		return false;
		page = 1;
	}
	else if(page < 1)
	{
		return false;
		page = this.totalPages;
	}

	this._start = parseInt(this.container.style.left);
	this._end = (page - 1) * -this._pageStepDistance;

	this._currentPage = page;

	this.moveContainer();
}

HomeSlider.prototype.moveContainer = function()
{
	var _this = this;
	this._Eingine.stop();
	this._Eingine.init(this._start, this._end, this._moveFunction, this._duration);
	this._Eingine.onChange = function(position)
	{
		_this.pagerDot.style.left = Math.round((position / _this._dotMoveCoefficient) / _this._dotStepDistance) + _this._dotLefOffset + 'px';
		_this.container.style.left = position + 'px';
	}
	this._Eingine.onFinish = function()
	{
		_this.pagerDot.style.left = (_this._currentPage - 1) * _this._dotStepDistance + _this._dotLefOffset + 'px';
		_this.onFinish();
	}
	this._Eingine.start();
}