/**
* @version 1.0.1
*/
var oCart = new function clsShoppingCart() {

		this.m_sRequestUri 			= "/index.php?mod=webshop_orders&com=doAjaxRequest&";
		this.m_sPlaceHolder 		= 'shoppingcart';
		this.m_sPlaceHolderSmall 	= 'shoppingcart_small';

		this.addSingleProdutToCart = function(p_iProductId, p_iQuantity, p_bRedirect, p_bRefresh) {
			var aProducts = new Array();
			aProducts.push(new Array(p_iProductId, p_iQuantity));
			if (p_bRedirect) {
				this.showPopup();
				this._redirectToCart(aProducts);
			} else {
				if (p_bRefresh != "undefined" || p_bRefresh == null) {
					this._ajaxAddProducts(aProducts, true);
				} else {
					this._ajaxAddProducts(aProducts, false);
				}
			}
			this.updateCart();
			
			this.showPopup();
			
		}
		
		this._redirectToCart = function(p_aProducts) {
			if (p_aProducts.length) {
				document.location = "/?mod=webshop_orders&com=orderProduct&order_code=" + p_aProducts[0][0];
			}
		}
		
		this.showPopup = function () {
			var oDiv = document.createElement('DIV');
			var self = this;
			oDiv.id = "shoppingcart_popup";
			oDiv.className = "shoppingcart_popup";
			
			var oDivShadow = document.createElement('DIV');
			oDivShadow.id = "shoppingcart_shadow";
			oDivShadow.className = "shoppingcart_shadow";
			
			document.body.appendChild(oDivShadow);
			document.body.appendChild(oDiv);
			oDiv.innerHTML = "<div class=\"contentPopup\">Uw product(en) worden toegevoegd...</div>";
			setTimeout(function () { self.killPopup(oDiv.id,oDivShadow.id); }, 1500);
		}
		
		this.killPopup = function (p_sDivId,p_sDivShadowId) {
			var oDiv = document.getElementById(p_sDivId);
			this._DOMRemoveChilds(oDiv);
			var oDivShadow = document.getElementById(p_sDivShadowId);
			this._DOMRemoveChilds(oDivShadow);
		}
		
		this._DOMRemoveChilds = function(p_oObj)  {
			if ( p_oObj.hasChildNodes() )
			{
				while ( p_oObj.childNodes.length >= 1 )
				{
					p_oObj.removeChild( p_oObj.firstChild );       
				} 
			}
			if (p_oObj.parentNode) {
				p_oObj.parentNode.removeChild(p_oObj);
			}
		}
		
		this.updateCart = function() {
			var oHTTP = new clsHTTPRequest();
			var sUrl = this.m_sRequestUri;
			sUrl += "request=updateCart";
			oHTTP.setRequestURI(sUrl);
			oHTTP.setHTML();
			if (document.getElementById(this.m_sPlaceHolder)) {
				oHTTP.getHTML(this.m_sPlaceHolder);
			}		
			if (document.getElementById(this.m_sPlaceHolderSmall)) {	
				sUrl += "&small=1";
				oHTTP.setRequestURI(sUrl);
				oHTTP.getHTML(this.m_sPlaceHolderSmall);
			}
		}
		
		this.removeSingleProduct = function(p_iProductId) {
			var aProducts = new Array();
			aProducts.push(p_iProductId);
			if (p_iProductId) {
				this._ajaxRemoveProducts(aProducts)
			}
			this.updateCart();
		}
		
		this.removeAllProducts = function() {
			this._ajaxClearCart();
			this.updateCart();
		}
		
		this._ajaxClearCart = function() {
			var oHTTP		= new clsHTTPRequest();
			var sUrl 		= this.m_sRequestUri;
			sUrl += "request=removeAllFromCart";
			oHTTP.setRequestURI(sUrl);
			oHTTP.setHTML();
			if (document.getElementById(this.m_sPlaceHolderSmall)) {
				sUrl += "&small=1";
				oHTTP.getHTML(this.m_sPlaceHolderSmall);			
			} else {
				oHTTP.getHTML(this.m_sPlaceHolder);			
			}
		}
		
		this._ajaxRemoveProducts = function(p_aProducts) {
			var oHTTP 		= new clsHTTPRequest();
			var sUrl 		= this.m_sRequestUri;
			var aProductIds = new Array();
			sUrl += "request=removeFromCart";
			if (p_aProducts.length) {
				for (i=0; p_aProducts[i]; i++) {
					aProductIds[i] = p_aProducts[i];
				}
			}
			sUrl += "&prodids="+aProductIds.join(",");
			oHTTP.setRequestURI(sUrl);
			if (document.getElementById(this.m_sPlaceHolderSmall)) {
				sUrl += "&small=1";
				oHTTP.getHTML(this.m_sPlaceHolderSmall);			
			} else {
				oHTTP.getHTML(this.m_sPlaceHolder);			
			}
			
		}
		
		this._ajaxAddProducts = function(p_aProducts, p_bRefresh) {
			var oHTTP = new clsHTTPRequest();
			var sUrl =  this.m_sRequestUri;
			var aProductIds = new Array();
			var aProductQty = new Array();
			sUrl += "request=addToCart&"
			if (p_aProducts.length) {
				for (i=0; p_aProducts[i]; i++) {
					aProductIds[i] = p_aProducts[i][0];
					aProductQty[i] = p_aProducts[i][1];
				}
			}
			sUrl += "prodids="+aProductIds.join(",");
			sUrl += "&qty="+aProductQty.join(",");
			oHTTP.setRequestURI(sUrl);
			if (document.getElementById(this.m_sPlaceHolderSmall)) {
				sUrl += "&small=1";
				oHTTP.setRequestURI(sUrl); 
				oHTTP.getHTML(this.m_sPlaceHolderSmall);			
			} else {
				oHTTP.getHTML(this.m_sPlaceHolder);			
			}
		}
}
	
	function updateCartOnload() {
		if (document.getElementById('shoppingcart') || document.getElementById('shoppingcart_small')) {
			oCart.updateCart();
		}
	}