var LightyBox = Class.create();

LightyBox.prototype = {
  initialize: function(content){
    this.options = Object.extend({
      overlayId: 'lighty_overlay',
      contentClassName: 'lighty_content' 
    }, arguments[1] || {});

    if(typeof(content) == 'string')
      content = this.createElement(content);
    this.content = content;
    this.createOverlay();
  },
  show: function(){
    this.setScrollPosition();
    this.setBodyStyle('hidden', '100%');
    this.setOverlayDimensions();
    this.setSelectStyle(function(el){
      el.__original_visibility = el.style.visibility;
      el.style.visibility = 'hidden';
    });
    this.overlay.style.display = 'block';
    this.content.style.display = 'block';
  },
  hide: function() {
    this.setBodyStyle('auto', 'auto');
    this.setSelectStyle(function(el){
      el.style.visibility = el.__original_visibility;
    });
    this.overlay.style.display = 'none';
    this.content.style.display = 'none';
    this.repairScrollPosition();
  },
  setScrollPosition: function() {
    if(this.ie() ){
      Position.prepare();
      this.position = {
        x: Position.deltaX,
        y: Position.deltaY
      };
      window.scrollTo(0, 0);
    }
  },
  repairScrollPosition: function() {
    if(this.ie() ){
      window.scrollTo(this.position.x, this.position.y);
    }
  },
  ie: function() {
    return navigator.appVersion.match(/MSIE/);
  },
  createOverlay: function(){
    var div;
    if(div = $(this.overlayId)){
      // none
    }else{
      div = document.createElement('div');
      div.id = this.options.overlayId;
      document.body.appendChild(div);
    }
    this.overlay = div;
  },
  setOverlayDimensions: function(){
    var d = Element.getDimensions(document.getElementsByTagName('html')[0]);
    this.overlay.style.width = d.width + 'px';
    this.overlay.style.height = d.height + 'px';
  },
  setSelectStyle: function(proc){
    // for WinIE z-index bug
    if(this.ie() )
      $A(document.getElementsByTagName('select')).each(proc);
  },
  setBodyStyle: function(overflow, height){
    document.getElementsByTagName('body')[0].style.height = height;
    document.getElementsByTagName('body')[0].style.overflow = overflow;
    document.getElementsByTagName('html')[0].style.height = height;
    document.getElementsByTagName('html')[0].style.overflow = overflow;
  },
  createElement: function(str) {
    var content = document.createElement('div');
    content.className = this.contentClassName;
    document.body.appendChild(div);
    return content;
  }
}
