
var Helpbubble = Class.create({
  /**
   * Build the helpbubble object
   * Insert it inside the correct parent element
   *
   * @param String parentid The id of the helpbubble's parent
   * @param String classname The class of the helpbubble
   * @param Array message The ids and bodies of every helpbubble message
   */
  initialize: function(parentid, classname, messages) {
    this.timer = null;
    this.duration = 3000;
    this.fadespeed = 0.25;
    this.parentObj = $(parentid);
    this.messages = messages;
    
    this.bubble = $(document.createElement('div')).addClassName(classname);
    this.parentObj.insert({top: this.bubble});
    this.hideHelp();
  },
  
  /**
   * Hide the helpbubble immediately
   */
  hideHelp: function() {
    if(this.timer) this.clearTimer();
    this.bubble.hide();
  },
  
  /**
   * Set the timer to fade after the duration
   */
  setTimer: function() {
    this.timer = setTimeout(this.fadeHelp.bind(this), this.duration);
  },
  
  /**
   * Fade the bubble
   */
  fadeHelp: function() {
    this.clearTimer();
    this.bubble.visualEffect("Fade", {duration: this.fadespeed});
  },
  
  /**
   * Clear the fade timer, don't fade
   */
  clearTimer: function() {
    if(this.timer) clearTimeout(this.timer);
  },
  
  /**
   * Display the helpbubble and fill it with our chosen message
   * @param String msg_id The id of the message to display
   */
  showHelp: function(msg_id) {
    this.hideHelp();
    
    this.bubble.id = msg_id;
    this.bubble.innerHTML = this.messages[msg_id];
    this.bubble.show();
    this.setTimer();
  }
  
  
});