  function LoadCartSummary()
     {
     		var gourl = baseurl + '/overlay/'+language+'/cart/summary/';
     		
				$.get(gourl,{},function(data) {
										 	$("#cartsummary").empty().append(data);
								
								}
				);
				
     }
     
     function LoadCartOverlay(options)
     {
     	 $("body").overlay({scroll:false,boxtop:150,contenturl:  baseurl + '/overlay/'+language.toLowerCase()+'/cart/?'+options,onloadhandler:OnLoadCartHandler,onclosehandler:OnExitCartHandler});
     }
     
     function OnExitCartHandler()
     {

     	  //make sure everything is updated even though no blur event was fired
     	  $('.number_textfield').each(function() {
					 var product = $(this).attr("id");
					 var prod_attr = product.split("_");
					 var textfield_value = parseInt($(this).val());

					 if(textfield_value > 0)
					 {
					 	  shoppingcart.setProduct(prod_attr[1],prod_attr[2],textfield_value,$('#price_'+prod_attr[1]+'_'+prod_attr[2]).val(),'');

					 }
					 else if(textfield_value <= 0)
					 {
					 	  shoppingcart.deleteProduct(prod_attr[1],prod_attr[2],'');

     	  	 }

					 
				});

				shoppingcart.saveCookie();
     	  LoadCartSummary();
     }
     
     function OnLoadCartHandler()
     {
     	  function update() {
     	  	shoppingcart.saveCookie();
     	  	LoadCartSummary();
     	  	
	     	  if(checkoutcart)
	     	  {
	     	  	var path = baseurl + '/cart/getcartupdate.php';
	     	  	var rand = Math.random()*1000000;
	     	  	$.getJSON(path,{id:rand},
							function updatecart(data) {
							
							$('#totaalprijscart').html('<h3>'+data['totaal']+'</h3>');
							
							$(".number_textfield").each(function(){
									var product = $(this).attr("id");
								  var prod_attr = product.split("_");
								  $('#subtotaal_'+ prod_attr[1] +'_'+prod_attr[2]).html(data[prod_attr[1] +'_'+prod_attr[2]]);
							});
								
						});
	     	  }
     	  }
     	  
     	  function remove(prodid,attrid) {
     	  	shoppingcart.deleteProduct(prodid,attrid,'');
     	  	$('#overlayproduct_' + prodid + '_' + attrid).remove();
     	  }
     	  
     		//make removelink hover
     		$('.removelink').hover(function() {
					$(this).css('cursor','pointer');
					}, function() {

					$(this).css('cursor','auto');
				});
				
     		//assign event handlers
     		//remove event
     		$('.removelink').click(function() {
					 var product = $(this).attr("id");
					 var prod_attr = product.split("_");
					 remove(prod_attr[1],prod_attr[2]);
					 update();
				});
				
				$('.number_textfield').blur(function() {
					 var product = $(this).attr("id");
					 var prod_attr = product.split("_");
					 var textfield_value = parseInt($(this).val());
					 
					 if(textfield_value > 0)
					 	shoppingcart.setProduct(prod_attr[1],prod_attr[2],$(this).val(),$('#price_'+prod_attr[1]+'_'+prod_attr[2]).val(),'');
					 else if(textfield_value <= 0)
					 	remove(prod_attr[1],prod_attr[2]);

					 update();
				});
     }
     
     
     $(document).ready(function () { 
 				
 				//load cart handler when in checkout
 				if(checkoutcart)
 					OnLoadCartHandler();
 				
 				$('.curlink').click(function(){
 					var curid = $(this).attr('id');
 					shoppingcart.currency = parseInt(curid.replace('cur',''));
 					shoppingcart.saveCookie();
 				});
 				
 				$('#registerform').submit(function() {

 								if($("input[name='passwordenc']").size())
 								{
		 								$("input[name='passwordenc']").val(hex_md5($("input[name='password']").val()));
		 								$("input[name='password']").val("");
		 								$("input[name='passwordconfenc']").val(hex_md5($("input[name='passwordconf']").val()));
		 								$("input[name='passwordconf']").val("");
 								}
 							  return true;
				});
 				
 				$('.nextbut').click(function(){
 					
 						//do payment form if it exists
 						if($('input[name=betaalwijze]').size())
 						{
 								var betaalwijze = $('input[name=betaalwijze]:checked').fieldValue();

								
 								if(betaalwijze == '')
 								{
 										$('input[name=betaalwijze]').attr('class','errorform');
 										return false;
 								}
 								
 								if(typeof betaalwijze == 'object')
 									betaalwijze = betaalwijze[0];
 								
 							  shoppingcart.paymentmethod = betaalwijze;
 							  
 							  if(betaalwijze == 'ideal')
 							  {
 							  	 var issuerid = $('#issuerid').val();
 							  	 if(issuerid == '')
 							  	 {
 							  	 	 $('#issuerid').attr('class','errorform');
 							  	 	 return false;
 							  	 }
 							  	 shoppingcart.idealbankcode = issuerid;
 							  	 shoppingcart.idealbankname = $('#issuerid option:selected').html();
 							  	 
 							  }	
 							  else
 							  {
 							  	shoppingcart.idealbankcode = '';
 							  	shoppingcart.idealbankname = '';
								}
								
								if(betaalwijze == 'rembours')
 							  {
 							  	 shoppingcart.shippingmethod = 'rembours';
 							  }	
 							  //alert(shoppingcart.paymentmethod);
 							  
 							  
 							  var val = $('input[name=couponcode]').val();
 								if(!empty(val))
 								{
 								
 									var gourl = baseurl + '/cart/validatecoupon.php?couponcode=' + val + '&' + uniqid();
 									var result = false;
     							$.ajax({
									   type: "POST",
									   url: gourl,
									   data: {},
									   async: false,
									   timeout: 20000,
									   success: function(msg){
							     							if(empty(msg))
																{
																	result = false;
																}
																else	
																{
																	
															 		result = true;
															 	}
							   		 },
							   		 error:  function(msg){
													result = false;
							   		 }
							 		});
							 		
							 		if(!result)
							 		{
							 			$('input[name=couponcode]').addClass('errorform');
							 			shoppingcart.couponcode = '';
							 			shoppingcart.saveCookie();
							 			return false;
							 		}
							 		else
							 		{
							 				$('input[name=couponcode]').removeClass('errorform');
											shoppingcart.couponcode = $('input[name=couponcode]').val();
											shoppingcart.saveCookie();
											return true;
							 		}
								
								}
								shoppingcart.couponcode = '';	
								shoppingcart.saveCookie();
 						}
 						
 						//update shipping info if exists
 						if($('p#shippinginfo').size())
 						{
 							  shoppingcart.shippinginitials = $("input[name='initials']").val();
 								shoppingcart.shippingpreposition = $("input[name='preposition']").val();
 								shoppingcart.shippingname = $("input[name='name']").val();
 								shoppingcart.shippingstreet = $("input[name='street']").val();
 								shoppingcart.shippinghouse = $("input[name='house']").val();
 								shoppingcart.shippingzipcode = $("input[name='zipcode']").val();
 								shoppingcart.shippingcity = $("input[name='city']").val();
 								shoppingcart.shippingcountry = $("input[name='country']").val();
 								shoppingcart.saveCookie();
 						}
 							
 						$('#checkoutform').submit(function() {
 							
 								if($("input[name='passwordenc']").size())
 								{
		 								$("input[name='passwordenc']").val(hex_md5($("input[name='password']").val()));
		 								$("input[name='password']").val("");
		 								$("input[name='passwordconfenc']").val(hex_md5($("input[name='passwordconf']").val()));
		 								$("input[name='passwordconf']").val("");
 								}
 								
 								if($(this).attr('name') == 'changeshipping')
 								{
 									  shoppingcart.shippinginitials = $("input[name='initials']").val();
 										shoppingcart.shippingpreposition = $("input[name='preposition']").val();
 										shoppingcart.shippingname = $("input[name='name']").val();
 										shoppingcart.shippingstreet = $("input[name='street']").val();
 										shoppingcart.shippinghouse = $("input[name='house']").val();
 										shoppingcart.shippingzipcode = $("input[name='zipcode']").val();
 										shoppingcart.shippingcity = $("input[name='city']").val();
 										shoppingcart.shippingcountry = $("select[name='country']").val();
 										shoppingcart.saveCookie();
 								}
 									
							  return true;
						});
 						$('#checkoutform').submit();
		
 						if($('#checkoutform').size())
 							return false;
 							
 					}
 				);
 				
 				var interval = 0;
 				
 				//preload the images for top
				$(".aanbieding_item > a:even").each(function() {
					
					$("<img>").attr("src", $(this).attr("href"));
				});
				
				function ChangeTop(elt)
 				{
 					 $(".aanbieding_item").attr("id","");
 					 $(elt).attr("id","aanbieding_item_a");
 					 var img = $($("#aanbieding_item_a > a")[0]).attr("href");
 					 var a = $($("#aanbieding_item_a > a")[1]).attr("href"); 
					 $("#headerimage").attr("src",img);
					 $("#headerlink").attr("href",a);
 				}
 				
 				function StartCycle()
 				{
 					 //set cycle
 					if(!interval)
 					{
						interval = setInterval(function() {
								//get index to show
								var n = ($('.aanbieding_item').index($('#aanbieding_item_a')[0]) + 1) % $('.aanbieding_item').size(); 
								ChangeTop($(".aanbieding_item").get(n));
							},4000);
					}
 				}
 				
 				function StopCycle()
 				{
 						if(interval)
 						{
 							clearInterval(interval);
 							interval = 0;
 						}
 				}
 				
 				
 				//set up sort filter event
				$("#sort").change(function() {
						var gotoURL = $("#sort").val();
						location.href = gotoURL;
				}); 
				
				//set up outlet filter event
				$("#outletonly").click(function() {
						var gotoURL = $("#outletonly").val();
						location.href = gotoURL;
				}); 
				
				//make cycle stop on mouseover aanbieding_item 
				//and change the image
				$(".aanbieding_item").mouseover(function() {
					  //stop animation
					  StopCycle();
						ChangeTop(this);
				});
				
				//make cycle run again on mouseout aanbieding_item
				$(".aanbieding_item").mouseout(function() {
					  //stop animation
					  StartCycle();
				});
				
				
				//start cycling only if on homepage
				if($(".aanbieding_item").length > 0)
					StartCycle();
				
				//$(document).overlay();
				$("#login_forgotpw").click(function() {
					  //show forgotpwform in overlay
					  $("body").overlay({contenturl:  baseurl + '/overlay/'+language.toLowerCase()+'/forgotpw/'});
				});	
				
				$("#logoutlink").click(function() {
					  //submit logout form
					  $("#logoutform").submit();
				});	
				
				$('#shippingmethod').change(function(){
						shoppingcart.shippingmethod = $(this).val();
						
						if(shoppingcart.shippingmethod == 'rembours')
							shoppingcart.paymentmethod = 'rembours';
						else if(shoppingcart.paymentmethod == 'rembours')
							shoppingcart.paymentmethod = 'ideal';
							
						shoppingcart.saveCookie();
						
				});
				
			
				
				//handle shopping cart
				shoppingcart.loadCookie();
				
				$('#addcart_submit').hover(function() {
					$(this).css('cursor','pointer');
					}, function() {

					$(this).css('cursor','auto');
				});
				
				$('#addcart_submit').click(function() {
					  var add = [];
						$('.addcart_option').each(function(){
								var value = $(this).val();
								
								//make sure it's defined and a number > 0
								if(value > 0)
								{
									var product = $(this).attr("id");
									var prod_attr = product.split("_");
									var price = $('#price_'+prod_attr[1]+'_'+prod_attr[2]).val();
									shoppingcart.addProduct(prod_attr[1],prod_attr[2],value,price,'','');
									add.push({'pid':prod_attr[1],'aid':prod_attr[2],'n':value});
							  }
        		});
        		
        		shoppingcart.saveCookie();
						LoadCartOverlay("add=" + $.toJSON(add));
									
						LoadCartSummary();
				});

				//set up sort filter event
				$("#itemsperpage").change(function() {
					var now = new Date();
						   var cookieoptions = {
							 domain: cookiedomain,
							 path: "/",
						   expiresAt: new Date(now.getFullYear() + 1,now.getMonth(),now.getDate())
				  	}
				  	
				  	$.cookies.set('itemsperpage',$("#itemsperpage").val(),cookieoptions);						
						location.reload();
				});
		}); 
