// JavaScript Document

	$(document).ready(function(){
							   
		// Add event to 'Add Product' forms
		$('form[name="addProductToCart"]').submit(function(){
			amount = this['amount'].value
			product = this['product'].value
			option = this['option'].value
			cart_add(amount, product, option)
			return false
		})
		
		cart_updateContentOverview();
	})
	
	
	/*
	*	cart_add
	*
	*	@descr	Add a product to the cart via AJAX
	*/
	function cart_add(amount, product, option){
		$.ajax({
			type: 'post',
			url: 'nl/winkelen/winkelwagen/add',
			dataType: 'json',
			data: {
				amount: amount, 
				product: product, 
				option: option, 
				method: 'ajax'
			},
			success: function(data){
				if(data){
					cart_popup('<img src="template/default/nl/images/add-cart-popup-icon.jpg" class="popupicon" /><h5>Product toegevoegd aan wagentje!</h5><p>Het product is toegevoegd aan uw winkelwagentje. <br/> Wat wilt u nu doen?</p><p><a href="#" class="button">Verder winkelen</a>&nbsp;&nbsp;of&nbsp;&nbsp;<a href="nl/winkelen/winkelwagen" class="button">Wagentje bekijken</a></p>')
					$('.cartnotification a.button:first').focus().click(function(){
						cart_popup_exit()
						return false
					})
					cart_updateOverview()
				} else
					alert('Onbekende fout opgetreden, ververs de pagina.')
			}
		})
	}
	
	
	/*
	*	cart_edit
	*
	*	@descr	Update a product in the cart via AJAX
	*/
	function cart_edit(amount, product, option){
		$.ajax({
			type: 'post',
			url: 'nl/winkelen/winkelwagen/edit',
			dataType: 'json',
			data: {
				amount: amount, 
				product: product, 
				option: option, 
				method: 'ajax'
			},
			success: function(data){
				if(data){
					cart_updateOverview()
					cart_updateContentOverview()
				} else
					alert('Onbekende fout opgetreden, ververs de pagina.')
			}
		})
	}
	
	
	/*
	*	cart_popup
	*
	*	@descr	Display a popup
	function cart_popup(){
		message = '<div class="overlay"></div><div class="cartnotification"><img src="template/default/nl/images/add-cart-popup-icon.jpg" class="popupicon" /><h5>Product toegevoegd aan wagentje!</h5><p>Het product is toegevoegd aan uw winkelwagentje. <br/> Wat wilt u nu doen?</p><button onclick="$(this).parent().fadeOut(\'fast\', function(){$(this).prev().fadeOut(\'fast\', function(){$(this).remove()}); $(this).remove()})" >Verder winkelen</button> of <button onclick="document.location = \'nl/winkelen/winkelwagen\'">Winkelwagentje bekijken</button></div>'
		$('body').prepend(message)
		$('div.overlay').fadeTo(0, 0).fadeTo('fast', 0.5)
		$('div.cartnotification').fadeIn('fast')
	}
	*/
	function cart_popup(message){
		popup = '<div class="overlay"></div><div class="cartnotification">' + message + '</div>'
		$('body').prepend(popup)
		$('div.overlay')
			.fadeTo(0, 0)
			.fadeTo('fast', 0.5)
			.click(function(){
				 cart_popup_exit()
			})
		$('div.cartnotification').fadeIn('fast')
	}


	/*
	*	cart_popup_exit
	*	
	*	@descr	Remove the popup from the page
	*/
	function cart_popup_exit(e){
		popup = $('body > .cartnotification')
		overlay = $('body > .overlay')
		$(popup).fadeOut("fast", function(){
			$(popup).remove()
		})
		$(overlay).fadeOut("fast", function(){
			$(overlay).remove()
		})
	}
	
	
	/*
	*	cart_updateOverview
	*	
	*	@descr	Update the overview box on the website after cart content changes.
	*/
	function cart_updateOverview(){
		$.ajax({
			type: 'post',
			url: 'nl/winkelen/winkelwagen/overview',
			dataType: 'json',
			success: function(data){
				if(data)
					$('#shoppingcartOverview #holder').fadeOut(500, function(){
						$('#shoppingcartOverview #items').html(data['amount'] + ' items')
						$('#shoppingcartOverview #cost').html('&euro; ' + data['price'])
						$('#shoppingcartOverview #holder').fadeIn(300)
					})
				else
					alert('Onbekende fout opgetreden, ververs de pagina.')
			}
		})
	}


	/*
	*	cart_updateContentOverview
	*	
	*	@descr	Update the overview box on the website after cart content changes.
	*/
	function cart_updateContentOverview(){
		$.ajax({
			type: 'post',
			url: 'nl/winkelen/winkelwagen/contentoverview',
			dataType: 'json',
			success: function(data){
				if(data){
					$('#cartContentOverview').fadeOut(500, function(){
						$('#cartContentOverview').html(data)
						$('#cartContentOverview').fadeIn(200)
						
						$('#cartContentOverview input.amount').bind("focus", function(){
							elem = this
							cart_popup('<img src="template/default/nl/images/edit-cart-popup-icon.jpg" class="popupicon" /><h5>Aantal wijzigen</h5><p>Hoeveel producten wilt u bestellen?<br/><br/></p><form><input type="text" name="amount" />&nbsp;&nbsp;<a href="#" class="button">Wijzigen</a></form>')
							$('.cartnotification input[name="amount"]').val(elem.value).focus();
							$('.cartnotification form').submit(function(){
								if($('.cartnotification input[name=amount]').val() != elem.value)
									cart_edit($('.cartnotification input[name=amount]').val(), elem.name.substring(4), elem.id.substring(4)) 
								cart_popup_exit()
								return false
							})							
							$('.cartnotification a.button').click(function(){
							   $('.cartnotification form').submit()
							   return false
							})
						})
						$('#cartContentOverview img.deleteproduct').bind("click dblclick", function(){
							elem = this
							cart_popup('<img src="template/default/nl/images/delete-cart-popup-icon.jpg" class="popupicon" /><h5>Product uit wagentje halen?</h5><p>Wilt u dit product uit uw winkelwagentje halen?<br/><br/></p><a href="#" class="button">Ja, haal weg</a>&nbsp;&nbsp;of&nbsp;&nbsp;<a href="#" class="button">Nee, bewaar</a>')
							$('.cartnotification a.button:last').focus().click(function(){
								cart_popup_exit()
								return false
							})
							$('.cartnotification a.button:first').click(function(){
								cart_edit(0, elem.name.substring(4), elem.id.substring(4)) 
								cart_popup_exit()
								return false
							})							
						})
					})
				} else
					alert('Onbekende fout opgetreden, ververs de pagina.')
			}
		})
	}
	
	function cart_changed(e){
		$('#testval').val(e.value)
	}
	