// 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();
			var d_start_date 	= jQuery('#d_start_date').val();
			var d_end_date 		= jQuery('#d_end_date').val();
			var i_amount			= jQuery('#i_cbo_amount').val();
			
			// post call {{{
			var i_view_id = 201;			// add product to basket
			jQuery.post("/php/basket.php", { 
					prd: i_id, 
					view: i_view_id, 
					d_start_date: d_start_date,
					d_end_date: d_end_date,
					i_amount: i_amount,
					values: s_values
				},
				function (data) {
					if (data.m_response) {
						jQuery.cartLoad(true);
						jQuery("#added-message").dialog({
							modal: true,
							width: 450,
							resizable: false,
							buttons: {
								Ok: function() {
									jQuery(this).dialog( "close" );
								}
							}
						});
						/*if(i_amount > 1) {
							jQuery("#basket_button").hide();
							jQuery("#aleady_in_basket").show();
						}*/
					}
					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, d_start_date, i_bsk_id, 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, d_start_date, i_bsk_id);		// 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, d_start_date, i_bsk_id) {
			// todo: 
			var i_view_id = 203; 
			jQuery.post("/php/basket.php", { prd: i_id, view: i_view_id, d_start_date: d_start_date, i_bsk_id: i_bsk_id },
				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 (!data.m_response.b_empty_basket) {
							jQuery('#basket_no_products').hide();
							jQuery('#basket_container').replaceWith(s_tpl);
							jQuery('#side_basket').show();
							jQuery('#side_product_block').hide();							
						}
						else {
							jQuery('#basket_no_products').show();
							jQuery('#basket').replaceWith('<div id="basket_products"></div>');
							jQuery('#basket_btn_goto').hide();
							jQuery('#side_basket').hide();
							jQuery('#side_product_block').show();		
						}

						// 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
					}
					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#btn_add_to_basket").click(function (e) {		// XXX: must be linked to specific buttons only
		e.preventDefault();
		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. 
});
// }}}

