// moo prompt touched by Mehmet Fatih YILDIZ (fatih@mfyz.com, www.mfyz.com)
// removed button actions, non-growing effects and custom positions

var MooPrompt = Box = new Class({
  setOptions: function(options){
    this.options = {
      title: null,
      width: 300,        // Set width of the box
      classes: 'box',  // default style id
      height: 0,         // Set height of the box (0 = sized to content)
      maxHeight: 500,    // Maximum height of the dialog box
      delay: 0,          // Delay before closing (0=no delay)
      overlay: true     // Cover the page
    };
    Object.extend(this.options, options || {});
  },
  
  initialize: function(content, options){
    this.setOptions(options);
    this.title = this.options.title;
    this.text  = content;
    this.cs    = this.options.classes; // class suffix
    if (document.all && document.compatMode) {
      this.iframe = new Element('iframe').setStyles({
        'position': 'absolute', 'top': 0, 'left': 0,
        'height': Window.getScrollHeight()+'px', 'width': Window.getScrollWidth()+'px'
      }).setProperties({
        'frameborder': 0, 'scrolling': 'no'
      }).injectInside(document.body);
    }
    if (this.options.overlay) {
      if ($(this.options.classes+'Overlay')) {
        this.overlay = $(this.options.classes+'Overlay');
      } else {
        this.overlay = new Element('div').setProperty('id', this.cs+'Overlay').addClass('boxOverlay');
        this.overlay.setStyles({
          'position': 'absolute', 'top': 0, 'left': 0, 'width': '100%', 'visibility': 'hidden'
        }).injectInside(document.body);
      }
    }
    this.container = new Element('div').setProperty('id', this.cs).addClass('boxContainer');
    this.container.setStyles({
      'position': 'absolute', 'visibility': 'hidden'
    }).injectInside(document.body);
    this.box = new Element('div').setProperty('class', 'Box');
    this.box.setStyles({
      'width': this.options.width+'px',
      'overflow': 'auto'
    }).injectInside(this.container);
    if(this.title) this.header = new Element('h3').setProperty('class', 'Header').setHTML(this.title).injectInside(this.box);
    this.content = new Element('div').setProperty('class', 'Content').injectInside(this.box);
    if ($type(content) == 'element' ) {content.injectInside(this.content);
    } else {this.content.setHTML(this.text);}
    this.boxHeight = (this.box.offsetHeight < this.options.maxHeight) ? this.box.offsetHeight : this.options.maxHeight;
    this.boxHeight = (this.options.height > 0) ? this.options.height : this.boxHeight;
    this.box.setStyle('display', 'none');
    this._position();
    this.eventPosition = this._position.bind(this);
    window.addEvent('scroll', this.eventPosition).addEvent('resize', this.eventPosition);
    if (this.options.overlay && this.overlay.getStyle('visibility') == 'hidden')
       this.fx1 = new Fx.Style(this.overlay, 'opacity', {duration:500}).custom(0, .8);
    // grow effect
    this.container.setStyle('top', (Window.getScrollTop()+(Window.getHeight()/2))+'px');
    var style = {}; style.height = 0; style.width = 0;
    this.container.setStyles(style);
    this.container.setStyle('visibility', '');
    this.fx2 = new Fx.Styles(this.container, {duration: 500});
    this.fx2.custom({
      'width': [0, this.options.width], 'margin-left': [0, -this.options.width/2], 'margin-right': [0, -this.options.width/2],
      'height': [0, this.boxHeight], 'margin-top': [0, -this.boxHeight/2], 'margin-bottom': [0, -this.boxHeight/2]
    }).chain(function() {
      this.box.setStyles({
        'visibility': 'hidden', 'display': '', 'height': this.boxHeight+'px'
      });
      new Fx.Style(this.box, 'opacity', {duration: 500}).custom(0, 1).chain(function() {
        this.box.setStyle('filter', '');
        if (this.options.delay > 0) {
          var fn = function () { this.close() }.bind(this).delay(this.options.delay);
        }
      }.bind(this));
    }.bind(this));
  },
  
  _position: function() {
    var wHeight = (Window.getScrollHeight() > Window.getHeight()) ? Window.getScrollHeight() : Window.getHeight();
    var lr = this.options.width/2;
    var tb = this.boxHeight/2;
    if (this.iframe) {
      this.iframe.setStyles({
        'height': Window.getScrollHeight()+'px',
        'width': Window.getScrollWidth()+'px'
      });
    }
    if (this.options.overlay)  this.overlay.setStyles({height: wHeight+'px'});
    // middle center
    this.container.setStyle('top', (Window.getScrollTop()+(Window.getHeight()/2))+'px');
    this.container.setStyle('left', '50%');
  },
  
  close: function(fn) {
    if ($type(this.fx1) == 'object')  this.fx1.clearTimer();
    this.fx2.clearTimer();
    if (typeof(fn) == 'function') {fn();}
    if (this.options.overlay && this.overlay.getStyle('opacity') == 0.8) {  new Fx.Style(this.overlay, 'opacity', {duration:250}).custom(0.8, 0);  }
    new Fx.Style(this.container, 'opacity', {duration:250,
      onComplete: function() {
        window.removeEvent('scroll', this.eventPosition).removeEvent('resize', this.eventPosition);
        if (this.options.overlay)  this.overlay.remove();
        this.container.remove();
        if (this.iframe)  this.iframe.remove();
      }.bind(this)
    }).custom(1, 0);
  }
});

MooPrompt.implement(new Chain);