	wps._require([gRootPath + 'lib/objectShop/idealABN.js']);
	wps._require([gRootPath + 'lib/objectShop/paypal.js']);
	wps._require([gRootPath + 'lib/objectShop/mollie.js']);

	wps.objectShop = function(basketContainer, parentObj){
		this.instID = 'wps.objectShop' + parseInt(Math.random()*1000000);
		this.basketContainer = basketContainer;
		this.basketText = "You have [number] item(s) in your cart ";
		this.cartCount = 0;
		this.pob = parentObj;
		this.loadingBasket = false;
	}
	
	wps.objectShop = wps.objectShop.extendsFrom(wps.base);
	
	wps.objectShop.prototype.addToCart = function(objectID, propertyID, priceObjectID){
		var wpsRPC = new wps.rpc;
		wpsRPC.debug = true;
		wpsRPC.attachWaiter(this.waiter, this);
		wpsRPC.attachUnWaiter(this.unWaiter, this);
		wpsRPC.createCall('objectShop', this.updateCart.bind(this), this);
		wpsRPC.call('addToCart', 'object_id=' + objectID, 'prop_id=' + propertyID, 'priceObjectID=' + priceObjectID, 'amount=1');
	}


	wps.objectShop.prototype.updateCartLine = function(objectID, propertyID, priceObjectID, amount, func, context){
		var wpsRPC = new wps.rpc;
		wpsRPC.debug = true;
		wpsRPC.attachWaiter(this.waiter, this);
		wpsRPC.attachUnWaiter(this.unWaiter, this);
		wpsRPC.createCall('objectShop', func.bind(context), context);
		wpsRPC.call('updateCartLine', 'object_id=' + objectID, 'prop_id=' + propertyID, 'priceObjectID=' + priceObjectID, 'amount='+amount);
	}
	
	wps.objectShop.prototype.doNothing = function(req){
		var dom = req.responseXML;
		var cartCount = wps.dom.getElementValue(dom.getElementsByTagName('message').item(0));
		this.cartCount = cartCount;
	}
	
	wps.objectShop.prototype.updateCart = function(req){
		var dom = req.responseXML;
		var cartCount = wps.dom.getElementValue(dom.getElementsByTagName('message').item(0));
		if (cartCount == 'false'){
			alert('Something went wrong while storing your cart, please retry or contact the webmaster.');
			return;
		}
		this.cartCount = cartCount;
		this.setBasketText();
	}
	
	wps.objectShop.prototype.setBasketText = function(){
		if (this.cartCount > 0){
			// update cart-text
			
			if (this.basketContainer.style.display = 'none') new Effect.Appear(this.basketContainer);
			
			var indexOfCartCount = this.basketText.indexOf('[number]');
			var cartTextBefore = this.basketText.substr(0, indexOfCartCount);
			var cartTextAfter = this.basketText.substr(indexOfCartCount + 8, this.basketText.length);
			
			this.basketContainer.update('');
			this.basketContainer.appendChild(Builder.node('span', cartTextBefore));
			this.basketContainer.appendChild(Builder.node('span', {id:'cartCount'}, this.cartCount));
			this.basketContainer.appendChild(Builder.node('span', cartTextAfter));
						
			new Effect.Pulsate($('cartCount'));
			var clear = Builder.node('span', {style:'cursor:pointer;text-decoration:underline;'}, ['[view your cart]']); 
			this.basketContainer.appendChild(clear);
			
			this.addEvent(clear, 'click', this.showBasket.bind(this));
		}else{
			new Effect.PhaseOut(this.basketContainer);
			this.basketContainer.update('');
		}
		this.onUpdate(this.pob); // run custom functions (if any) from parent-objects
	}
	
	wps.objectShop.prototype.clearBasket = function(){
		//deprecated?
		var wpsRPC = new wps.rpc;
		wpsRPC.debug = true;
		wpsRPC.attachWaiter(this.waiter, this);
		wpsRPC.attachUnWaiter(this.unWaiter, this);
		wpsRPC.createCall('objectShop', this.updateCart.bind(this), this);
		wpsRPC.call('clearCart');
	}
	
	wps.objectShop.prototype.getBasket = function(func, context){
		var wpsRPC = new wps.rpc;
		wpsRPC.debug = true;
		wpsRPC.attachWaiter(this.waiter, this);
		wpsRPC.attachUnWaiter(this.unWaiter, this);
		wpsRPC.createCall('objectShop', func.bind(context), context);
		wpsRPC.call('renderCart');
	}

	wps.objectShop.prototype.sortBasket = function(req){
		this.waiter();
		var DOM = req.responseXML;
		var hash = $H({});
		// make object/hash from DOMnodes
		$A(DOM.getElementsByTagName('row')).each(
			(function(node){
				var nodeObjectID = wps.dom.getFieldValueFromRowNode(node, 'object_id');
				var priceObjectID = wps.dom.getFieldValueFromRowNode(node, 'priceObjectID');
				if (hash.get(priceObjectID)) {
					var obj = hash.get(priceObjectID); 
					obj.amount = obj.amount + 1;
				}else{
					var obj = {object_id:nodeObjectID, priceObjectID:priceObjectID, price:wps.dom.getFieldValueFromRowNode(node, 'price'), mediaName:wps.dom.getFieldValueFromRowNode(node, 'mediaName'), objectName:wps.dom.getFieldValueFromRowNode(node, 'ObjectName'), amount:1};
					hash.set(priceObjectID, obj);
				}
			}).bind(this)
		)		
		/*$A(DOM.getElementsByTagName('row')).each(
			(function(node){
				var nodeObjectID = wps.dom.getFieldValueFromRowNode(node, 'object_id');
				if (hash.get(nodeObjectID)) {
					var obj = hash.get(nodeObjectID); 
					obj.amount = obj.amount + 1;
				}else{
					var obj = {object_id:nodeObjectID, price:wps.dom.getFieldValueFromRowNode(node, 'price'), objectName:wps.dom.getFieldValueFromRowNode(node, 'ObjectName'), amount:1, pKey:wps.dom.getFieldValueFromRowNode(node, 'pKey')};
					hash.set(nodeObjectID, obj);
				}
			}).bind(this)
		)*/
		return hash; 
	}
		
	wps.objectShop.prototype.showBasket = function(){
		if ($('showBasket') || this.loadingBasket ) return;
		this.loadingBasket = true;
		var wpsRPC = new wps.rpc;
		wpsRPC.debug = true;
		wpsRPC.attachWaiter(this.waiter, this);
		wpsRPC.attachUnWaiter(this.unWaiter, this);
		wpsRPC.createCall('objectShop', this.renderBasket.bind(this), this);
		wpsRPC.call('renderCart');
	}
	
	wps.objectShop.prototype.renderBasket = function(req){
		this.waiter();
		var DOM = req.responseXML;
		var hash = this.sortBasket(req);
		// make object/hash from DOMnodes
		
		var slidingDiv = Builder.node('div', {id:'showBasket', className:'wpsObjectShopBasket', style:'position:absolute;display:none;'});
		new Draggable(slidingDiv);
		this.basketContainer.appendChild(slidingDiv);
		var list = Builder.node('ul');
		slidingDiv.appendChild(list);
		hash.each(
			(function(hashElem){
				var obj = hashElem.value;
				var objectID = obj.object_id;
				var text = obj.amount + ' x ' + obj.objectName + ' (' + obj.mediaName + ') : E ' +  (obj.amount * obj.price) + ' ';
				var link = Builder.node('span', {className:'removeLink'}, ['[remove]']);
				link.objectID = objectID;
				link.priceObjectID = obj.priceObjectID;
				link.amount = obj.amount;
				this.addEvent(link, 'click', this.removeBasketLine.bind(this));
				var li = Builder.node('li', {className:'row', id: objectID+ "_basketLine"}, [text, link]);
				list.appendChild(li);
			}).bind(this)
		)
		var closer = Builder.node('span', {className:'closer'}, ['[Close window]']);
		this.addEvent(closer, 'click', this.closeBasket.bind(this));
		slidingDiv.appendChild(closer);
		new Effect.PhaseIn(slidingDiv);
		this.loadingBasket = false;
		this.unWaiter();
	}
	
	wps.objectShop.prototype.closeBasket = function(){
		var func = (function(){this.basketContainer.removeChild($('showBasket'))}).bind(this)
		new Effect.PhaseOut($('showBasket'));
		setTimeout(func, 2000);
	}
	
	wps.objectShop.prototype.removeBasketLine = function(event, elem, doCartUpdate){
		if (!doCartUpdate) doCartUpdate =true;
		if (!elem){
			var elem = Event.element(event);
		}
		var objectID = elem.objectID;
		var priceObjectID = elem.priceObjectID;
		var amount = elem.amount;
		if (doCartUpdate){
			var func = (function(req){this.removeLine(req, objectID, amount)}).bind(this)
		}else{
			var func = (function(req){}).bind(this)
		}
		var wpsRPC = new wps.rpc;
		wpsRPC.debug = true;
		wpsRPC.attachWaiter(this.waiter, this);
		wpsRPC.attachUnWaiter(this.unWaiter, this);
		wpsRPC.createCall('objectShop', func, this);
		wpsRPC.call('removeLine', 'objectID=' + objectID, 'priceObjectID=' + priceObjectID);
		//
	}
	
	wps.objectShop.prototype.removeLine = function(req, objectID, amount){
		wps.dom.filteredFirstNode($('showBasket')).removeChild($(objectID+ "_basketLine"));
		this.cartCount = this.cartCount - amount;
		if (this.cartCount == 0){
			this.setBasketText();
		}else{
			$('cartCount').update(this.cartCount);
		}
	}
	
	wps.objectShop.prototype.onUpdate = function(){
			// do nothing, should be overridden bij parent-objects
	}
	
	wps.objectShop.prototype.storeOrder = function(event){
		var elem = Event.element(event);
		var paymentMethod = elem.id;
		var func = (function(req){this.startPaymentMethod(req, paymentMethod)}).bind(this)
		var values = [];
		this.orderVariables.each(function(aVar){values.push(aVar.key + "=" + aVar.value + '');})
		var wpsRPC = new wps.rpc;
		wpsRPC.debug = true;
		wpsRPC.attachWaiter(this.waiter, this);
		wpsRPC.attachUnWaiter(this.unWaiter, this);
		wpsRPC.createCall('objectShop', func, this);
		wpsRPC.call('storeOrder', values );
	}
	
	wps.objectShop.prototype.setOrderVariables = function(hash){
		this.orderVariables = hash;
	}
	
	wps.objectShop.prototype.startPaymentMethod = function(req, method){
		var message = wps.dom.getElementValue(req.responseXML.getElementsByTagName('message').item(0));
		if ( (message =='false') || (!method) ) {
			alert("Something went wrong storing your order, please contact technical support at webmaster@i-v-o.nl or +31725209141");
			return;
		}else{
			this.orderID = wps.dom.getElementValue(req.responseXML.getElementsByTagName('orderID').item(0));
			this.amount = wps.dom.getElementValue(req.responseXML.getElementsByTagName('amount').item(0));
			
			var oObj = new wps.objectShop[method](this);
			oObj.init();
		}
	}