BULLE_SENS_GAUCHE = 1; // positionné à gauche du boutton
BULLE_SENS_DROITE = 2; // positionné à droite du bouton
BULLE_SENS_HAUT = 1; // position au dessus du bouton
BULLE_SENS_BAS = 2; // positionné sous le bouton

TOOL_TIP_W = 30;
TOOL_TIP_H = 30;


/**********************
 *
 *  Class InfoCuisine *
 *
 *********************/
(function(){
 InfoCuisine = function(elt){
  
    this.container = $("#cuisine_sliderContainer");
    this.elt = elt;
    this.bulle = elt.next();

    this.bulle_W = 0;
    this.bulle_H = 0;
    this.refTop = 0; // position y de la bulle ouverte
    this.refLeft = 0; // position x de la bulle ouverte
    this.sensX = BULLE_SENS_DROITE;
    this.sensY = BULLE_SENS_HAUT;

    this.initPosition(this);

    this.isOpening = false;
    this.isClosing = false;

    this.initSensBulle();
    
    this.initListeners(this);
  };
  
  
  InfoCuisine.prototype.initPosition = function(obj){
    obj.bulle_W = this.bulle.width()+15;
    obj.bulle_H = this.bulle.height();
    obj.bulle.width(0);
    obj.bulle.height(0);
    
    var eltOffset = {
      left:obj.elt.css("left").match(/[0-9]+/i)*1, 
      top:obj.elt.css("top").match(/[0-9]+/i)*1
    };
    
    obj.refTop = eltOffset.top - obj.bulle_H;
    if(obj.refTop <= 0){
      obj.refTop += TOOL_TIP_H + obj.bulle_H;
      this.sensY = BULLE_SENS_BAS;
    }
    
    obj.refLeft = eltOffset.left + TOOL_TIP_W;
    if(obj.refLeft + obj.bulle_W > obj.container.width()){
      obj.refLeft -= obj.bulle_W + TOOL_TIP_W;
      obj.sensX = BULLE_SENS_GAUCHE;
    }
    
    var pos = obj.getClosedCoords(obj);    

    obj.bulle.css({
      display:"block",
      "left":pos.left,
      "top":pos.top
    });
  };
  
  
  
  
  /**
  * retourne les coords {left, top} quand la bulle est ouverte
  */
  InfoCuisine.prototype.getOpenedCoords = function(obj){
    return {left:obj.refLeft, top:obj.refTop};
  };
  
  
  /**
   * retourne les coords {left, top} quand la bulle est fermées (donc prète à être ouverte
   */
  InfoCuisine.prototype.getClosedCoords = function(obj){
    var pos = {};

    if(obj.sensX == BULLE_SENS_GAUCHE){
      pos.left = obj.refLeft + obj.bulle_W;
    }else{
      pos.left = obj.refLeft;
    }
    
    if(obj.sensY == BULLE_SENS_BAS){
      pos.top = obj.Reftop - obj.bulle_H;
    }else{
      pos.top = obj.refTop + obj.bulle_H;
    }
    
    return pos;
  };
  
  
  
  InfoCuisine.prototype.initSensBulle = function(){
    this.sens = 1;
  };
  
  
  
  
  InfoCuisine.prototype.initListeners = function(obj){
    
    obj.elt.click(function(evt){
      evt.preventDefault();
     })
     
    obj.elt.mouseenter(function(evt){
      if(!obj.isOpening){
        obj.isOpening = true;
        if(!obj.isClosing){ 
          obj.openBulle(obj);
        }
      }
    });
    
    obj.elt.mouseleave(function(evt){
      if(!obj.isClosing){
        obj.isClosing = true;

        if(!obj.isOpening){
           obj.closeBulle(obj);
        }
      }
    });
   
  }; // end initListeners
  
 

/**
 * ouverture de la bulle
 */
 InfoCuisine.prototype.openBulle = function(obj){ 
   var coords = obj.getOpenedCoords(obj);
    obj.bulle.animate(
        {
          width:obj.bulle_W, 
          height:obj.bulle_H, 
          top:coords.top,
          left:coords.left
        },{
          duration:300,
          complete: function() {
            obj.isOpening = false;
            obj.isClosing = false;
            $(this).children().first().removeClass("no_visible");
          },
          easing:"easeOutCirc",
          step: function(now, fx){
            if(obj.isClosing){
              obj.isOpening = false;
              $(this).stop();
              obj.closeBulle(obj);
            }
          }
        }
  );
    
}; // end openBulle



/**
 * fermeture de la bulle
 */
 InfoCuisine.prototype.closeBulle = function(obj){ 
   obj.bulle.children().first().addClass("no_visible");
   var coords = obj.getClosedCoords(obj);
   var top = coords.top;
   var left = coords.left;
   var h = 0;
   var w = 0;
    obj.bulle.animate(
      {
        width:w, 
        height:h, 
        top:top,
        left:left
      },{
      duration:300,
      complete: function() {
          obj.isClosing = false;
          obj.isOpening = false;
      },
      easing:"easeOutCirc",
      step: function(now, fx){
          if(obj.isOpening){
            obj.isClosing = false;
            $(this).stop();
            obj.openBulle(obj);
          }
      }
    });
    
 }; // end closeBulle

})();
// end Class InfoCuisine
