﻿if (!CART) { var CART = new Object(); }

CART = {
	// add product to cart	
	add				: function(p_nId, type, name) {

			//console.info("Id given to javascript "+ p_nId);
			if (p_nId >= 1) {
				new Ajax.Request(window.location,
						{
							method		: "post",
							postBody	: "action=AJAX_add_cart&nProd_id=" + p_nId + "&type=" + type + "&name=" + name,
							onSuccess	: function(t) {
								CART.doneAdd(t.responseText, name);
							},
							onFailure	: function(t) {
								alert("Error:\n"+t.status+" - "+t.statusText);
							}
						}
				);
			}
			
	},
	
	// succes on the add to cart
	doneAdd			: function(responseText, name) {

			var json	= responseText.parseJSON();

			if (json.cart.code == "success") {

				if($('basketBlock')) {

					var bHighlight = ($('basketBlock').style.display != "none");

					if(!bHighlight) {
						CART.addToCart(json.cart.message, name);
						
						new Effect.Appear('basketBlock');		
					} else {
						CART.addToCart(json.cart.message, name);	
					}
					
					if(bHighlight) 
						new Effect.Highlight($('basketBlock'),{startcolor:'#f3f3f3', endcolor:'#ffff33', duration:'0.8'});					
				}
				
			} else {
				alert("Error while adding item to cart :\n" + json.cart.message);
			}
		
	},
	
	addToCart	: function(type, name) {
		switch(type) {
			case "cases": 
				CART.addCases(name);
			break;
			
			case "brochures":
				CART.addBrochures(name);
			break;
			
			case "referenties":
				CART.addReferentie(name);
			break;			
		}														
	},
	
	addCases		: function(name) {
		var bhasCases = $('cart_casestudies');

		if(!bhasCases) {
			
			var toAdd = '<li id="cart_casestudies">Casestudy\'s<ul><li>'+name+'</li>';
			new Insertion.Bottom($('basket'), toAdd);
		
			
		} else {
			
			var toAdd = '<li>'+name+'</li>';
			new Insertion.Bottom($('cart_casestudies'), toAdd);	
			
		}
	},
	
	addBrochures		: function(name) {
		var bhasBrochures = $('cart_brochures');

		if(!bhasBrochures) {
			
			var toAdd = '<li id="cart_brochures">Brochures<ul><li>'+name+'</li></ul>';
			new Insertion.Bottom($('basket'), toAdd);
			
		} else {
			
			var toAdd = '<li>'+name+'</li>';
			new Insertion.Bottom($('cart_brochures'), toAdd);	
			
		}

	},	
	
	addReferentie		: function(name) {
		var bhasBrochures = $('cart_referenties');

		if(!bhasBrochures) {
			
			var toAdd = '<li id="cart_referenties">Referenties<ul><li>'+name+'</li></ul>';
			new Insertion.Bottom($('basket'), toAdd);
			
		} else {
			
			var toAdd = '<li>'+name+'</li>';
			new Insertion.Bottom($('cart_referenties'), toAdd);	
			
		}

	},		
	
	clearCart		: function() {
		new Ajax.Request(window.location,
			{
				method		: "post",
				postBody	: "action=AJAX_delete_cart",
				onSuccess	: function(t) {
					CART.doneClear(t.responseText);
				},
				onFailure	: function(t) {
					alert("Error:\n"+t.status+" - "+t.statusText);
				}
			}
		);
	},
	
	doneClear		: function(responseText) {
		
			var json	= responseText.parseJSON();

			if (json.cart.code == "success") {	
				new Effect.Fade('basketBlock', {
					afterFinish : function() {
						if($('cart_casestudies'))
							$('cart_casestudies').remove();
							
						if($('cart_brochures'))
						$('cart_brochures').remove();																	 
					}
				});				
				
			} else {
				alert("Error while clearing cart:\n" + json.cart.message);	
			}		
	},
	
	sendContact		: function(form) {
		if(formChecker.checkForm(form)) {
			CART.doSendContact(form);
		} else {
			return false;	
		}
	},
	
	doSendContact	: function(form) {
		var sPostBody = Form.serialize(form);
		
		new Ajax.Request(window.location,
			{
				method		: "post",
				postBody	: "action=AJAX_send_contact&" + sPostBody,
				onSuccess	: function(t) {
					CART.doneSendContact(t.responseText);
				},
				onFailure	: function(t) {
					alert("Error:\n"+t.status+" - "+t.statusText);
				}
			}
		);
	},
	
	doneSendContact	: function(responseText) {
		var json	= responseText.parseJSON();
		
		if($(json.send.status))
			$(json.send.status).style.display = "block";
		
		if(json.send.status == "success") {
			new Effect.Fade('basketBlock', {
				afterFinish : function() {
					if($('cart_casestudies'))
						$('cart_casestudies').remove();
						
					if($('cart_brochures'))
					$('cart_brochures').remove();																	 
				}
			});			
		}
	}	
}

