
var frontEndBasket = {
	replaceBasket: function (id) {
		return function (e) {
			var text, rem_item = true, item_total_price;
			var add_basket_button_text = 'В корзину';
			var detect_options = {};
			if (e.summary.amount > 0) {
				text = '<span class="price">' + e.summary.price.actual + '</span> ' + e.summary.price.suffix;
				for (var i in e.items.item) {
					var item = e.items.item[i];
					if (item.id == id) {
						rem_item = false;
						item_total_price = item["total-price"].actual + ' ' + item["total-price"].suffix;
						item_metr = item["item_metr"];
					}
					if (item.page.id == id) {
						if (detect_options.amount) {
							detect_options.amount = detect_options.amount + item.amount;
						}
						else detect_options = {'id':id, 'amount':item.amount};
					}
				}
				if (detect_options.amount) {
					var add_basket_button = jQuery('#add_basket_' + detect_options.id);
					if (add_basket_button[0].tagName.toUpperCase() == 'A') {
						add_basket_button.text(add_basket_button_text + ' (' + detect_options.amount + ')');
					}
					else add_basket_button.val(add_basket_button_text + ' (' + detect_options.amount + ')');
				}
				if (rem_item) {
					if (jQuery('.cart_item_' + id)) {
						jQuery('.cart_item_' + id).remove();
						jQuery('.cart_summary').html(text);
					}
				}
				else {
					jQuery('.cart_item_price_' + id).text(item_total_price);
					jQuery('.cart_summary').html(text);
					if (item_metr!=0) jQuery('.cart_item_metr_' + id).text('(' + item_metr);
					else jQuery('.cart_item_metr_' + id).text('');
				}
				
				//text = 'Товаров: ' + e.summary.amount + '<br />На сумму ' + text;
				text = 'Товаров: ' + e.summary.amount;
			}
			else {
				text = 'Товаров: 0. <br />Корзина пуста.';
				fulltext = 'Корзина пуста.';
				if (jQuery('.basket')) {
					jQuery('.basket').html(fulltext);
				}
			}
			jQuery('.basket_info_summary').html(text);
		};
	},
	add: function (id, form, popup) {
		var e_name, options = {};
		/*if (form) {
			var elements = jQuery(':radio:checked', form);
			for (var i = 0; i < elements.length; i++) {
				e_name = elements[i].name.replace(/^options\[/, '').replace(/\]$/, '');
				options[e_name] = elements[i].value;
			}
		}
		amount = 1;
		options['amount'] = amount;
		basketFront.putElement(id, options, frontEndBasket.replaceBasket(id));
		*/
		amount = 1;
		if (form) {
			var elements = jQuery(':checkbox', form);
			if (elements.length > 0) {
				checkelements = jQuery(':checkbox:checked', form);
				if(checkelements.length > 0) {
					for (var i = 0; i < checkelements.length; i++) {
					e_name = checkelements[i].name.replace(/^options\[/, '').replace(/\].+$/, '');
					options[e_name] = checkelements[i].value;
					//if (elements[i].checked == true) {
					//options['amount'] = amount;
					basketFront.putElement(id, options, frontEndBasket.replaceBasket(id));
					//}
				}}
				else alert ('Необходимо выбрать цвет');
			}
			else {
				//options['amount'] = amount;
				basketFront.putElement(id, options, frontEndBasket.replaceBasket(id));
			}

		}

		if (popup) jQuery('#add_options').remove();
	},
	addFromList: function (id, is_options) {
		if (is_options) {
			if (jQuery('#add_options').length == 0) {
				jQuery.ajax({
					url: '/upage//' + id + '?transform=modules/catalog/popup-add-options.xsl',
					dataType: 'html',
					success: function (data) {
						frontPopup({
							id: 'add_options',
							header: 'Выбор опций',
							width: '400px',
							content: data
						});
					}
				});
			}
		}
		else {
			frontEndBasket.add(id);
		}
	},
	modify: function (id, amount_new, amount_old) {
		if (amount_new.replace(/[\d]+/) == 'undefined' && amount_new != amount_old) {
			basketFront.modifyItem(id, { amount: amount_new }, frontEndBasket.replaceBasket(id));
		}
	},
	remove: function (id) {
		basketFront.removeItem(id, frontEndBasket.replaceBasket(id));
	}
};
