$( function () {
	$("form[name=cart_quantity]").submit( function (evenement) {
		evenement.preventDefault();
		
		$form = $(this);
		
		$.ajax({
			url: 'add_to_shopping_cart.php',
			type: 'POST',
			data: $("form[name=cart_quantity]").serialize(),
			dataType: 'json',
			beforeSend: function () {
				$form.find('input[type=image]').after('<img src="images/ajax-loader.gif" id="ajax-loader" />');
			},
			success: function (data) {
				$("img#ajax-loader").remove();
				
				$html_output = '';
				
				$.each(data.contents, function (key, elem) {
					$html_output += elem;
				});
				
				$html_output += data.footer;
				
				$(".shopping_cart_box").html( $html_output );
				
				$lightbox_content = '';
				
				$lightbox_content += '<h1>Het volgende product is aan uw winkelwagen toegevoegd:</h1>';
				$lightbox_content += data.add_product;
				
				$lightbox_content += '<div class="clear"></div>';
				$lightbox_content += '<div id="continue_shopping"></div>';
				$lightbox_content += '<div id="checkout"></div>';
				
				showLightbox($lightbox_content);
				
				$("div#checkout").click( function () {
					parent.location.href = data.checkout_link;
				});
				
				$("div#continue_shopping, div#lightBoxOpacity").click( function (evenement) {
					$("div#warningBox, div#lightBoxOpacity").remove();
					evenement.preventDefault();
				});
			}
		});
	});
	
	$("img.bestellen").click( function (evenement) {
		evenement.preventDefault();
		
		$form = $(this);
		
		$reg = /^(.*)-p-/;
		$reg2 = /\.html(.*)/;
		$products_id = $(this).parent().attr('href').replace($reg, '').replace($reg2, '');
		
		$.ajax({
			url: 'add_to_shopping_cart.php?products_id=' + $products_id + '&action=buy_now_ajax',
			type: 'GET',
			dataType: 'json',
			beforeSend: function () {
				$form.after('<img src="images/ajax-loader-small.gif" id="ajax-loader" />');
			},
			success: function (data) {
				$("img#ajax-loader").remove();
				
				if(data){							
					if(data.redirect == 'true') {
						parent.location.href = data.redirect_url;
					} else {
						$html_output = '';
						
						$.each(data.contents, function (key, elem) {
							$html_output += elem;
						});
						
						$html_output += data.footer;
						
						$(".shopping_cart_box").html( $html_output );
						
						$lightbox_content = '';
						
						$lightbox_content += '<h1>Het volgende product is aan uw winkelwagen toegevoegd:</h1>';
						$lightbox_content += data.add_product;
						
						$lightbox_content += '<div class="clear"></div>';
						$lightbox_content += '<div id="continue_shopping"></div>';
						$lightbox_content += '<div id="checkout"></div>';
						
						showLightbox($lightbox_content);
						
						$("div#checkout").click( function () {
							parent.location.href = data.checkout_link;
						});
						
						$("div#continue_shopping, div#lightBoxOpacity").click( function (evenement) {
							$("div#warningBox, div#lightBoxOpacity").remove();
							evenement.preventDefault();
						});
					}
				}
			}
		});
	});
});

function showLightbox($lightbox_content) {
	$("body")
		.append('<div id="lightBoxOpacity"></div>')
		.append('<div id="warningBox">' + $lightbox_content + '</div>')
			.find('div#lightBoxOpacity')
				.css('opacity', 0)
				.animate({
					opacity: 0.5
				}, 500);
}
