// Extend jQuery with 'Cart'-functions {{{
(function(jQuery) {		

	// cartAddProduct {{{ 
	jQuery.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 = jQuery(target).parents("form").get(0);
			var s_values = jQuery(o_form_parent).serialize();
			var i_id = jQuery('input[name=id]').val();
			
			// post call {{{
			var i_view_id = 201;			// add product to basket
			jQuery.post("/php/basket.php", { prd: i_id, view: i_view_id, values: s_values },
				function (data) {
					if (data.m_response) {
						jQuery.cartLoad(true);
					}
					else {
						//alert(data.s_errors);		
					}
				}, "json");
			// }}} 
		}
	});
	// }}}
	
	// cartAddProduct2 {{{ 
	jQuery.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
		 */
		cartAddProduct2: function (target) {
			var s_id = jQuery(target).attr('id');
			var i_id = s_id.replace('prd_btn_', '');
			
			// post call {{{
			var i_view_id = 201;			// add product to basket
			jQuery.post("/php/basket.php", {prd: i_id, view: i_view_id},
				function (data) {
					if (data.m_response) {
						jQuery.cartLoad(true);
					}
					else {
						//alert(data.s_errors);		
					}
				}, "json");
			// }}} 
		}
	});
	// }}}
	
	// cartRemoveProduct {{{ 
	jQuery.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 = jQuery(target).parents('.basket_pitem').get(0);
			
			// todo: do post, upon success remove item from basket (once on each click)
			jQuery.cartDecreaseNumber(i_prd_id, s_code);		// XXX: remove
			
			// check if there's anything left in basket
			var a_basket_items = jQuery('.basket_pitem');
			if (a_basket_items.length == 0) {
				jQuery('#basket_no_products').show();
				jQuery('#basket_btn_goto').hide();
			}
		}
	});
	// }}}
	
	// cartClear {{{ 
	jQuery.extend({
    /**
		 *	removes all products from the cart
		 *	@pre:								
		 *	@in: 		
		 *	@out:		
		 *	@post:							cart will be emptied completely
		 */
		cartClear: function () {
			// todo: 
		}
	});
	// }}}
	
	// cartIncreaseNumber {{{ 
	jQuery.extend({
    /**
		 *	increases the number in order of a certain product by 1
		 *	@pre:								
		 *	@in: 		
		 *	@out:		
		 *	@post:							product count++
		 */
		cartIncreaseNumber: function () {
			// todo: 
		}
	});
	// }}}
	
	// cartDecreaseNumber {{{ 
	jQuery.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; 
			jQuery.post("/php/basket.php", { prd: i_id, view: i_view_id, code: s_code },
				function (data) {
					if (data.m_response) {
						jQuery.cartLoad();
						jQuery('#sidebasket').css({backgroundColor: '#f2c8ac'}).animate({backgroundColor: '#B75A1C'}, 1500);
					}
					else {
						//alert(data.s_errors);			// XXX;
					}
				}, "json");
			return false;
		}
	});
	// }}}
	
	// cartUpdatePriceTotal {{{ 
	jQuery.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 {{{ 
	jQuery.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 (b_higlight) {
			var i_view_id = 1; 
			jQuery.post("/php/basket.php", { view: i_view_id, tpl_view: 'summarized' },
				function (data) {
					if (data.m_response) {
						var s_tpl = data.m_response.s_html;
						if (s_tpl.length > 0) {
							jQuery('#basket_no_products').hide();
							jQuery('#basket_container').replaceWith(s_tpl);
							//jQuery('#basket_btn_goto').show();					
						}
						else {
							jQuery('#basket_no_products').show();
							jQuery('#basket').replaceWith('<div id="basket_products"></div>');
							jQuery('#basket_btn_goto').hide();
						}

						// update product count in topnav {{{
						jQuery('#basket_top_menu_items').text(data.m_response.i_items);
						// }}}
						
						// only highlight when we do an action on the basket
						if(b_higlight) {
							jQuery('#sidebar_right_shop').css({backgroundColor: '#B46DA9'}).animate({backgroundColor: '#E8E8E8'}, 1500);
					}
					}
					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)
	jQuery("button.basketadd").click(function (e) {		// XXX: must be linked to specific buttons only
		jQuery.cartAddProduct(jQuery(e.target));
		return false;		// bypass submit button
	});	

	jQuery.cartLoad();
	
	// todo: to delete an item from the basket, first try to make the POST-call. If that succeeds, THEN delete it from basket. 
});
// }}}

