$(function(){
		getCart();		
		
		// Product toevoegen
		$('#webshop-add-product').click(function()
		{	
			var quantity 	= $('#product-quantity').val();
			var product 	= $('.webshop-product-combobox option:selected').text();
			var product_id 	= $('.webshop-product-combobox option:selected').val();

			if(product_id=='') 
			{
				alert('U heeft geen product geselecteerd.');
				return false;
			}
			else
			{

				if(quantity=='' || parseInt(quantity)<1 ) 
				{
					alert('U moet minimaal 1 product afnemen.');
					return false;
				}
				else
				{
					$.ajax({
						type: 'POST',
						url: '../../wp-content/themes/flirtcompany_uno_tre/webshop/session.php?action=add',
						data: {aantal:quantity,naam:product,product_id:product_id},
						success: function(){
							getCart();
							
							$('.webshop-product-combobox option').removeAttr('selected'); 
							$('.webshop-product-combobox option:first-child').attr('selected', true); 
						}						 
					});	
					return false;				
				}				
			}		
		});		

		function getCart()
		{		
			$.getJSON('../../wp-content/themes/flirtcompany_uno_tre/webshop/session.php?action=get', function(data) 
			{			
				if($(data).length==0)
				{
					$('#webshop-cart-content li').remove();
					$('#webshop-cart-content').append('<li>Uw winkelmandje is leeg.</li>');
				}
				else
				{
					$('#webshop-cart-content li').remove();
					
					jQuery.each(data, function(index, itemData) 
							{			
								$('#webshop-cart-content').append('<li id="product_' + itemData.product_id  + '"><strong>' + itemData.naam + '</strong><br/>Aantal: <em>' + itemData.aantal + 'x</em> - <a class="remove_product" id="' + itemData.product_id + '" href="#">Verwijder</a></li>');
					});
					
					// Bind click voor het verwijderen van een product
					$('.remove_product').unbind('click').click(function()
					{
						elementId = $(this).attr('id');

						$.ajax({
							type: 'POST',
							url: '../../wp-content/themes/flirtcompany_uno_tre/webshop/session.php?action=remove',
							data: {product_id:elementId},
							success: function(data){
								getCart();
							}										 
						});
						
						return false;
					});			
				}				
			});						
		}			

		$('#webshop-cart').submit(function()
		{				
			if($('#webshop-cart').valid())
			{
				$.ajax({
					type: 'POST',
					url: '../../wp-content/themes/flirtcompany_uno_tre/webshop/session.php?action=place',
					data:
					{
						naam:		$('input[name="naam"]').val(),
						adres:		$('input[name="adres"]').val(),
						postcode:	$('input[name="postcode"]').val(),
						woonplaats:	$('input[name="woonplaats"]').val(),
						telefoon:	$('input[name="telefoon"]').val(),
						email:		$('input[name="email"]').val(),
						opmerking: 	$('textarea[name="opmerking"]').val()
					},
					beforeSend: function()
					{
						$('#webshop-note').addClass('noticeText').html('Bezig met versturen...').show();
					},
					success: function(data)
					{
						if(data=='0') 
						{
							alert('Voeg eerst een product toe voordat u uw bestelling plaatst');
						}
						else
						{
							$('input[name="naam"]').val('');
							$('input[name="adres"]').val('');
							$('input[name="postcode"]').val('');
							$('input[name="woonplaats"]').val('');
							$('input[name="telefoon"]').val('');
							$('input[name="email"]').val('');
							$('textarea[name="opmerking"]').val('');
							
							// Cart legen
							$('#webshop-cart-content li').remove();
							$('#webshop-cart-content').append('<li>U winkelmandje is leeg.</li>');
							
							$('#webshop-note').html('Uw bestelling is succesvol ontvangen!<br/><br/>Er is een e-mail bevestiging gestuurd.<br/> Geen e-mail ontvangen? Controleer dan uw spambox.');
						}
					}										 
				});

				return false;
			}		
			else
			{
				alert("Vul alstublieft alle verplichte velden correct in. \nAlleen dan kan uw bestelling worden geplaatst.");
				return false;		
			}	
			return false;			
		});		

		$('#webshop-submit').click(function()
		{
			$('#webshop-cart').submit();
			return false;			
		});
		
		$('.product-toggle').click(function() {
			
			  var product_id = $(this).attr('id');
			  product_id = product_id.split('_');
			  
			  $('#excerpt_'+product_id[1]).toggle('slow');
			  
			  return false;
		});

    });
