$(document).ready(function() {
	
	$('.btn-buy').click(function() {
		
		if( $(this).hasClass('btn-buy-v3') ) { return false; }
		
		var id = $(this).attr('data-id');
		var quantity = $('input[button-id="' + id + '"]').val();
		var price_type = $(this).attr('data-price');
		
		$.ajax({
			type: "POST",
			url: "/ajax/add_to_basket.php",
			data: "CatalogBasketAction=Add&CatalogBasketItem=" + id + "&CatalogBasketQuantity=" + quantity + "&CatalogBasketPrice=" + price_type,
			success: function(data) {
				
				var parsed = $.parseHTML(data);
				var table = $(parsed).find(".drop-cart").html();
				var small = $(parsed).find("a[data-drop='cart']").html();

				$(".drop-cart").html( table );
				$("a[data-drop='cart']").html( small );
				
				UpdateBasket();
				
			}
		});
		
		$(this).after("<p style='color: green;' class='success'>Товар в корзине</p>");
		


		setTimeout(hideSuccess, 3000);
		
	});
	
	
	$('.btn-buy-v2').click(function() {
		
		var id = $(this).attr('data-id');
		var quantity = $(this).parent().parent().parent().find('input.quantity').val();
		var price_type = $(this).attr('data-price');
		
		console.log(id);
		console.log(quantity);
		console.log(price_type);
		
		$.ajax({
			type: "POST",
			url: "/ajax/add_to_basket.php",
			data: "CatalogBasketAction=Add&CatalogBasketItem=" + id + "&CatalogBasketQuantity=" + quantity + "&CatalogBasketPrice=" + price_type,
			success: function(data) {
				
				var parsed = $.parseHTML(data);
				var table = $(parsed).find(".drop-cart").html();
				var small = $(parsed).find("a[data-drop='cart']").html();

				$(".drop-cart").html( table );
				$("a[data-drop='cart']").html( small );
				
				UpdateBasket();
				
			}
		});
		
		$(this).after("<span style='color: green; margin-top: 5px;' class='success'>Товар в корзине</span>");
		


		setTimeout(hideSuccess, 3000);
		
	});
	
	
	$('.btn-buy-v3').click(function() {
		
		var id = $(this).attr('data-id');
		var quantity = 1;
		var price_type = $(this).attr('data-price');
		
		console.log(id);
		console.log(quantity);
		console.log(price_type);
		
		$.ajax({
			type: "POST",
			url: "/ajax/add_to_basket.php",
			data: "CatalogBasketAction=Add&CatalogBasketItem=" + id + "&CatalogBasketQuantity=" + quantity + "&CatalogBasketPrice=" + price_type,
			success: function(data) {
				
				var parsed = $.parseHTML(data);
				var table = $(parsed).find(".drop-cart").html();
				var small = $(parsed).find("a[data-drop='cart']").html();

				$(".drop-cart").html( table );
				$("a[data-drop='cart']").html( small );
				
				UpdateBasket();
				
			}
		});
		
	});
	
	
	/*
	$('.change-basket').click(function() {
		
		var val = $(this).parent().find('input').val();
		var type = $(this).attr('type');
		var id = $(this).parent().find('input').attr('data-id');
		
		if( type == 'minus' && val != 1 ) {
			val--;
		}
		
		if( type == 'plus' ) {
			val++;
		}
		
		window.location.href = '?action=SetQuantity&item=' + id + '&quantity=' + val;
		
		console.log(val);
		
	});	
	*/
	
	
	$('.change-basket').click(function() {
		
		var val = $(this).parent().find('input').val();
		var type = $(this).attr('type');
		var id = $(this).parent().find('input').attr('data-id');
		
		if( type == 'minus' && val != 1 ) {
			val--;
		}
		
		if( type == 'plus' ) {
			val++;
		}
		
		window.location.href = '?action=update&item=' + id + '&quantity=' + val;
		
		console.log(val);
		
	});
	
	
	
	$('.group-number-v2 button').click(function() {
		
		var price = $(this).parent().find('input').attr('price');
		var val = $(this).parent().find('input').val();
		var type = $(this).attr('type');
		
		if( type == 'minus' && val != 1 ) {
			val--;
		}
		
		if( type == 'plus' ) {
			val++;
		}
		
		$(this).parent().parent().parent().find('.col-summ-val .price').text( price * val + ' р.' );
		
		console.log(type);
		
	});
	
	
	var start = true;
	
	$(document).on("click", ".drop-cart .group-number-v2 button", function() {
		
		
		
		var price = $(this).parent().find('input').attr('price');
		var val = $(this).parent().find('input').val();
		var id = $(this).parent().find('input').attr('data-id');
		var type = $(this).attr('type');
		
		// Странно что не работает, но все же
		if( !start ) {
		
			if( type == 'minus' && val != 1 ) {
				val--;
			}
			
			if( type == 'plus' ) {
				val++;
			}
		
		}
		
		var url = "/ajax/update_quantity.php?AJAX=Y&action=SetQuantity&item=" + id + "&quantity=" + val;
		
		
		$.ajax({
			type: "GET",
			url: url,
			dataType: "html" ,
			success: function(data) {
				
				var parsed = $.parseHTML(data);
				var table = $(parsed).find(".drop-cart").html();
				var small = $(parsed).find("a[data-drop='cart']").html();

				$(".drop-cart").html( table );
				$("a[data-drop='cart']").html( small );
				
				start = false;

			}
		});
		
	});
	
	
	$('.btn-wishlist').click(function() {
		
		var id = $(this).attr('data-id');
		
		$.ajax({
			type: "GET",
			url: "?action=ADD_TO_COMPARE_LIST&id=" + id,
			//data: "?action=ADD_TO_COMPARE_LIST&id=" + id ,
			success: function(data) {
				
				$.ajax({
					type: "POST",
					url: "/ajax/UpdateCompare.php",
					data: "IBLOCK_TYPE=catalog&IBLOCK_ID=12&COMPARE_NAME=CATALOG_COMPARE_LIST",
					success: function(data) {
						
						console.log(data);
						
						$('.wishlist').html( data );
						
						//$('div.cart').html( data );
						
					}
				});
				
				//$('div.cart').html( data );
				
			}
		});
		
		return false;
		
	});
	
	
});



function hideSuccess() {
	$('.success').remove();
	$('.success').remove();
}

function UpdateBasket() {
	
	var drops = $('[data-drop]');

	drops.each(function() {
		var toggle = $(this);
		var drop = $('.drop-' + toggle.data('drop'));

		if (!drop.length) {
			drop = toggle.siblings('.drop');
		}

		if (!drop.length) {
			return;
		}

		drop.data('fn', function(e) {
			if (!$(e.target).hasClass('drop') && !$(e.target).parents('.drop')[0]) {
				drop.removeClass('active');
				$(document).unbind('click', drop.data('fn'));
			}
		});

		toggle.on('click', function(e) {
			e ? e.preventDefault() : false;
			e.stopPropagation ? e.stopPropagation() : e.cancelBubble = true;

			$('[class*="drop-"]').removeClass('active');

			drops.removeClass('active');
			drop.addClass('active');

			$(document).on('click', drop.data('fn'));
		});

		$('[data-bind="close"]', drop).on('click', function(e) {
			e ? e.preventDefault() : false;
			drop.removeClass('active');
			$(document).unbind('click', drop.data('fn'));
		});
	});
	
}

