var Lightbox = {
	lbox : null,
	target : null,
	body : null,
	html : null,
	overflow : null,
	position: null,
	height : null,
	scrollTop : null,
	params : null,
	_unbox : null,
	_move : null,
	
	box : function(id, params) {
		this.params = params || {}
		this._unbox = this._unbox || this.unbox.bindAsEventListener(this);
		this._move = this._move || this.move.bindAsEventListener(this);
		this.target = $(id);
		this.body = this.body || $(document.body);
		this.html = this.html || $(this.body.parentNode);

		if (!this.lbox) {
			this.lbox = $(document.createElement('div'));
			this.body.appendChild(this.lbox);
	        this.html.setStyle({height: '100%'});
		}
		this.overflow = this.overflow || this.body.getStyle('overflow');
		this.position = this.position || this.body.getStyle('position');		
		this.height = this.height || this.body.getStyle('height');
		this.scrollTop = Window.getScrollTop(window);
		
		window.scrollTo(0,0);
		
		this.lbox.setStyle({
			backgroundColor: this.params.color || '#000000',
			opacity: this.params.opacity || .8,
			position: 'absolute',
			top: 0,
			left: 0,
			height: '100%',
			width: '100%',
			zIndex: this.target.getStyle('z-index')-1,
			cursor: 'pointer'
		});
		
        this.lbox.observe('click', this._unbox);
      // if (!this.params.left || !this.params.top) {
      //    Event.observe(window, 'resize', this._move);
      // }
		this.body.setStyle({
			overflow: 'hidden',
			position: 'relative',
			height: '100%'
		});
		this.move();
		this.target.show();
		this.lbox.show();
	},
	
	unbox : function() {
		this.body.setStyle({
			overflow: this.overflow || '',
			position: this.position || '',
			height: this.height || ''
		});
		this.lbox.hide();
		this.target.hide();
		window.scrollTo(0, this.scrollTop);
	},
	
	move : function() {
		var center = this.getCenter();
		this.target.setStyle({
			left: this.params.left || center.left + 'px',
			top: this.params.top || center.top + 'px'
		})
	},
	
	getCenter : function() {
		var l = this.lbox.getWidth()/2 - this.target.getWidth()/2;
		var t = this.lbox.getHeight()/2 - this.target.getHeight()/2;
		return {left: l, top: t}
	}
}

var Window = {
    /* gets offset from the top of the viewable area */
    getScrollTop : function(target) {
        target = target || window;
        if (target.document || target.contentDocument) {
            _window = target;
            _document = _window.contentDocument || _window.document;
            target = null;
        }
        if (target) {
            return target.scrollTop;
        } else if (_document.documentElement && _document.documentElement.scrollTop) {
            return _document.documentElement.scrollTop;
        } else if (_document.body && _document.body.scrollTop) {
            return _document.body.scrollTop;
        } else if (_window.pageYOffset) {
            return _window.pageYOffset;
        } else if (_window.scrollY) {
            return _window.scrollY;
        }
        return 0;
    },

    setScrollTop : function(target, amount) {
        target = target || window;
        if (target.document || target.contentDocument) {
            _window = target;
            _document = _window.contentDocument || _window.document;
            target = null;
        }
        if (target) {
            target.scrollTop += amount;
        } else if (_document.body && _document.body.scrollTop != undefined) {
            _document.body.scrollTop = amount;
        } else if (_document.documentElement && _document.documentElement.scrollTop != undefined) {
            _document.documentElement.scrollTop = amount;
        } else if (_window.pageYOffset != undefined) {
            _window.pageYOffset = amount;
        } else if (_window.scrollY != undefined) {
            _window.scrollY = amount;
        }
        return 0;
    }
}