// Extend jQuery with 'Cart'-functions {{{
(function($) {		

	// cartAddProduct {{{ 
	$.extend({
    /**
		 *	add a product to the cart with the selected settings.
		 *	@pre:								button has to be located in the form with the product properties
		 *	@in: 		target			the button that was clicked. Is located within a form, which will be serialized
		 *	@out:		
		 *	@post:							product will be added to cart, or number in cart will be increased if it already exists
		 */
		cartAddProduct: function (target) {
			var o_form_parent = $(target).parents("form").get(0);
			var s_values = $(o_form_parent).serialize();
			var i_id = $('input[name=id]').val();
			
			// post call {{{
			var i_view_id = 201;			// add product to basket
			$.post("/php/basket.php", { prd: i_id, view: i_view_id, values: s_values },
				function (data) {
					if (data.m_response) {
						$.cartLoad();
						/* old code (obsolete) {{{
						var s_name 		= data.m_response;
						var s_option 	= data.m_response_option;
						var s_desc 		= data.m_response_desc;
						$('#basket_no_products').hide();
						$('#basket_btn_goto').show();
						var s_html = '<table class="basket_pitem"><tr><th><input type="hidden" name="' + s_name + '" id="' + i_id + '" value="' + s_values + '" />' + s_name + '</th><td>&nbsp;<a href="#" onclick="jQuery.cartRemoveProduct(this);">X</a>&nbsp;</td></tr>';
						if (s_option) s_html += '<tr><th colspan="2" class="spread">' + s_option + '</th></tr>';
						if (s_desc) s_html += '<tr><th colspan="2">' + s_desc + '</th></tr>';
						s_html += '</table>'; 
						$('#basket_products').append(s_html);
						$('#basket_container').css({backgroundColor: '#f00'}).animate({backgroundColor: '#fff'}, 1000);
						}}} */

						// google analytics push {{{
						try {
							//Syntax: _gaq.push ([‘_trackEvent’,’event category’,’event action’,’event label’, event value]);
							/*
							 * Event Category => It is the name given to group of similar events which you want to track. For e.g. ‘videos’
							 * Event Action => It is the name given to the type of event you want to track for a particular webpage element. For e.g. ‘play’, ‘stop’, ‘pause’, ‘share’ etc.
							 * Event Label => It is used to provide additional information about the webpage element you want to track like: title of a video, name of a gadget, name of the downloadable file etc.
							 * Event Value => It is the numerical value assigned to the event you want to track. For e.g. download time, length of the video footage played or some dollar value.
							 *
							 * Note: Specifying ‘event category’ and ‘event action’ in _trackEvent() method is mandatory. Whereas specifying ‘event label’ and ‘event value’ in _trackEvent() method is optional.
							 *
							 * Read more: http://seohimanshu.com/2010/09/15/event-tracking-guide-google-analytics-simplified-version/#ixzz1HQyU3t8g
							 */
							var s_name = data.m_response;
							_gaq.push(['_trackEvent' , 'Webshop', 'productToevoegen', s_name]);
						}   
						catch(err) {
							// failure
						}   
						// }}} */

					}
					else {
						//alert(data.s_errors);		
					}
				}, "json");
			// }}} 
		}
	});
	// }}}
	
	// cartRemoveProduct {{{ 
	$.extend({
    /**
		 *	removes a product from the cart
		 *	@pre:								button has to be located in the form with the product id
		 *	@in: 		target			the button that was clicked. Is located within a form, which will be serialized
		 *	@out:		
		 *	@post:							product will be removed from the cart once (new count=count-1)
		 */
		cartRemoveProduct: function (i_prd_id, s_code, target) {
			var o_table = $(target).parents('.basket_pitem').get(0);
			
			// todo: do post, upon success remove item from basket (once on each click)
			$.cartDecreaseNumber(i_prd_id, s_code);		// XXX: remove

			// analytics info is in cartDecreaseNumber, so don't add code here
			
			// check if there's anything left in basket
			var a_basket_items = $('.basket_pitem');
			if (a_basket_items.length == 0) {
				$('#basket_no_products').show();
				$('#basket_btn_goto').hide();
			}
		}
	});
	// }}}
	
	// cartClear {{{ 
	$.extend({
    /**
		 *	removes all products from the cart
		 *	@pre:								
		 *	@in: 		
		 *	@out:		
		 *	@post:							cart will be emptied completely
		 */
		cartClear: function () {
			// todo: 
		}
	});
	// }}}
	
	// cartIncreaseNumber {{{ 
	$.extend({
    /**
		 *	increases the number in order of a certain product by 1
		 *	@pre:								
		 *	@in: 		
		 *	@out:		
		 *	@post:							product count++
		 */
		cartIncreaseNumber: function () {
			// todo: 
		}
	});
	// }}}
	
	// cartDecreaseNumber {{{ 
	$.extend({
    /**
		 *	decreases the number in order of a certain product by 1
		 *	@pre:								
		 *	@in: 		
		 *	@out:		
		 *	@post:							product count--
		 */
		cartDecreaseNumber: function (i_id, s_code) {
			// todo: 
			var i_view_id = 203; 
			$.post("/php/basket.php", { prd: i_id, view: i_view_id, code: s_code },
				function (data) {
					if (data.m_response) {
						$.cartLoad();
						$('#basket_container').css({backgroundColor: '#f00'}).animate({backgroundColor: '#fff'}, 1000);

						// google analytics push {{{
						try {
							var s_name = data.m_response;
							//s_name = s_name + ' (' + s_code + ')';
							_gaq.push(['_trackEvent' , 'Webshop', 'productVerwijderen', s_name]);
						}   
						catch(err) {
							// failure
						}   
						// }}} */
					}
					else {
						//alert(data.s_errors);			// XXX;
					}
				}, "json");
			return false;
		}
	});
	// }}}
	
	// cartUpdatePriceTotal {{{ 
	$.extend({
    /**
		 *	refreshes the total cost of all cart-contents
		 *	@pre:								a product is added/removed in any way
		 *	@in: 								
		 *	@out:		
		 *	@post:							price total will be refreshed
		 */
		cartUpdatePriceTotal: function () {
			// todo: 
		}
	});
	// }}}
	
	// cartLoad {{{ 
	$.extend({
    /**
		 *	loads the cart
		 *	@pre:								the page is loaded
		 *	@in: 								
		 *	@out:		
		 *	@post:							cart will be filled with the products from basket (or not)
		 */
		cartLoad: function () {
			var i_view_id = 1; 
			$.post("/php/basket.php", { view: i_view_id, tpl_view: 'summarized' },
				function (data) {
					if (data.m_response) {
						var s_tpl = data.m_response;
						if (s_tpl.length > 0) {
							$('#basket_no_products').hide();
							$('#basket_products').replaceWith(s_tpl);
							$('#basket_btn_goto').show();
						}
						else {
							$('#basket_no_products').show();
							$('#basket_products').replaceWith('<div id="basket_products"></div>');
							$('#basket_btn_goto').hide();
						}

						// red glow onLoad:
						//$('#basket_container').css({backgroundColor: '#f00'}).animate({backgroundColor: '#fff'}, 1000);
					}
					else {
						//alert(data.s_errors);		
					}
				}, "json");
		}
	});
	// }}}

})(jQuery);	// }}}

// 'onload'-stuff {{{
jQuery(document).ready(function() {

	// todo: create redraw function ? 

	// todo: from product detail view, just a serialized submit will do. Afterwards, the redraw function should be called to show the corrected basket. (or not use a redraw, but just correct the products)
	$("input.basketadd").click(function (e) {		// XXX: must be linked to specific buttons only
		$.cartAddProduct($(e.target));
		return false;		// bypass submit button
	});	

	$("div#basket_btn_goto a").click(function (e) {
		// google analytics push {{{
		try {
			_gaq.push(['_trackEvent' , 'Webshop', 'naarAfrekenen']);
		}   
		catch(err) {
			// failure
		}   
		// }}} */
	});

	$.cartLoad();
	
	// todo: to delete an item from the basket, first try to make the POST-call. If that succeeds, THEN delete it from basket. 
});
// }}}

