function toggleShopOverlay(visible){
	var overlay		= $('shopoverlay');
	var background	= overlay.getElement('div.background');
	
	// Fix background height
	background.setStyle('height', window.getScrollHeight());
	
	// Start transition
	background.set('tween', {duration: 1000, transition: Fx.Transitions.quintOut});
	
	if(visible){
		background.setStyle('opacity', 0);
		background.tween('opacity', 0.7);
		overlay.setStyle('display', 'block');
		overlay.setStyle('visibility', 'visible');
	}else{
		background.tween('opacity', 0);
		overlay.setStyle('visibility', 'hidden');
	}
}

window.addEvent('load', function(){
	$$('.right_content form').each(function (form) {
		
		if(form.id == 'shoppingcartform') return false;

		form.addEvent('submit', function(e){
			new Event(e).stop();
			
			if(Fx.Scroll)
			{
				var scroller = new Fx.Scroll(document.body);
				scroller.toTop();
			}
			
			new Request({
				method: 'post',
				url: '/shoppingcart/jsonplus/' + form.getElements('input')[0].value + '/' + form.getElements('select')[0].value,
				onSuccess: function(txt)
				{
					var naam = JSON.decode(txt).result.name;
					var image = JSON.decode(txt).result.image;
					var price = JSON.decode(txt).totalPrice;
					var quantity = JSON.decode(txt).totalQuantity;
					var product_text = 'producten';
					
					function number_format( number, decimals, dec_point, thousands_sep ) {
						var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
						var d = dec_point === undefined ? "," : dec_point;
						var t = thousands_sep === undefined ? "." : thousands_sep, s = n < 0 ? "-" : "";
						var i = parseInt(n = Math.abs(+n || 0).toFixed, 10) + "", j = (j = i.length) > 3 ? j % 3 : 0;
						
						return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
					}
					
					price = number_format(price, 2, ',', '.');
					
					if (image === null)
					{
						image = '';
					}
					
					if (quantity == 1)
					{
						product_text = 'product';
					}
					
					$('cart_products_').set('text', quantity + ' ' + product_text);
					$('cart_price_').set('text', price);
					
					if($('shopoverlay'))
					{
						$('shopoverlay').getElement('h3').set('text', naam);
						$('shopoverlay').getElement('img').set('src', '/image/productthumb?file=' + image);
					}
					else
					{
						window.location.reload();
					}
					
					toggleShopOverlay(true);
				}
			}).send();
		});
	});
	
	if($$('.close')[0])
	{
		$$('.close')[0].addEvent('click', function(e){
			e = new Event(e).stop();
			
			toggleShopOverlay(false);
		});
	}
});

