
	$(document).ready(function(){
			var isVisible = $("body").hasClass("display_flying_cart");
			var initTop  = $('#flying_cart_products_list_wrapper ul').css('top');
			var pas = isVisible ? parseInt($('#flying_cart_products_list_wrapper ul li.product').css('height').replace('px', ''))
					 +	parseInt( $('#flying_cart_products_list_wrapper ul li.product').css('marginBottom').replace('px', '')) : 0;				
			var nbProductsPerLine = 6;
			var nbDisplayedLines = 2;
			var currentLine = 1;
			
			
			var __NORMAL_SIZE__ = 	parseInt($('#flying_cart_products_list_wrapper').css('height'));
			
			function updateFlyingCart(_action,_id,_qte){

				var _retour = $.ajax({
					  type: "GET",
					  data: {action: _action,
					  		 qte: _qte,
					  		 id: _id},
					  url: "/php/ajax/ajax.update_cart.php",
					  async: false
					}).responseText;
					
					var _params_retour = _retour.split("|") ;
					var nbitems = $('#flying_cart_products_list_wrapper ul li.product').length  - 1;	
					$("#header_nb_cart_items span").text(_params_retour[1]) ;
					
					if (_params_retour[1] == 0) {
						$("#flying_cart_content").slideToggle("fast");
						$("body").removeClass("display_flying_cart");
					}else{
						if(nbitems <= nbProductsPerLine){
							$("#flying_cart_products_list_wrapper").css("height", (__NORMAL_SIZE__ / 2) + "px");
							$("#flying_cart_nav_top").hide();
							$("#flying_cart_nav_bottom").hide();
						}
						else{
							$("#flying_cart_products_list_wrapper").css("height", __NORMAL_SIZE__  + "px");
							var lines = Math.ceil(nbitems / nbProductsPerLine);
							if(lines <= nbDisplayedLines){
								$("#flying_cart_nav_top").hide();
								$("#flying_cart_nav_bottom").hide();
								$('#flying_cart_products_list_wrapper ul').css('top', "10px" );
							}
						}
					}
			}
			
			function openFlyingCart(speed){
				var nbProducts = $('#flying_cart_products_list_wrapper ul li.product').length ;
				speed = (speed == null ) ? "normal" : speed;
				$('#flying_cart_nav_top').hide();
	
				if(nbProducts <= nbProductsPerLine){
					$('#flying_cart_products_list_wrapper').css('height', (__NORMAL_SIZE__ / 2) + 'px');
					$('#flying_cart_nav_bottom').hide();	
				}
				else{
					$('#flying_cart_products_list_wrapper').css('height', __NORMAL_SIZE__  + 'px');
					var lines = Math.ceil($('#flying_cart_products_list_wrapper ul li.product').length / nbProductsPerLine);
					if(lines <= nbDisplayedLines){
						$('#flying_cart_nav_bottom').hide();				
					}else{
						$('#flying_cart_nav_bottom').show();
					}
				}
				if($.browser.msie){ /* L'effet n'est pas activé sous IE, le contenu du panier disparait à son ouverture */
					$("#flying_cart_content").show();
				}else{
					$("#flying_cart_content").slideDown(speed);
				}
				
			}
			function closeFlyingCart(speed){
				speed = (speed == null ) ? "normal" : speed;
				if($.browser.msie){
					$("#flying_cart_content").hide();
				}
				else{
					$("#flying_cart_content").slideUp(speed);
				}
			}			

			
			// Boutons de navigation dans le panier volant
			$('#flying_cart_nav_bottom').click(function(){
				var nbLines = Math.ceil($('#flying_cart_products_list_wrapper ul li.product').length / nbProductsPerLine);
				
				if(currentLine < nbLines) {
					var top = parseInt($('#flying_cart_products_list_wrapper ul').css('top').replace('px', ''))
							- (nbDisplayedLines *  pas);
					$('#flying_cart_products_list_wrapper ul').css('top', top + "px" );
					currentLine += nbDisplayedLines;
					if(currentLine > 1) {
						$('#flying_cart_nav_top').show();
					}
					if(currentLine >= nbDisplayedLines){
						$('#flying_cart_nav_bottom').hide();				
					}
				}
			});
			$('#flying_cart_nav_top').click(function(){
				var nbLines = Math.ceil($('#flying_cart_products_list_wrapper ul li.product').length / nbProductsPerLine);
				
				if(currentLine > 1) {
					var top = parseInt($('#flying_cart_products_list_wrapper ul').css('top').replace('px', ''))
							+ (nbDisplayedLines *  pas);
					$('#flying_cart_products_list_wrapper ul').css('top', top + "px" );
					currentLine -= nbDisplayedLines;
					
					if(currentLine <= 1) {
						$('#flying_cart_nav_top').hide();
					}
					if(currentLine < nbDisplayedLines){
						$('#flying_cart_nav_bottom').show();				
					}
				}

			});
			
			
		    //Suppression de tous les articles dans le panier
		    $('#flying_cart a#flying_cart_nav_empty_cart').live('click', function(){
		    	var _action = $('input.action',this).val();
				updateFlyingCart(_action,0,0);
		    });


			//Bouton : fermer le panir volant
			$('#flying_cart_nav_close').click(function(){
				closeFlyingCart("slow"); 
			});	
			
			$("#flying_cart_tab").click( function(){
				if($('#flying_cart_content').css('display') != 'none'){
					closeFlyingCart("slow"); 
				}else{
					openFlyingCart("slow"); 
				}
			});
			
			// Suppression d'un item du panier volant
			$("#flying_cart a.bt_delete").live('click', function(){
				var _action = $("input.action", this).val();
				var _prod = $("input.prod", this).val();
				
				$(this).parent('li.product').hide(function(){
					updateFlyingCart(_action,_prod,0);
					$(this).remove();
				});
			});
			
			// Fermeture du panier volant quand l'utilisateur clique en dehors
			$(document.window).bind('click', function(event){
				var _elem = $('#flying_cart');
				var _current = $(event.target);
			
				if(!_current.is(_elem) && _current.parents('#flying_cart').length <= 0){
					if($('#flying_cart_content').css('display') != 'none'){
						closeFlyingCart(); //$("#flying_cart_content").slideUp("normal");
					}
				}
			});

	});
