$(document).ready(function(){
	
	$('#menu .button').mouseover(function() {
		$(this).children('.submenu').show();
	}).mouseout(function() {
		$(this).children('.submenu').hide();
	});
	
	
	
	$('#social .sitem img').mouseover(function() {
		$(this).animate({
			top		:	'5px'
		}, 100);
	}).mouseleave(function() {
		$(this).animate({
			top		:	'18px'
		}, 100);
	});
	
	$('.producten tr.r').mouseover(function() {
		$(this).children('td').css('background','#c4d5e1');
	}).mouseout(function() {
		$(this).children('td').css('background','none');
	});
	
	$(document).click(function() {
		if($('#autocompleet').length > 0) {
			$('#autocompleet').hide();
		}
	});
	
	//	Winkelwagen
	$('#winkelwagen').append('<div id="w_tp"></div>');
	$('#w_tp').css({position:'absolute',left:'0px',top:'0px',width:'200px',height:'2px',background:'url(images/winkelwagen_top.gif)'});


	$('#winkelwagen').append('<div id="w_bt"></div>');
	$('#w_bt').css({position:'absolute',left:'0px',bottom:'0px',width:'200px',height:'2px',background:'url(images/winkelwagen_bottom.gif)'});

	$('.winkelwagen td').click(function(e) {
		if(e.target.tagName != 'INPUT' && e.target.tagName != 'IMG') {
			if($(this).parent().children().attr('class') == 'artikelnr' || $(this).parent().children().attr('class') == 'artikelnr closed') {
				if($(this).parent().children().attr('class') == 'artikelnr') {
					$(this).parent().children().addClass('closed');
					$(this).parent().next().children().show();
				}
				else {
					$(this).parent().children().removeClass('closed');
					$(this).parent().next().children().hide();
				}
			}
		}
	});	
	
	$('#dropdown').mouseover(function() {
		$('#list').show();
	}).mouseout(function() {
		$('#list').hide();
	});
	$('#dropdown .option').mouseover(function() {
		$(this).css({background:'#f1f1f1'});
	}).mouseout(function() {
		$(this).css({background:'#ffffff'});
	}).click(function() {
		var aantal = $(this).attr('class').split(' ');
		$('#dropdown .selected').text((aantal[1] == 9999 ? 'Alle' : aantal[1]));
		$('#toon span').text((aantal[1] == 9999 ? 'artikelen' : 'artikelen per pagina'));
		$('#list').hide();
		$.ajax({
			type	:	'POST',
			url		:	'update_cart.php',
			data	:	'&type=producten_pp&producten_pp=' + aantal[1],
			success	:	function(r) {
				var pid = $('#dropdown').attr('class').split('_');
				location.reload();
			}
		});
	});
	
	//	Content
	$('.rechts').append('<div class="r_tp"></div>');
	$('.r_tp').css({position:'absolute',left:'0px',top:'0px',width:'686px',height:'3px',background:'url(images/content_top.gif)'});
	$('.rechts').append('<div class="r_bt"></div>');
	$('.r_bt').css({position:'absolute',left:'0px',bottom:'0px',width:'686px',height:'3px',background:'url(images/content_bottom.gif)'});
	
	//	Assortiment menu
	$('.item').click(function () {
		var sClass = $(this).attr('class');
		$(".item:visible").removeClass('closed');
		$('.subitems:visible').slideUp('slow');
		if(sClass == 'item') {
			$(this).addClass('closed');
			$(this).next().slideDown('slow');
		}
	});
	
	//	Zoeken
	$('#zoeken input').click(function() {
		if($(this).val() == 'Zoeken...') {
			$(this).val('');
		}
	}).focus(function() {
		if($(this).val() == 'Zoeken...') {
			$(this).val('');
		}
	}).blur(function() {
		if($(this).val() == '') {
			$(this).val('Zoeken...');
		}
	}).keyup(function() {
		if($('#autocompleet').length == 0) {
			$('#zoeken').append('<div id="autocompleet" style="display:none;"></div>');
			$('#autocompleet').css({
				position	:	'absolute',
				left		:	'0px',
				top			:	'24px',
				width		:	'198px',
				border 		:	'1px #8ca2ba solid',
				background	:	'#fff',
				zIndex		:	'55'
			});
		}
		var zoekterm	=	$(this).val();
		if(zoekterm == '' || zoekterm == '') {
			$('#autocompleet').hide();
		}
		else {
			$.ajax({
				type	:	'GET',
				url		:	'shop_actions.php?getProducts=' + zoekterm,
				success	:	function(r) {
					$('#autocompleet').html(r);
					if(r != '') {
						$('#autocompleet').slideDown(500);
					}
					else {
						$('#autocompleet').hide();
					}
				}
			});
		}
	});
	
	//	Producten overzicht
	$('.producten .product').mouseover(function() {
		$(this).css({background:'url(images/product_item_hover.gif)'});
	});
	$('.producten .product').mouseout(function() {
		$(this).css({background:'url(images/product_item.gif)'});
	});
	$('.producten .product .bekijk').click(function() {
		loadPopup($(this).parent().attr('id'));
	});
	//	Formulieren
	$('.formulier .text').click(function() {
		$(this).css({border:'1px #154273 solid'});
	});
	$('.formulier .text').focus(function() {
		$(this).css({border:'1px #154273 solid'});
	});
	$('.formulier .text').blur(function() {

		var sValidate = frmValField($(this).attr('name'), $(this).val(), 'overige');
		var bValidate = sValidate == '' ? true : false;
		
		$(this).parent().next().children().empty();
		$(this).parent().next().next().children().empty();
		if(bValidate) {
			$(this).css({border:'1px #8ca2ba solid'});
			$(this).parent().next().children().append('<img src="images/val_oke.gif" />');	
		}
		else {
			$(this).css({border:'1px #aa0909 solid'});
			$(this).parent().next().children().append('<img src="images/val_err.gif" />');
			$(this).parent().next().next().children().append(sValidate);
		}
	});
	$('.formulier .submit').click(function() {
		if($(this).parent().parent().parent().parent().parent().attr('id') == 'frm_bestellen') {
			var check = false;
			$('.formulier #frm_bestellen .val').each(function() {
				var name 	= 	$(this).parent().prev().children().attr('name');
				var value	=	$(this).parent().prev().children().val();
				var error	=	frmValField(name, value, 'overige');
				if(error) {
					check = true;
					$(this).html('<img src="images/val_err.gif" />');
					$(this).parent().next().children().html(error);
					$(this).parent().prev().children().css({border:'1px #aa0909 solid'});
				}
			});
			if(!check) {
				$.ajax({
					type	:	'POST',
					url		:	'winkelwagen.php',
					data	:	'&action=checkBetaalmethode',
					success	:	function(r) {
						if(r == 'geen') {
							$('#betaal_v').html('<img src="images/val_err.gif" />');
							$('#betaal_vm').html('Kies een betaalmethode');
							$('.formulier .radio').css({color:'#aa0909'});
						}
						else {
							var bestaalmethod = r;
							$('#betaal_v').html('<img src="images/val_oke.gif" />');
							$('#betaal_vm').html('');
							$('.formulier .radio').css({color:'#8ca2ba'});
							$.ajax({
								type	:	'POST',
								url		:	'winkelwagen.php',
								data	:	'&action=sendOrder',
								success	:	function(r) {
									$('.winkelwagen').prev().hide();
									$('.winkelwagen').hide();
									if(bestaalmethod == 'ideal') {
										//	Bedrag
										var amout = 100; //100 EURO
										var amout = Math.round(parseFloat(amout * 100));
										//	OrderID
										var orderID = 1;
										mydate = new Date();
										tv = mydate.getYear() % 10;
										orderID = orderID + tv;
										tv = (mydate.getMonth() * 31) + mydate.getDate();
										orderID = orderID + ((tv < 10) ? '0' : '') + ((tv < 100) ? '0' : '') + tv;
										tv = (mydate.getHours() * 3600) + (mydate.getMinutes() * 60) + mydate.getSeconds();
										orderID = orderID + ((tv < 10) ? '0' : '') + ((tv < 100) ? '0' : '') + ((tv < 1000) ? '0' : '') + ((tv < 10000) ? '0' : '') + tv;
										tvplus = Math.round(Math.random() * 9);		
										var orderID = (orderID + ((tvplus + 1) % 10));
										$('#bestellen').html('<h1>Bestellen</h1><div class="tekst">Hartelijk dank voor uw bestelling. We nemen op korte termijn contact met u op.<br /><br/><br/><br/><br/><br/>Binnen 10 seconden word iDeal geladen, gebeurt dat niet? klik dan <a href="javascript:$(\'#frm_ideal\').submit();">hier</a>.<form method="post" id="frm_ideal" name="frm_ideal" action="https://internetkassa.abnamro.nl/ncol/prod/orderstandard.asp"><input type="hidden" name="PSPID" value="breevaart" /><input type="hidden" name="orderID" id="orderID" value="' + orderID + '" /><input type="hidden" name="amount" id="amount" value="' + amout + '" /><input type="hidden" name="currency" value="EUR" /><input type="hidden" name="language" value="NL_NL" /><input type="hidden" name="PM" value="iDEAL" /></form></div><script type="text/javascript">window.setTimeout(function(){$(\'#frm_ideal\').submit();},10000)</script>');
									}
									else {
										$('#bestellen').html('<h1>Bestellen</h1><div class="tekst">Hartelijk dank voor uw bestelling. We nemen op korte termijn contact met u op. <br/><br/><br/><br/><br/></div>');
									}
									$.ajax({
										type	:	'POST',
										url		:	'update_cart.php',
										data	:	'&type=del_all',
										success	:	function(r) {
											$('#info_aantal').html('0');
											$('#info_prijs').html('&euro; 0,00');
										}
									});
									$.ajax({
										type	:	'POST',
										url		:	'send_form.php',
										data	:	'&form=delete&frm_name=frm_bestellen'
									});
								}
							});
						}
					}
				});
			}
		}
		else if($(this).parent().parent().parent().parent().parent().attr('id') == 'frm_offerte') {
			var check = false;
			$('.formulier #frm_offerte .val').each(function() {
				var name 	= 	$(this).parent().prev().children().attr('name');
				var value	=	$(this).parent().prev().children().val();
				var error	=	frmValField(name, value, 'offerte');
				if(error) {
					check = true;
					$(this).html('<img src="images/val_err.gif" />');
					$(this).parent().next().children().html(error);
					$(this).parent().prev().children().css({border:'1px #aa0909 solid'});
				}
			});
			if(!check) {
				$('#offert_aanvragen').html('<h1>Offert aanvragen</h1><div class="tekst">Uw offerte aanvraag word nu verzonden.</div>');
				var form_values = $(this).parent().parent().parent().parent().parent().serialize();
				$.ajax({
					type	:	'POST',
					url		:	'send_form.php',
					data	:	'&form=offerte&' + form_values,
					success	:	function(r) {
						if(r == 'verzonden') {
							$('#offert_aanvragen').html('<h1>Offert aanvragen</h1><div class="tekst">Uw offerte aanvraag is verzonden.<br /><br />Wij danken uw voor uw offerte aanvraag.</div>');
							$.ajax({
								type	:	'POST',
								url		:	'send_form.php',
								data	:	'&form=delete&frm_name=frm_offerte'
							});
						}
						else {
							$('#offert_aanvragen').html('<h1>Offert aanvragen</h1><div class="tekst">Er is een fout opgetreden tijdens het verzenden van uw offerte aanvraag, onze excuses voor het ongemak.</div>');
						}
					}
				});
			}
		}
		else if($(this).parent().parent().parent().parent().parent().attr('id') == 'frm_contact') {
			var check = false;
			$('.formulier #frm_contact .val').each(function() {
				var name 	= 	$(this).parent().prev().children().attr('name');
				var value	=	$(this).parent().prev().children().val();
				var error	=	frmValField(name, value, 'contact');
				if(error) {
					check 	= 	true;
					$(this).html('<img src="images/val_err.gif" />');
					$(this).parent().next().children().html(error);
					$(this).parent().prev().children().css({border:'1px #aa0909 solid'});
				}
			});
			if(!check) {
				
				var form_values 	= 	'';//$(this).parent().parent().parent().parent().parent().serialize();
				
				
				var naam		=	$('.formulier input').val();
				var telefoon		=	$('.formulier input[name=telefoon]').val();
				var email		=	$('.formulier input[name=emailadres]').val();
				var opmerkingen		=	$('.formulier textarea[name=opmerkingen]').val();
				
				form_values		=	'naam='+naam+'&telefoon='+telefoon+'&emailadres='+email+'&opmerkingen='+opmerkingen;
				
				//form_values		=	form_values.serialize();
				//alert(form_values.serialize());
				//return false;
				//$('#contact_form').html('Uw bericht word nu verzonden.');
				
				$.ajax({
					type	:	'POST',
					url		:	'send_form.php',
					data	:	'&form=contact&' + form_values,
					success	:	function(r) {
						if(r == 'verzonden') {
							$('#contact_form').html('Bedank voor uw bericht. Op werkdagen krijgt u binnen 24 uur een reactie.');
							$.ajax({
								type	:	'POST',
								url		:	'send_form.php',
								data	:	'&form=delete&frm_name=frm_contact'
							});
						}
						else {
							$('#contact_form').html('Er is een fout opgetreden tijdens het verzenden van het formulier, onze excuses voor het ongemak.');
						}
					}
				});
				
			}
		}
	});
	$('.winkelwagen .delete').click(function() {
		if(confirm('Weet u zeker dat u dit product uit uw winkelwagen wilt verwijderen?')) {
			var product = $(this).attr('id').split('_')[1];
			$.ajax({
				type	:	'POST',
				url		:	'delete_product.php',
				data	:	'&delete=' + product,
				success	:	function() {
					location.reload();
				}
			});
			/*
			$(this).parent().parent().next().hide();
			$(this).parent().parent().remove();
			$.ajax({
				type	:	'POST',
				data	:	'type=subtotaal',
				url		:	'update_cart.php',
				success	: function(msg) {
					$('#subtotaal').html(msg);
					$('#info_prijs').html(msg);
				}
			});
			$.ajax({
				type	:	'POST',
				data	:	'type=totaal',
				url		:	'update_cart.php',
				success	: function(msg) {
					$('#totaal').html('<strong>' + msg + '</strong>');
				}
			});
			$.ajax({
				type	:	'POST',
				data	:	'type=aantal',
				url		:	'update_cart.php',
				success	: function(msg) {
					$('#info_aantal').html(msg);
				}
			});
			$.ajax({
				type	:	'POST',
				data	:	'type=offerte',
				url		:	'update_cart.php',
				success	: function(msg) {
					if(msg == 'geen') {
						if($('#offert_aanvragen').css('display') != 'none') {
							$('#offert_aanvragen').css({display:'none'});
							$('#offerte_aanvragen_minimaal').css({color:'#aa0909'});
						}
					}
				}
			});
			$.ajax({
				type	:	'POST',
				url		:	'winkelwagen.php',
				data	:	'&action=checkProducts',
				success	:	function(r) {
					if(r == 0) {
							location.href = 'winkelwagen.html';	
						$('.rechts').html('<h1>Winkelwagen</h1><div class="tekst">Uw winkelwagen is leeg.</div>');
						$('.rechts').append('<div class="r_tp"></div>');
						$('.r_tp').css({position:'absolute',left:'0px',top:'0px',width:'686px',height:'3px',background:'url(images/content_top.gif)'});
						$('.rechts').append('<div class="r_bt"></div>');
						$('.r_bt').css({position:'absolute',left:'0px',bottom:'0px',width:'686px',height:'3px',background:'url(images/content_bottom.gif)'});
						$('#winkelwagen').html('<strong>Winkelwagen</strong><br />U winkelwagen is leeg.');
						$('#winkelwagen').append('<div id="w_tp"></div>');
						$('#w_tp').css({position:'absolute',left:'0px',top:'0px',width:'200px',height:'2px',background:'url(images/winkelwagen_top.gif)'});
						$('#winkelwagen').append('<div id="w_bt"></div>');
						$('#w_bt').css({position:'absolute',left:'0px',bottom:'0px',width:'200px',height:'2px',background:'url(images/winkelwagen_bottom.gif)'});
						$('#winkelwagen').click(function() {
							location.href = 'winkelwagen.html';	
						});
					}
				}
			});
			*/
		}
	});
	
	//	Winkelwagen update
	var input_val = '0';
	$('.winkelwagen td.i input').click(function() {
		if($(this).val() != '') {
			input_val = $(this).val();
			$(this).val('');
		}
	}).focus(function() {
		if($(this).val() != '') {
			input_val = $(this).val();
			$(this).val('');
		}
	}).blur(function() {
		if(input_val == $(this).val() || $(this).val() == '') {
			$(this).val(input_val);
		}
		else {
			var product = $(this).attr('id').split('_');
			var aantal  = $(this).val();
			if($(this).val() < 1) {
				aantal  = 1;
			}
			$(this).val(aantal);
			$.ajax({
				type	:	'POST',
				data	:	'type=update&product=' + product[1] + '&aantal=' + aantal,
				url		:	'update_cart.php',
				success	: function(msg) {
					$.ajax({
						type	:	'POST',
						data	:	'type=totaal_product&product=' + product[1],
						url		:	'update_cart.php',
						success	: function(msg) {
							$('#sub_pro_' + product[1]).html(msg);
						}
					});
					$.ajax({
						type	:	'POST',
						data	:	'type=subtotaal',
						url		:	'update_cart.php',
						success	: function(msg) {
							$('#subtotaal').html(msg);
							$('#info_prijs').html(msg);
						}
					});
					$.ajax({
						type	:	'POST',
						data	:	'type=totaal',
						url		:	'update_cart.php',
						success	: function(msg) {
							$('#totaal').html('<strong>' + msg + '</strong>');
						}
					});
					$.ajax({
						type	:	'POST',
						data	:	'type=verzendk',
						url		:	'update_cart.php',
						success	: function(msg) {
							$('#verzendK').html(msg);
						}
					});
					$.ajax({
						type	:	'POST',
						data	:	'type=aantal',
						url		:	'update_cart.php',
						success	: function(msg) {
							$('#info_aantal').html(msg);
						}
					});
					$.ajax({
						type	:	'POST',
						data	:	'type=offerte',
						url		:	'update_cart.php',
						success	: function(msg) {
							if(msg == 'geen') {
								if($('#offert_aanvragen').css('display') != 'none') {
									$('#offert_aanvragen').css({display:'none'});
									$('#offerte_aanvragen_minimaal').css({color:'#aa0909'});
								}
							}
						}
					});
				}
			});
		}
	});
	
	//	Offerte aanvragen
	$('.offerte_aanvragen').click(function() {
										   
		var subtotaal 	=	$('#subtotaal').text();
		subtotaal		=	subtotaal.split(',');
		subtotaal		=	subtotaal[0].replace('.', '');
		subtotaal		=	parseInt(subtotaal.substr(2, (subtotaal.length - 2)));

		if(subtotaal < 500) {
			$('#offerte_aanvragen_minimaal').css({
				color	:	'#aa0909'
			});
			$.ajax({
				type	:	'POST',
				data	:	'type=offerte_aanvragen&offerte_aanvragen=0',
				url		:	'update_cart.php'
			});
		}
		else {
			$('#bestellen').hide();
			$('#offerte_aanvragen_minimaal').css({
				color	:	'#154273'
			});
			if($('#offert_aanvragen').attr('style') != '') {
				$('#offert_aanvragen').show();
				$.ajax({
					type	:	'POST',
					data	:	'type=offerte_aanvragen&offerte_aanvragen=1',
					url		:	'update_cart.php'
				});
				$.ajax({
					type	:	'POST',
					data	:	'type=bestellen&bestellen=0',
					url		:	'update_cart.php'
				});
			}
		}
		
	});
	
	//	Bestellen 
	$('.bestellen').click(function() {
		$('#offerte_aanvragen_minimaal').css({
			color	:	'#154273'
		});
		$('#offert_aanvragen').hide();
		$.ajax({
			type	:	'POST',
			data	:	'type=offerte_aanvragen&offerte_aanvragen=0',
			url		:	'update_cart.php'
		});
		$.ajax({
			type	:	'POST',
			data	:	'type=bestellen&bestellen=1',
			url		:	'update_cart.php'
		});
		$('#bestellen').show();
	});
	
	// Form values opslaan
	$('.formulier input').blur(function() {
		var form = $(this).parent().parent().parent().parent().parent().attr('id');
		$.ajax({
			type	:	'POST',
			data	:	'form=' + form + '&field=' + $(this).attr('name') + '&value=' + $(this).val(),
			url		:	'form_values.php'
		});
	});
	$('.formulier textarea').blur(function() {
		var form = $(this).parent().parent().parent().parent().parent().attr('id');
		$.ajax({
			type	:	'POST',
			data	:	'form=' + form + '&field=' + $(this).attr('name') + '&value=' + $(this).val(),
			url		:	'form_values.php'
		});
	});
	
	$('.formulier .text').each(function() {
		if($(this).val() != '') {
			var sValidate = frmValField($(this).attr('name'), $(this).val(), 'overige');
			var bValidate = sValidate == '' ? true : false;
			
			$(this).parent().next().children().empty();
			$(this).parent().next().next().children().empty();
			if(bValidate) {
				$(this).css({border:'1px #8ca2ba solid'});
				$(this).parent().next().children().append('<img src="images/val_oke.gif" />');	
			}
			else {
				$(this).css({border:'1px #aa0909 solid'});
				$(this).parent().next().children().append('<img src="images/val_err.gif" />');
				$(this).parent().next().next().children().append(sValidate);
			}
		}
	});
	
	$('.formulier td.radio').click(function() {
		if($(this).hasClass('av')) {
			if($(this).hasClass('checked')) {
				$('.formulier td.radio.av').removeClass('checked');
				var form = $(this).parent().parent().parent().parent().parent().attr('id');
				$.ajax({
					type	:	'POST',
					data	:	'form=frm_bestellen&field=av&value=',
					url		:	'form_values.php'
				});
				$(this).next().children('.val').html('<img src="images/val_err.gif" />');
				$(this).next().next().children('.val_m').text('U dient akkoord te gaan met de algemene voorwaarden.');
				$('.formulier .radio.av').css({color:'#aa0909'});
			}
			else {
				$('.formulier td.radio.av').addClass('checked');
				var form = $(this).parent().parent().parent().parent().parent().attr('id');
				$.ajax({
					type	:	'POST',
					data	:	'form=frm_bestellen&field=av&value=' + $(this).attr('value'),
					url		:	'form_values.php'
				});
				$(this).next().children('.val').html('<img src="images/val_oke.gif" />');
				$(this).next().next().children('.val_m').text('');
				$('.formulier .radio.av').css({color:'#154273'});
			}
		}
		else {
			$('.formulier td.radio.bm').removeClass('checked');
			$(this).addClass('checked');
			var form = $(this).parent().parent().parent().parent().parent().attr('id');
			$.ajax({
				type	:	'POST',
				data	:	'form=frm_bestellen&field=betaalmethode&value=' + $(this).attr('value'),
				url		:	'form_values.php'
			});
			$(this).next().children('.val').html('<img src="images/val_oke.gif" />');
			$(this).next().next().children('.val_m').text('');
			$('.formulier .radio.bm').css({color:'#154273'});
		}
	});
	
});

function frmValField(el, val, frm) {
	if(frm == 'offerte' && el == 'bedrijf' && val == '') {
		return 'Dit veld is verplicht.';	
	}
	else {
		switch(el) {
			case 'plaats':
			case 'naam':
				if(val == '') {
					return 'Dit veld is verplicht.';	
				}
			break;
			case 'adres':
				if(val == '') {
					return 'Een geldig adres + huisnr. is verplicht.';
				}
			break;
			case 'postcode':
				val	= val.replace(' ', '');
				if(val.length != 6) {
					return 'Een geldige postcode is verplicht.';
				}
			break;
			case 'telefoon':
				if(val.length < 10) {
					return 'Een geldig telefoonnummer is verplicht.';
				}
			break;
			case 'emailadres':
				var emailadres 	= val.split('@');
				if(emailadres.length == 1 || emailadres[0].length < 1 || emailadres[1].length < 2) {
					return 'Een geldig e-mailadres is verplicht.';
				}
				else {			
					var extentie	= emailadres[1].split('.');
					if(extentie.length == 1 || extentie[1].length < 2 || extentie[1].length > 3) {
						return 'Een geldig e-mailadres is verplicht.';
					}
				}
			break;
		}
	}
	return false;	
}

function loadPopup(pdID) {
	
	//	Popupbox
	var popup_left 	= ($(window).width() - 600) / 2 + 'px';
	var popup_top	= ($(window).height() - 400) / 2 + 'px';
	var pd_id		= pdID.split('_');
	pd_id			= pd_id[1];
	
	$('body').prepend('<div id="layer"></div>');
	$('body').prepend('<div id="popup"></div>');
	
	$('#popup').prepend('<span class="laden">Product wordt geladen...</span>');
	
	$('#layer').css({position:'fixed',left:'0px',top:'0px',width:'100%',height:'100%',background:'#000',zIndex:'250',display:'none',opacity:'0.33',zIndex:250});
	$('#popup').css({position:'fixed',left:popup_left,top:popup_top,zIndex:300});	
	$('#layer').fadeIn('slow');
	$('#popup').fadeIn('slow');
	
	$('#layer').click(function() {
	   $('#popup').hide();
		$('#layer').fadeOut('slow');
	});
	
	$.ajax({
		url: 'product.php?product=' + pd_id,
		success: function(msg) {
			$('#popup').css({background:'#fff'});
			$('#popup .laden').hide();
			$('#popup').html(msg);
			$('.sluiten').click(function() {
				$('#popup').hide();
				$('#layer').fadeOut('slow');
			});
			$('#popup .product input.pcount').click(function() {
				$(this).css({border:'1px #154273 solid'});
				if($(this).val() == 1) {
					$(this).val('');
				}
			});
			$('#popup .product input.pcount').focus(function() {
				$(this).css({border:'1px #154273 solid'});
				if($(this).val() == 1) {
					$(this).val('');
				}
			});
			$('#popup .product input.pcount').blur(function() {
				$(this).css({border:'1px #8ca2ba solid'});
				if($(this).val() == '') {
					$(this).val('1');
				}
			});
			$('#popup .product input.breedte').blur(function() {
				calculate('');
			});
			$('#popup .product input.hoogte').blur(function() {
				calculate('');
			});
			$('#popup .product .radio').click(function() {
				calculate('click');
			});
		}
	});
	
}

function calculate(ev) {
	
	//	Alle afmetingen in centimeters
	if(ev == 'click') {
		if($('.calculator .radio').attr('class') == 'radio') {
			$('.calculator .radio').attr('class', 'radio checked');
			var snijverlies		=	true;
		}
		else {
			$('.calculator .radio').attr('class', 'radio');
			var snijverlies		=	false;
		}
	}
	else {
		if($('.calculator .radio').attr('class') == 'radio') {
			var snijverlies		=	false;
		}
		else {
			var snijverlies		=	true;
		}
	}
	
	var wand_breedte		=	parseInt($('#popup .product input.breedte').val());
	if(isNaN(wand_breedte)) {
		wand_breedte		=	0;	
	}
	var wand_hoogte			=	parseInt($('#popup .product input.hoogte').val());
	if(isNaN(wand_hoogte)) {
		wand_hoogte			=	0;	
	}
	
	var tegel_b				=	$('#popup .product input.breedte').attr('id').split('_');
	var tegel_h				=	$('#popup .product input.hoogte').attr('id').split('_');
	
	var tegel_breedte		=	parseInt(tegel_b[1]);
	var tegel_hoogte		=	parseInt(tegel_b[1]);
	var tegel_voeg			=	0.3;
	
	var aantal_breedte		=	Math.ceil(wand_breedte / (tegel_breedte + tegel_voeg));
	var aantal_hoogte		=	Math.ceil(wand_hoogte / (tegel_hoogte + tegel_voeg));
	
	var aantal_tegels		=	(aantal_breedte * aantal_hoogte);
	if(snijverlies) {
		aantal_tegels		=	aantal_tegels * 1.10;
	}
	
	aantal_tegels			=	Math.ceil(aantal_tegels);
	
	if(aantal_tegels > 0) {
		$('.pcount').val(aantal_tegels);
	}
	
}

function addProduct(pid) {
	$.ajax({
		type	:	'GET',
		url		:	'http://www.tegelgalerie.nl/shop_actions.php?addProduct=' + pid + '&aantal=1',
		success	:	function(response) {
			if(response == 'offerte') {
				location.href = 'http://www.tegelgalerie.nl/offerte-aanvragen.html';
			}
			else {
				location.href = 'http://www.tegelgalerie.nl/winkelwagen.html';
			}
		}
	});
}

function loadPage(nPageID) {
	$('.subitems a').css({fontWeight:'normal'});
	$('.item').css({fontWeight:'normal'});
	$('#subm_' + nPageID).css({fontWeight:'bold'});
	$('#toon').hide();
	$.ajax({
		type	:	'POST',
		data	:	'page=' + nPageID,
		url		:	'loadpage.php',
		success	: function(msg) {
			$('#container .rechts.main').html(msg);
			$('.rechts').append('<div class="r_tp"></div>');
			$('.r_tp').css({position:'absolute',left:'0px',top:'0px',width:'686px',height:'3px',background:'url(images/content_top.gif)'});
			$('.rechts').append('<div class="r_bt"></div>');
			$('.r_bt').css({position:'absolute',left:'0px',bottom:'0px',width:'686px',height:'3px',background:'url(images/content_bottom.gif)'});
		}
	});
	$.ajax({
		type	:	'POST',
		data	:	'page=' + nPageID,
		url		:	'sitepath.php',
		success	: function(response) {
			$('#sitepath').html(response);
		}
	});
}

function loadProducts(nPageID, nPageNumber) {
	$('.tekst span').text(nPageNumber);
	$('.paging a').removeClass('select');
	$.ajax({
		type	:	'POST',
		data	:	'page=' + nPageID + '&number=' + nPageNumber,
		url		:	'loadproducts.php',
		success	: function(response) {
			$('.producten').html(response);
			$('html,body').animate({scrollTop: '0px'}, 0);
		}
	});
}

function setWeergave(type) {
	$.ajax({
		type	:	'POST',
		data	:	'type=weergave&m=' + type,
		url		:	'update_cart.php',
		success	: function(response) {
			 location.reload();
		}
	});
}
