var Lightbox = {
  init: function(bid) {
    this.box = new Element('div', {id: 'ajax-smallframe'});
    this.box.addClassName('lightbox');
    this.backShield = new Element('div', {id: 'fade-out'});
    this.backShield.setStyle({width: document.viewport.getWidth() + 'px', height: document.viewport.getHeight() + 'px'}).setOpacity(0.5);
    this.box.hide();
    this.backShield.hide();
    Event.observe((document.onresize ? document : window), 'resize', function(event) {
      Lightbox.setPos();
    });
    var bodyElement = $(bid);
    bodyElement.insert(this.box).insert(this.backShield);
  },
  setDimensions: function(w, h) {
    this.box.setStyle({width: w + 'px', height: h + 'px'});
  },
  setContent: function(content) {
    var type = typeof content;
    if(type == 'function') {
      this.box.update(content());
      return;
    }
    if(type == 'string') {
      this.box.update(content);
      return;
    }
  },
  setPos: function() {
    this.backShield.setStyle({width: document.viewport.getWidth() + 'px', height: document.viewport.getHeight() + 'px'})
    var width = this.box.getWidth();
    var height = this.box.getHeight();
    var v_width = document.viewport.getWidth();
    var v_height = document.viewport.getHeight();
    var posTop = (v_height/2) - (height/2);
    var posLeft = (v_width/2) - (width/2);
    this.box.setStyle({top: posTop + 'px', left: posLeft + 'px'});
  },
  show: function() {
    this.setPos();
    this.backShield.show();
    this.box.show();
    this.evListen = this.checkForEscape.bindAsEventListener(this);
    document.observe('keypress', this.evListen);
  },
  hide: function() {
    document.stopObserving('keypress', this.evListen);
    this.box.hide();
    this.backShield.hide();
  },
  checkForEscape: function(event) {
    if(event.keyCode == Event.KEY_ESC) {
      Lightbox.hide();
    }
  },
  addClose: function(dest) {
    var close = new Element('div', {id: 'ajax-smallframe-close'});
    if(dest == 'hide') {
      var res = function(event) {
	Lightbox.hide();
      };
    }else if(dest == 'reload') {
      var res = function(event) {
	window.location.reload();
      };
    }else{
      var res = function(event) {
	window.location.href = dest;
      };
    }
    close.observe('click', res);
    this.box.insert(close);
  }
};