var shoppingcart = {
	baseurl:'',
	language:'NL',
	discount:0.0,
	products:[],
	addproducts:[],
	delproducts:[],
	currency:1,
	shippingmethod:'standaard',
	shippingname:'',
	shippinginitials:'',
	shippingpreposition:'',
	shippingstreet:'',
	shippinghouse:'',
	shippingzipcode:'',
	shippingcity:'',
	shippingcountry:'',
	paymentmethod:'ideal',
	idealbankcode:'',
	idealbankname:'',
	couponcode:'',
	
	addProduct: function(prodid,attrid,nr,price,giftinfo,giftid){
		var i = 0;
		
		
		if(giftinfo.length && giftid.length) //gift should always be set to 1 or 0 for removal
		{
			nr = 1;
		}
			
		var product = {p:prodid,a:attrid,n:parseInt(nr),pr:Math.round(parseFloat(price)*100)/100,giftinfo:giftinfo,giftid:giftid};
		
		
		if(!giftinfo.length || !giftid.length)
		{
			for(i=0; i < this.products.length;i++)
			{
				 if(this.addproducts[i].p == prodid && this.addproducts[i].a == attrid && this.addproducts[i].giftid == giftid)
				 {
				 			if(giftinfo.length && giftid.length)
				 				product.n = 1;
				 			else
				 				product.n += this.addproducts[i].n;
				 			this.addproducts[i] = product;
				 			return;
				 }
			}
		}

		//alert(product.giftid);	
		this.addproducts[this.addproducts.length] = product;
		this.cleanProducts();	
	},
	
	setProduct: function(prodid,attrid,nr,price,giftid){
		var i = 0;
		
		if(giftid.length && nr > 1)
			nr = 1;
			
			var product = {p:prodid,a:attrid,n:parseInt(nr),pr:Math.round(parseFloat(price)*100)/100,giftinfo:'',giftid:giftid};
			for(i=0; i < this.products.length;i++)
			{
				 if(this.products[i].p == prodid && this.products[i].a == attrid && this.products[i].giftid == giftid)
				 {
				 			this.products[i].pr = product.pr;
				 			this.products[i].n = product.n;
				 			return;
				 }
			}
		
		this.products[this.products.length] = product;
		this.cleanProducts();
	},
	
	getProduct: function(prodid,attrid,giftid){
		for(i=0; i < this.products.length;i++)
		{
			 if(this.products[i].p == prodid && this.products[i].a == attrid && this.products[i].giftid == giftid)
			 {
			 			return this.products[i].n;
			 }
		}
		return 0;
	},
	
	alertProduct: function(){
		var show = "";
		for(i=0; i < this.products.length;i++)
		{
			 var tmp = this.products[i]
			 show += "\nproductid: " + tmp.p + " attributeid: " + tmp.a + " price: " + tmp.pr + " number: " + tmp.n+ " giftid: " + tmp.giftid;
		}
		
		alert(show);
	},
	
	deleteProduct: function(prodid,attrid,giftid){
		for(i=0; i < this.products.length;i++)
		{
			 if(this.products[i].p == prodid && this.products[i].a == attrid && this.products[i].giftid == giftid)
			 {
			 			
			 			this.products.splice(i,1);
			 			
			 }
		}
		this.delproducts[this.delproducts.length] = {p:prodid,a:attrid,n:0,pr:0.0,giftid:giftid};
	},
	
	cleanProducts: function(){
		for(i=0; i < this.products.length;i++)
		{
			 if(!this.products[i].n || this.products[i].n < 1)
			 {
			 			this.deleteProduct(this.products[i].p,this.products[i].a);
			 }
		}
	},
	
	emptyProducts: function(){
		this.products = [];
		this.addproducts = [];
		this.delproducts = [];
	},
	
	
	loadSession: function(url){
		
	},
	
	saveSession: function(url){
		
	},
	
	loadCookie: function(){
		
		var cookie_cart = $.cookies.get('cart');
		var tmp = null;
		
		if(typeof cookie_cart == 'object')
			tmp = cookie_cart;
		else if(typeof cookie_cart == 'string')
		{
			tmp	= $.parseJSON(cookie_cart);
		}

		for (var name in tmp) {
			this[name] = tmp[name];
		}
		

	},
	
	
	updateSession: function(){
		//alert('update');
		

		//alert(baseurl + '/cart/cartupdate.php');
		$.ajax({
		   type: "POST",
		   url: baseurl + '/cart/cartupdate.php',
		   data: {cart: $.toJSON(this)},
		   async: false,
		   timeout: 20000,
		   success: function(msg){
     		//alert( 'updatesession: ' + msg );
   		 },
   		 error:  function(msg){
     		alert( 'error updating cart');
   		 }
 		});
 		
	},
	
	saveCookie: function(){
		//create the cookie expiration date
		var now = new Date();
		
		//update session on server
		this.updateSession();
		
		//clear all
		this.emptyProducts();
		
			
				//between 2 and 3 years into the future
		var cookieoptions = {
			 domain: cookiedomain,
			 path: "/",
		   expiresAt: new Date(now.getFullYear() + 1,now.getMonth(),now.getDate())
  	}
  	
  	$.cookies.set('cart',$.toJSON(this),cookieoptions);
	},
	
	showCartInOverlay: function(){
		
	},
	
	refreshCart: function(element){
		
		$(element).empty().append();
	}
	
};
