var Lightbox = {
  init: function(bid, options) {
  	options = Object.extend({
  		closeFunction: function() {
  			Lightbox.hide();
  		}}, options);
    this.box = new Element('div', {id: 'ajax-sf-target'});
    this.box.addClassName('lightbox');
    var firstDiv = new Element('div');
    firstDiv.addClassName('corner-garlands');
    this.target = new Element('div', {id: 'ajax-smallframe'});
    this.target.addClassName('corner-garlands-bottom');
    firstDiv.insert(this.target);
    this.box.insert(firstDiv);
    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);
    this.closeFunction = options.closeFunction;
  },
  setDimensions: function(w, h) {
    this.box.setStyle({width: (w+20) + 'px', height: (h+20) + 'px'});
    this.target.setStyle({width: w + 'px', height: h + 'px'});
  },
  setContent: function(content) {
    var type = typeof content;
    if(type == 'function') {
      this.target.update(content());
      return;
    }
    if(type == 'string') {
      this.target.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'});
  },
  setCloseFunction: function(closeFunction) {
  	this.closeFunction = closeFunction;
  	if($('ajax-smallframe-close')) {
  		$('ajax-smallframe-close').stopObserving('click');
  		$('ajax-smallframe-close').observe('click',  this.closeFunction);
  	}
  },
  show: function() {
    this.setPos();
    this.backShield.show();
    this.box.show();
    document.observe('keypress', this.checkForEscape);
  },
  hide: function() {
    document.stopObserving('keypress',  this.checkForEscape);
    this.box.hide();
    this.backShield.hide();
  },
  isShowing: function() {
  	return this.box.visible();
  },
  checkForEscape: function(event) {
    if(event.keyCode == Event.KEY_ESC) {
      Lightbox.closeFunction();
    }
  },
  addClose: function() {
    var close = new Element('div', {id: 'ajax-smallframe-close'});
    close.observe('click',  this.closeFunction);
    this.target.insert(close);
  }
};
