/*
* Brands Slider - Enco Veinding
* @Author: Alexander Gavazov
* @Site: www.studio.bg
*/


var BrandsSlider = function(parameters)
{
	this.prev = parameters.prev;
	this.next = parameters.next;
	this.container = parameters.container;
	this.content = parameters.content;
	this.elements = parameters.elements;
	this.overs = parameters.overs;
	this.elementOffset = parameters.elementOffset;
	this.stepOffset = parameters.stepOffset;

	this._Eingine = new VE;
	this._overData = [];
	this._moveFunction = Easing.expoOut;
	this._duration = 1.2;
	this._totalWidth = 0;
	this._pageWidth = 0;
	this._totalPages = 0;
	this._currentPage = 1;

	this.setBehaviour();
}

BrandsSlider.prototype.setBehaviour = function()
{
	// Content size
	this.elements.each(function(node) {
		this._totalWidth += node.getWidth() + this.elementOffset;
	}.bind(this));
	this.content.style.width = this._totalWidth + 'px';

	// Hover images
	this.overs.each(function(node, i) {
		this._overData[i] = {
			Eingine: new VE,
			position: 0,
			node: node
		};

		node.setOpacity(0);

		node.onmouseover = this.fades.bind(this, this._overData[i], 100);
		node.onmouseout = this.fades.bind(this, this._overData[i], 0);
	}.bind(this));

	// Steps
	this._pageWidth = this.container.getWidth() + this.stepOffset;
	this._totalPages = Math.ceil(this._totalWidth / this._pageWidth);

	// Click events
	this.prev.onclick = this.goPrev.bind(this);
	this.next.onclick = this.goNext.bind(this);

	// Set content left 0
	this.content.style.left = 0;
}

BrandsSlider.prototype.goPrev = function()
{
	this.goTo(this._currentPage - 1);
}

BrandsSlider.prototype.goNext = function()
{
	this.goTo(this._currentPage + 1);
}

BrandsSlider.prototype.goTo = function(page)
{
	if(page > this._totalPages)
	{
		page = 1;
	}
	else if(page < 1)
	{
		page = this._totalPages;
	}

	this._start = parseInt(this.content.style.left);
	this._end = (page - 1) * -this._pageWidth;

	this._currentPage = page;

	this.move();
}

BrandsSlider.prototype.move = function()
{
	var _this = this;
	this._Eingine.stop();
	this._Eingine.init(this._start, this._end, this._moveFunction, this._duration);
	this._Eingine.onChange = function(position)
	{
		_this.content.style.left = position + 'px';
	}
	this._Eingine.start();
}

BrandsSlider.prototype.fades = function(overData, end)
{
	var overData = overData;
	overData.Eingine.stop();
	overData.Eingine.init(overData.position, end, '', .6);
	overData.Eingine.onChange = function(position)
	{
		overData.position = position;
		overData.node.setOpacity(position / 100);
	}
	overData.Eingine.start();
}