jQuery(function(){

jQuery('a.add_to_cart_from_list, a.remove_from_cart_from_list, a.remove_all_from_cart_from_list').click(function(){

  /* centrer et faire apparaitre l'indicateur de chargement 
  changeCss(jQuery('#indicator'));
  // notification apparait puis disparait					          
  jQuery('#indicator').fadeIn(250);
           */
  var classe_action = jQuery(this).attr('class');
       
  id_product = jQuery(this).attr('name');
  objet = jQuery(this);
  
  switch(classe_action){
      case 'add_to_cart_from_list':
        action = 'add';
      break;
      
      case 'remove_from_cart_from_list':
         action = 'remove';
      break;
      
      case 'remove_all_from_cart_from_list':
         action = 'remove_all';
      break;
  }
  
  
      
  jQuery.post('list_product_page_cart.php',{id:id_product, action:action},function(number_of_selected_instances){
  
    if(number_of_selected_instances!=''){
      jQuery(objet).parents(0).next('.total-exemplaires').html("("+number_of_selected_instances+")");
    }else{
      jQuery(objet).parents(0).next('.total-exemplaires').html("");
    };
    
    
    //faire disparaitre l'indicateur de chargement
      //jQuery('#indicator').hide('fast');
      
    //afficher une notification de modification du nbr d'exemplaires commandés
    notify_cart_content_modified(jQuery(objet));
  })
  
    
  return false;
});
    
    /* fonction count_product_number_of_selected_exemplaires()
        
         pour compter le nombre d'exemplaires d'un produit sélectionnés dans la commande: 
         
        - envoyer en POST l'id du produit cliqué
        - fichier receveur utilise l'id_produit pour compter le nbr de VAR $_SESSION[id_produit] correspondantes à ce produit
        - afficher résultat
        - html(résultat)
      
        */
         
    function count_product_number_of_selected_exemplaires(objet){
    
      //récupérer l'id du produit : product_N
      product_id = jQuery(objet).attr('name');
      
      jQuery.post('count_product_number_of_selected_exemplaires.php',{product_id:product_id},function(data){
        //indiquer le nombre d'exemplaires commandés dans le champ prévu à cet effet
        jQuery(objet).parents(0).next('.total-exemplaires').html(data);
      })
      
    };
    /* fin fonction count_product_number_of_selected_exemplaires() */ 
    
    /*  function notify_cart_content_modified() */
     function notify_cart_content_modified(objet){
      
      name_of_the_product = jQuery(objet).attr('id');
      action = jQuery(objet).attr('class');
      product_type= jQuery(objet).attr('href');
      
        switch(action){
          case 'add_to_cart_from_list':
            var message = product_type+' '+ name_of_the_product +' ajoutée à votre commande';
          break;
          case 'remove_from_cart_from_list':
            var message = product_type+' '+ name_of_the_product +' retirée de votre commande';
          break;
          case 'remove_all_from_cart_from_list':
            var message = product_type+'(s) '+ name_of_the_product +' retirée(s) de votre commande';
          break;
        }
      
      
      
      jQuery('#notify').html(message);
      changeCss(jQuery('#notify'));
      
      /* notification apparait puis disparait */					          
      jQuery('#notify')
      .fadeIn(500)
      .fadeOut(500);
     
     }
    /* fin function notify_cart_content_modified() */
    
        
    /* fonction pour centrer la boite de notification */
      function changeCss(element){
  
          var imageHeight = jQuery(element).height();
          var imageWidth = jQuery(element).width();
          var windowWidth = jQuery(window).width();
          var windowHeight = jQuery(window).height();
          var scrollHeight = jQuery(window).scrollTop();
          
          jQuery(element).css({
              "position" : "absolute",
              "left" : windowWidth / 2 - imageWidth / 2 ,
              "top" : windowHeight /2 - imageHeight / 2 + scrollHeight
          });
      };
        
    // compter aprés le chargement de la page, le nombre d'exemplaires commandés pour chaque produit
    jQuery('.add_to_cart_from_list').each(function(){
      count_product_number_of_selected_exemplaires(this);
    });
})
