var Popup = Class.create();
Popup.prototype = {
  initialize: function(element) {
    this.popup = $(element);

    return this;
  },

  show: function(e) {
    this.popup.style.top = (Event.pointerY(e) - 50) + "px";
    Event.stop(e);
    Effect.Appear(this.popup, { duration: 0.25 });

    this.eventHandler = this.handleClick.bindAsEventListener(this);
    Event.observe(document, 'mousedown', this.eventHandler);
  },

  hide: function() {
    if (this.popup.visible()) {
      Effect.Fade(this.popup, { duration: 0.25 });

      Event.stopObserving(document, 'mousedown', this.eventHandler);
    }
  },

  handleClick: function(event) {
    if (this.popup) {
      var x = Event.pointerX(event);
      var y = Event.pointerY(event);

      if (!Position.within(this.popup, x, y)) {
        this.hide();
      }
    }
  } 
};
