﻿/**
*	object Cart 
*@package depending on prototype AND scriptaculous (> v1.5)
*
**/

if (!CART) { var CART = new Object(); }

CART = {
	
	slideIn 		: function(element) {
		new Effect.MoveBy( element.parentNode, 185, 0 );
		element.onclick = function(event) { 
			CART.menu.slideOut(element); 
		}
		
		return false;
	},
	slideOut 		: function(element) {		
		new Effect.MoveBy( element.parentNode, -185, 0 );
		element.onclick = function(event) { 
			CART.menu.slideIn(element); 
		}
		return false;
	},	
	// add product to cart	
	add				: function(p_nId) {
			//console.info("Id given to javascript "+ p_nId);
			if (p_nId >= 1) {
				new Ajax.Request(window.location,
						{
							method		: "post",
							postBody	: "action=AJAX_add2cart&nProd_id=" + p_nId,
							onSuccess	: function(t) {
								CART.doneAdd(t.responseText);
							},
							onFailure	: function(t) {
								alert("Error:\n"+t.status+" - "+t.statusText);
							}
						}
				);
			}
			
	},
	// succes on the add to cart
	doneAdd			: function(responseText) {
			
			var json	= responseText.parseJSON();
			if (json.cart.code == "success") {
				
				if($('container_cart')) {
					
					new Effect.ScrollTo("container_cart");
					
					if ($('no_cart_items')) Element.remove('no_cart_items');
					if ($('old_cart_items')) Element.remove('old_cart_items');				
					// build the added row
					/*
					var aRow = "<li id=\"old_cart_items\">You have <strong>"+ json.cart.num +" products</strong> in your cart for the amount of <strong>EUR "+json.cart.amount+"</strong></li>";
					*/
					var aRow = "<li id=\"old_cart_items\">u hebt <strong>"+ json.cart.num +" producten</strong> in uw winkelmandje ter waarde van  <strong>EUR "+json.cart.amount+"</strong></li>";
					new Insertion.Bottom($('quick_cart'), aRow); 
					
					new Effect.Highlight('container_cart', { 
							startcolor:'#576B00', 
							endcolor:'#DBD6BC', 
							duration:'4', 
							afterFinish: function() {  } 
					});	
							
				}
				
			} else {
				alert("not added :\n" + json.status.text);
			}
		
	},
	
	empty			: function() {
			
			if($('container_cart')) {
					
					
					if ($('no_cart_items')) Element.remove('no_cart_items');
					if ($('old_cart_items')) Element.remove('old_cart_items');				
					// build the added row
					/*					
					var aRow = "<li id=\"old_cart_items\">You're cart is empty.</li>";
					*/
					var aRow = "<li id=\"old_cart_items\">Uw winkelmandje is leeg</li>";
					new Insertion.Bottom($('quick_cart'), aRow); 
					
					new Effect.Highlight('container_cart', { 
							startcolor:'#576B00', 
							endcolor:'#DBD6BC', 
							duration:'4', 
							afterFinish: function() {  } 
					});	
							
				}
		
	},
	
	deleteSelected		: function(p_sCart) {
		  
		document.getElementById(p_sCart).cart_action.value = "delete";
		document.getElementById(p_sCart).submit();
		
	},
	updateOrder		: function(p_nId) {
		
		
		document.getElementById("cart_action").value = "update";
		document.getElementById("nSelec_"+p_nId).checked = true;
		document.getElementById("nNumber_"+p_nId).value = document.getElementById("nNumber_"+p_nId).value;
		document.getElementById("frmOrder").submit();
		
	}// end of last method
	
}




