// 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 (i_id, i_model, i_pc, i_cntr, i_color, i_amount, s_message, s_ref_name) {
			var s_values = '';
			
			// post call {{{
			var i_view_id = 201;			// add product to basket
			$.post("/ajax", { prd: i_id, view: i_view_id, values: s_values, model: i_model, pc: i_pc, cntr: i_cntr, color: i_color, amount: i_amount, ref: s_ref_name },
				function (data) {
					if (data.m_response) {
						$.cartLoad();
						
						// make row green, to indicate it was just added
						//$('.' + i_id).css('background-color', '#5c8832').css('color', '#fff').css('font-style', 'italic');
						$('.' + i_id).css('background-color', '#90d46f').css('font-style', 'italic');

						// change image + attributes
						$('img#img_' + i_id).attr('src', 	 '/images/beheer/add.png');
						$('img#img_' + i_id).attr('title', 'Voeg nogmaals toe aan winkelwagen');			// todo: translate
						$('img#img_' + i_id).attr('alt', 	 'Voeg nogmaals toe aan winkelwagen');			// todo: translate

						$.prompt(s_message, {
										  timeout: 1500,			// waits x milliseconds before it auto-closes
										  callback: '',
											// todo: position. Put in top right corner?
											// todo: styling.
											buttons: {}
										});


            
            _gaq.push(['_trackEvent' , 'BasketAdd', 'EPC', i_id+'|'+i_pc+'|'+i_model]);
					}
					else {
						//alert(data.s_errors);
					}
				}, "json");
			// }}} 
		}
	});
	// }}}

	// cartAddProductWebshop {{{ 
	$.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
		 */
		cartAddProductWebshop: 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();
      var s_code = $('input[name=s_code]').val();
			var i_amount = $('select#i_qty option:selected').val();
			var s_message = $(target).attr('rel');

			// post call {{{
			var i_view_id = 201;			// add product to basket
			$.post("/ajax", { prd: i_id, view: i_view_id, values: s_values, amount: i_amount },
				function (data) {
					if (data.m_response) {
						$.cartLoad();

						$.prompt(s_message, {
										  timeout: 1500,			// waits x milliseconds before it auto-closes
										  callback: '',
											// todo: position. Put in top right corner?
											// todo: styling.
											buttons: {}
										});
            _gaq.push(['_trackEvent', 'BasketAdd', 'Shop', s_code]);
					}
					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

      // 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();
			}
      _gaq.push(['_trackEvent', 'BasketDelete', 'Shop', s_code]);
		}
	});
	// }}}
	
	// 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("/ajax", { 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);
					}
					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('/ajax',{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_loading').hide();
							$('#basket_no_products').hide();
							$('#basket_btn_goto a').show();
							var s_href = $('#basket_btn_goto a').attr('href');
							$('#basket_products').replaceWith('<span id="basket_products"><a href="' + s_href + '">' + s_tpl + ' - &euro; ' + data.m_response_desc + '</a></span>');
							$('#basket_btn_goto').show();
						}
						else {
							$('#basket_loading').hide();
							$('#basket_no_products').show();
							$('#basket_btn_goto a').hide();
							$('#basket_products').replaceWith('<span id="basket_products"></span>');
							$('#basket_btn_goto').hide();
						}
					}
					else {
						//alert(data.s_errors);		
						$('#basket_loading').hide();
						$('#basket_no_products').show();
						$('#basket_btn_goto a').hide();
						$('#basket_products').replaceWith('<span id="basket_products"></span>');
						$('#basket_btn_goto').hide();
					}
				}
      , "json");
		}
	});
	// }}}

})(jQuery);	// }}}

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

	$("input.basketadd").click(function (e) {		// XXX: must be linked to specific buttons only
		$.cartAddProductWebshop($(e.target));
		return false;		// bypass submit button
	});	
});
// }}}
