
var OpenedLightbox = null;

function cLightbox() {
    var _this = this;

    var _showModule = null;
    var _lightBoxElement = null;

    this.setContent = function (element) {
        if (element != null) {
            _showModule = element;
        }
    }

    this.resize = function () {
        if (_showModule != null) {
            var tmp = _showModule.offsetHeight;

            var height = _showModule.style.height;

            if (height != '') {
                height = height.replace('px', '');
                height = parseInt(height);
            }

            var width = _showModule.style.width;

            if (width != '') {
                width = width.replace('px', '');
                width = parseInt(width);
            }

            if (height == null || height == '' || height == 'NaN' || height == 0 || height == 'auto') {
                height = _showModule.offsetHeight;
            }

            if (width == null || width == '' || width == 'NaN' || width == 0 || width == 'auto') {
                width = _showModule.offsetWidth;
            }

            var bodyWidth = document.body.offsetWidth;
            var bodyHeight = 0;

            if (window.innerHeight) {
                bodyHeight = window.innerHeight;
            } else {
                bodyHeight = this.getDocHeight();
            }

            var margWidth = bodyWidth - width;
            var margHeight = bodyHeight - height;

            _showModule.style.marginLeft = (margWidth / 2) + 'px';
            _showModule.style.marginTop = (margHeight / 2) + 'px';

            _showModule.className = 'OpacityNone';
        }
    }

    /**
     * FROM: http://james.padolsey.com/javascript/get-document-height-cross-browser/
     */
    this.getDocHeight = function () {
        var D = document;
        return Math.max(
            Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
            Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
            Math.max(D.body.clientHeight, D.documentElement.clientHeight)
            );
    }

    this.open = function () {
        OpenedLightbox = this;

        _lightBoxElement = document.createElement('div');
        _lightBoxElement.obj = this;
        _lightBoxElement.className = 'PositionFixedFull HeightWidthFull OpacityHalf';
        _lightBoxElement.style.backgroundColor = '#000';
        _lightBoxElement.style.zIndex = '999';

        if (_showModule != null) {
            document.body.appendChild(_lightBoxElement);
            _lightBoxElement.onclick = function() {
                this.obj.close();
            }

            _showModule.className = 'OpacityFull';
            _showModule.style.position = 'absolute';
            _showModule.style.zIndex = '1000';
            _showModule.style.display = '';

            window.setTimeout('OpenedLightbox.resize()', 20); //God damned workaround weil es immer wieder probs mit der offsetHeight gab
        }
    }

    this.close = function() {
        _showModule.style.display = 'none';
        document.body.removeChild(_lightBoxElement);
    }

    this._onclick = function(onclickEvent) {

        if (onclickEvent && typeof(onclickEvent) == 'function') {
            _lightBoxElement.onclick = onclickEvent;
        } else {
            _lightBoxElement.onclick = function() {
                this.obj.close();
            }
        }
    }
}
