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


var Preview = function(gallery, container)
{
	this.gallery = gallery;
	this.container = container;
	this.FadeEingine = new VE;
	this.MoveEingine = new VE;

	this.setActions();
}

Preview.prototype.setActions = function()
{
	var elements = this.gallery.getElementsByTagName('A');
	for(var i = 0; i < elements.length; i++)
	{
		elements[i].onclick = this.load.bind(this, elements[i]);
	}
}

Preview.prototype.load = function(link)
{
	this.loadImg = document.createElement('IMG');
	this.loadImg.src = link.href;
	this.loadImg.onload = this.removeOldImage.bind(this);
	return false;
}

Preview.prototype.removeOldImage = function()
{
	// Fade out
	this.fade(100, 0);

	// Show new image
	var _container = this.container;
	var _loadImg = this.loadImg;
	var _method = this;

	this.MoveEingine.stop();
	this.MoveEingine.init(parseInt(this.container.offsetHeight), this.loadImg.height, '', .8);
	this.MoveEingine.onChange = function(position)
	{
		_container.style.height = position + 'px';
	};
	this.MoveEingine.onFinish = function()
	{
		removeNodes(_container);
		_container.appendChild(_loadImg);
		_method.fade(0, 100);
	}
	this.MoveEingine.start();
}

Preview.prototype.fade = function(from, to)
{
	var _container = this.container;
	this.FadeEingine.stop();
	this.FadeEingine.init(from, to, '', .8);
	this.FadeEingine.onChange = function(position)
	{
		setOpacity(_container, position);
	};
	this.FadeEingine.start();
}