$(function() {
	$('.icon_minus').bind('click', function() {
		inp = $(this).parent().find('input');
		v = inp.attr('value');
		//inp.attr('value', parseFloat(v) - 1.0);
		i = inp.attr('id').substr(16);
		basket.change(i, parseFloat(v) - 1.0);
	});
	$('.icon_plus').bind('click', function() {
		inp = $(this).parent().find('input');
		v = inp.attr('value');
		//inp.attr('value', parseFloat(v) + 1.0);
		i = inp.attr('id').substr(16);
		basket.change(i, parseFloat(v) + 1.0);
	});
	$('.icon_remove').bind('click', function() {
		inp = $(this).parent().find('input');
		i = inp.attr('id').substr(16);
		basket.remove(i);
	});
	$('.basket_quantity').bind('change',function() {
		inp = $(this);
		i = inp.attr('id').substr(16);
		v = inp.attr('value');
		basket.change(i, v);
	});
	$('.basket_quantity').bind('keypress',function(e) {
		if (e.which == 13) {
			inp = $(this);
			i = inp.attr('id').substr(16);
			v = inp.attr('value');
			basket.change(i, v);
			return false;
		}
		return true;
	});
});

var basket = {

	add: function (id, number) {
		$.ajax({
			type:'POST',
			url: "/include/ajax/market/add.php",
			dataType : "text",
			data: { 'id' : id, 'add' : number },
			beforeSend:function(){
				$("#basket_" + id).after('<img id="basket_' + id + '" src="/img/ajax-loader.gif" alt="load.." />').remove()
			},
			success: function(data) {
				basket.update();
				ps = $('#basket_products');
				setTimeout(function(){
					$('#basket_' + id).after(data).remove();
				}, 500);
				$('.no_basket').hide();
				$('.basket').show();
			}
		});
		return false;
	},

	change: function (id, v) {
//		var n = $("#basket_quantity_" + id).attr("value");
//		if (command=='del') {$(".card_"+id).remove();}
		$.ajax({
			type:'POST',
			url: "/include/ajax/market/change.php",
			dataType : "text",
			data: { 'id': id, 'n': v },
			beforeSend:function(){
			   $("#basket_" + id).after('<img id="basket_' + id + '" src="/img/ajax-loader.gif" alt="load.." />').remove();
			},
			success: function(data) {
				setTimeout(function(){
					$('#basket_' + id).remove();
					$('#basket_add_' + id).after('<span class="in-cart">В корзине</span>').remove();
					var v=$(data).find("#v").html();
					var price=$(data).find("#price").html();
					var itogo=$(data).find("#itogo").html();
					var total=$(data).find("#total").html();
					$('#basket_quantity_' + id).attr('value', v);
					$(".price"+id).html(price + ' р.');
					$(".itogo").html(itogo);
					$(".total").html(total);
				}, 100);
				$('.no_basket').hide();
				$('.basket').show();
			}
		});
	},

	remove: function (id) {
		$.ajax({
			type:'POST',
			url: "/include/ajax/market/remove.php",
			dataType : "text",
			data: { 'id' : id },
			beforeSend: function() {
				$("#basket_" + id).html('<img src="/img/ajax-loader.gif" alt="load.." />')
			},
			success: function(data) {
				setTimeout(function(){
					$('#basket_' + id).html('');
					$('#basket_product_' + id).remove();
					var price=$(data).find("#price").html();
					var itogo=$(data).find("#itogo").html();
					var total=$(data).find("#total").html();
					$(".price"+id).html(price);
					$(".itogo").html(itogo);
					$(".total").html(total);
					if (!$('.basket_product').length) {
						$('.basket_products').hide();
						$('.basket_no_products').show();
					}
					if (total == 0) {
						$('.basket').hide();
						$('.no_basket').show();
					}
				}, 300);
			}
		});
		return false;
	},

	update: function(id) {
		$.ajax({
			type: 'POST',
			url: "/include/ajax/market/update.php",
			dataType: "text",
			data: { 'id' : id },
			beforeSend: function() {
				//$("basket_")
				$("#basket_total").html('<img src="/img/ajax-loader.gif" alt="load.." />')
			},
			success: function(data) {
				$('#basket_total').html(data);
			}
		});
	},

	/*update: function() {
		$.ajax({
			type: 'POST',
			url: "/include/ajax/market/updateCard.php",
			dataType: "text",
			data: { },
			beforeSend: function() {
				$("#basket_total").html('<img src="/img/ajax-loader.gif" alt="load.." />')
			},
			success: function(data) {
				$('#basket_total').html(data);
			}
		});
	}*/
}
