

$(document).ready(function(){

	$('.cart-delete').click(function(){
		
		var id 			= parseInt($(this).attr('id'));
		var productid 	= parseInt($(this).attr('productid'));		
		
		
		
		$('#loader').show();

		if (typeof ga === 'function') {
			ga('send', {
			  hitType: 'event',
			  eventCategory: 'Winkelwagen',
			  eventAction: 'Product verwijderd',
			  eventLabel: 'productid_'+productid
			}); 		
		}

		$.post("dynamic/set?what=delCartItem", { id: id, productid: productid }, function(data){
			location.reload();
		});

	});

	$('input[name=cart-amount],select[name=cart-amount]').change(function(){

		$('#loader').show();

		var productid 	= parseInt($(this).attr('productid'));
		var amount 		= parseInt($(this).val());
		
		$.post("dynamic/set?what=setCartItemAmount", { productid: productid, amount: amount }, function(data){
			location.reload();
		});

	});

	//
	
	$('.coupon-delete').click(function(){

		/*

		var productid 	= parseInt($(this).attr('productid'));
		var amount 		= parseInt($(this).val());*/
		
		$.post("dynamic/set?what=delCartCoupon", function(data){ // , { productid: productid, amount: amount }
			location.reload();
		});

	});	

});


	function setCart(){
		var version	= ($('#cart_1').is('visible') ? 1 : 0);

		$.getJSON( "dynamic/cart?_=" + new Date().getTime(), function(data){

			if(data.cookies === true){
				$('#cookies').remove();
			}

			if(version == 1){
				$('#cart_1 .bullet').html(data.amount);
			} else {
				$('.cart_balance .contents').html(data.amount+' Artikel(en)');
				$('.cart_balance .sum span').html('&euro; '+data.subtotal);
			}

			var items = [];
			$.each( data.products, function( key, item ) {

				var options = [];
				$.each(item.options, function(key, option){
					options.push(option.value);
				});
				
				if(version == 1){
					items.push('<li><figure><img src="'+item.image+'"></figure><a class="name" href="'+item.url+'">'+item.name+'</a><div class="options">'+options.join(',')+'</div></li>');
				} else {
					items.push('<tr style="background-color: #fff;"><td>'+(item.amount*1)+' x</td><td width="50px;"><img src="'+item.image+'"></td><td width="95px;">'+item.name+'</td><td width="5px;">€</td><td width="40px;">'+item.total+'</td></tr>');
				}
				
			});

			if(version == 1){
				$('li', $('#cart_1 .list')).remove();
			} else {
				$('tr', $('.cart_dropdown table tbody')).remove();
			}

			if(version == 1){
				$(items.join('')).appendTo('#cart_1 .list');
			} else {
				$(items.join('')).appendTo('.cart_dropdown table tbody');
			}

		});

	}

	setCart();
