/*  Object Cart
/*--------------------------------------------------------------------------*/
var Cart = {
	url: 	'/ajax/cart/',
	locked: false,
	informer: null,
		
	add: function (id) {
		if (!this.isLocked() && id) {
			this.lock();
			this.sendCommand('add', id, null, null, function () {				
				this.showInformer('Артикул добавлен в заказ');
				this.unLock();
			}.bind(this), function () {
				this.showErrorInformer('Ошибка добавления в заказ');
				this.unLock();
			}.bind(this));
		}
	},
	
	sendCommand: function (command, id, parameters, method, onSuccess, onFailure) {
		return this.request(this.url, this.getUrlParams(command, id, parameters), method, onSuccess, onFailure);	
	},
	
	getUrlParams: function (command, id, parameters) {
		var params = '';
		if (command) {
			params += 'command='+ command;
			if (id) params += '&id='+ id;
			if (parameters) params += '&'+ parameters;
			params += '&'+ Math.random();
		}
		return params;
	},
	
	request: function (url, params, method, onSuccess, onFailure) {
		return (new Ajax.Request(url, {
			parameters: params,
			method: 	method ? method : 'get',
			onSuccess: 	onSuccess,
			onFailure: 	onFailure
		}));
	},
	
	getInformer: function () {
		if (!this.informer) {
			this.informer = new Informer();
		}
		return this.informer;
	},
	
	showInformer: function (message) {
		var informer = this.getInformer();
		if (informer) {
			informer.show(message);
		}
	},
	
	showErrorInformer: function (error_massage) {
		var informer = this.getInformer();
		if (informer) {
			informer.showError(error_massage);
		}
	},
	
	isLocked: function () {
		return this.locked;
	},
	
	lock: function () {
		this.locked = true;
	},
	
	unLock: function () {
		this.locked = false;
	}
}