/*
* Site Order
* @Author: Alexander Gavazov
* @Site: www.studio.bg
*/


var Order = function(parameters)
{
	this.link = parameters.link;
	this.id = this.link.rel;
	this.box = parameters.box;
	this.image = parameters.image;

	this._orderPath = 0;
	this._productId = 0;
	this._cloning = '';
	this._imagePositions = '';
	this._duration = .6;
	this._moveOffset = {
		left: -10,
		top: 10
	};

	this._EingineImage = new VE;
	this._EingineMoveX = new VE;
	this._EingineMoveY = new VE;

	this.setBehaviour();
}

Order.prototype.setBehaviour = function()
{
	this._imagePositions = this.image.cumulativeOffset();
	this._productId = this.link.rel;
	this.link.onclick = this.addProduct.bind(this);
}

Order.prototype.addProduct = function()
{
	this.sendInfo();

	this.removeCloning();
	this.createCloning();

	this.moveCloning();

	// Link return
	return false;
}

Order.prototype.sendInfo = function()
{
	new Ajax.Request(root_directory + lang + '/ajax/add_order/?id=' + this.id, {
		method: 'post',
		onSuccess: function(transport) {
			this.completeInfo(transport.responseText.evalJSON());
		}.bind(this)
	});
}

Order.prototype.completeInfo = function(jsonRespone)
{
	if(jsonRespone.status == 'err' && jsonRespone.msg)
	{
		alert(jsonRespone.msg);
	}
	else if(jsonRespone.status == 'ok')
	{
		this.box.update(jsonRespone.orders);
	}
}

Order.prototype.finishAdding = function()
{
	this.removeCloning();
}

Order.prototype.removeCloning = function()
{
	if(this._cloning.parentNode)
	{
		this._cloning.remove();
	}
}

Order.prototype.createCloning = function()
{
	this._cloning = this.image.cloneNode(false);
	this._cloning.setStyle({
		position: 'absolute',
		left: this._imagePositions.left + 'px',
		top: this._imagePositions.top + 'px',
		opacity: .8
	});
	document.body.appendChild(this._cloning);
}

Order.prototype.moveCloning = function()
{
	var _this = this;

	// Remove image
	this._EingineImage.stop();
	this._EingineImage.init(parseInt(this._cloning.width), 0, Easing.cricIn, this._duration);
	this._EingineImage.onChange = function(position)
	{
		_this._cloning.width = position;
	}
	this._EingineImage.onFinish = function(position)
	{
		_this.finishAdding();
	}
	this._EingineImage.start();


	// Move image X
	this._EingineMoveX.stop();
	this._EingineMoveX.init(this._imagePositions.left, this.box.cumulativeOffset().left + this._moveOffset.left, Easing.cricIn, this._duration);
	this._EingineMoveX.onChange = function(position)
	{
		_this._cloning.style.left = position + 'px';
	}
	this._EingineMoveX.start();


	// Move image Y
	this._EingineMoveY.stop();
	this._EingineMoveY.init(this._imagePositions.top, this.box.cumulativeOffset().top + this._moveOffset.top, Easing.cricIn, this._duration);
	this._EingineMoveY.onChange = function(position)
	{
		_this._cloning.style.top = position + 'px';
	}
	this._EingineMoveY.start();
}