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

var PortfolioDetails = function(parameters)
{
	this.thumbs = parameters.thumbs;
	this.thumbsMasks = parameters.thumbsMasks;
	this.slider = parameters.slider;
	this.content = parameters.content;
	this.loadImgEvent = parameters.loadImgEvent; // Becouse IE6

	this._SliderEingine = new VE;
	this._ContentEingine = new VE;
	this._thumbsOffset = 122;
	this._preloader = new Element('div', {className: 'ajax_preloader'});
	this._preloadImage = '';

	this.setBehaviour();
}

PortfolioDetails.prototype.setBehaviour = function()
{
	this.thumbs.each(function(node, i) {
		Event.observe(this.thumbsMasks[i], 'click', this.load.bind(this, node.href, i * this._thumbsOffset));
	}.bind(this));

	this.slider.style.top = 0;
}

PortfolioDetails.prototype.load = function(link, sliderPosition)
{
	this.showPreloader(link.search('.swf') != -1 ? this.loadFlash.bind(this, link) : this.loadImage.bind(this, link));
	this.moveSlider(sliderPosition);
}

PortfolioDetails.prototype.moveSlider = function(position, callBack)
{
	var _this = this;
	var start = parseInt(this.slider.style.top);

	this._SliderEingine.stop();
	this._SliderEingine.init(start, position, Easing.expoOut, 1);
	this._SliderEingine.onChange = function(position)
	{
		_this.slider.style.top = position + 'px';
	}
	this._SliderEingine.start();
}

PortfolioDetails.prototype.showPreloader = function(callBack)
{
	var start = parseInt(this.content.offsetHeight);

	this.content.update();

	var nextCallBack = function(callBack) {
		this.content.update(this._preloader);
		setTimeout(callBack, 300);
	}.bind(this, callBack);

	this.resizeContent(start, 100, nextCallBack);
}

PortfolioDetails.prototype.resizeContent = function(start, end, callBack)
{
	var _this = this;
	var callBack = callBack;

	this._ContentEingine.stop();
	this._ContentEingine.init(start, end, Easing.expoOut, 1);
	this._ContentEingine.onChange = function(position)
	{
		_this.content.style.height = position + 'px';
	}
	if(callBack)
	{
		this._ContentEingine.onFinish = callBack
	}
	this._ContentEingine.start();
}

PortfolioDetails.prototype.loadImage = function(link)
{
	this._preloadImage = new Element('IMG', {src: link, onload: this.loadImgEvent, style: "position: absolute; top: 9999px; left: 0;"});
	this._preloadImage.onload = this.preloadImage.bind(this); //IE6
	this.content.insert(this._preloadImage);
//alert(link);
//alert(this._preloadImage.identify());
	if(Prototype.Browser.Opera && this._preloadImage.offsetHeight > 15) //Opera
	{
		this.preloadImage();
	}
}

PortfolioDetails.prototype.preloadImage = function()
{
	if(this._preloader.parentNode)
	{
		Element.remove(this._preloader);
	}
	var start = parseInt(this.content.offsetHeight);
	var end = this._preloadImage.offsetHeight;

	this.resizeContent(start, end, function() {this._preloadImage.style.top = 0}.bind(this));
}

PortfolioDetails.prototype.loadFlash = function(link)
{
	d('loadFlash');
}
