var Lightbox = function() {

    return {
        idPrefix: 'lightBox',
        maskColour:'#000000',
        maskOpacity: 0.5,
        fadeDuration: 0.5,

        closeImage:'/images/btn_box_close.png',
        closeImageStyles:{},
    
        show: function(id, w, h) {

            this.hide();

            if (!$(this.idPrefix + 'Dimmer')) {
                var tmp = window.opera && parseFloat(window.opera.version()) < 9.5 ? document.documentElement : document.body
                var black = new Element('DIV');
                black.id = this.idPrefix + 'Dimmer';
                black.style.width = (tmp.clientWidth) + 'px';
                black.style.height = (tmp.clientHeight) + 'px';
                black.setOpacity(0.4);
                document.body.appendChild(black);
            }
            else {
                black = document.getElementById(this.idPrefix + 'Dimmer');
            }
            black.style.display = 'block';
            if (typeof id == "string") {
                id = $(id);
            }
            w = w || 400;
            h = h || 300;

            id.style.zIndex = 550;
            id.style.display= 'block';
            $(id).setStyle({
                width: w + 'px',
                height: h + 'px'
            });
            this.setLightBoxPosition(id, w, h);
            Event.observe(window, 'scroll', function(){
                this.setLightBoxPosition(id, w, h);
            }.bind(this));
        },

        showUrl: function(conf) {

            var url   = conf.url     || '',
                w     = conf.width   || 400,
                h     = conf.height  || 400,
                id    = conf.id      || 'anon',
                cb    = conf.cb      || Prototype.emptyFunction,
                el    = $(id),
                inner = $(id + '-inner'),
                close = $(id + '-close');

            

            if(!el) {

                el = new Element('DIV',{
                    id:id
                });
                el.setStyle({
                    position: 'absolute',
                    display: 'none',
                    zIndex: 2000,
                    backgroundColor:'#ffffff'
					//,
					//height: h + 16 + 'px'
                });
                //                el.update('oo');

                inner = new Element('DIV', {id:id+'-inner'});
                inner.setStyle({
                    overflow:'auto',
                    width: w + 'px',
                    height: h + 'px'
                });

                el.insert(inner);

                $(document.body).insert(el);
            }


            this.setPosition(el, w, h);

            new Ajax.Updater(inner, url, {
                evalScripts: true,
				method: 'GET',
                onComplete: function() {
                    if(!close) {
                        close = new Element('DIV', {
                            id:id + 'Close'
                        });

                        var closeStyles = {
                            position: 'absolute',
                            display: 'block',
                            top: '0',
                            right: '-30px',
                            width: '16px',
                            height: '16px',
                            zIndex: 2100,
                            cursor:'pointer',
                            background:'transparent url('+this.closeImage+') no-repeat scroll 0 0'
                        }
                        Object.extend(closeStyles, this.closeImageStyles);


                        close.setStyle(closeStyles);
                        close.observe('click', function() {
                            this.hide();
                        }.bind(this));
                        
                        close.update('&nbsp;');
//                        console.log(close);
                        el.insert(close);
                    }
                    this.maskPage(function() {
                        el.appear({
                            duration: this.fadeDuration,
                            afterFinish: function() {
                                cb();
                                inner.setStyle({overflow:'auto'});
                            }
                        });
                    }.bind(this));

                    this.activeId = id;

                    
                }.bind(this)
            });




        },

        hide: function(){
            var el = $(this.activeId);
            var inner = $(this.activeId+'-inner');
            if(el) {
                inner.setStyle({overflow:'auto'});
                this.activeId = null;
                el.fade({
                    duration: this.fadeDuration,
                    afterFinish: this.removeMask.bind(this)
                });
                
            }
            else {
                this.removeMask();
            }

        },

        maskPage: function(cb) {
            var mask;

            if (!$(this.idPrefix + 'Mask')) {
                var tmp = window.opera && parseFloat(window.opera.version()) < 9.5 ? document.documentElement : document.body

                mask = new Element('DIV');
                mask.id = this.idPrefix + 'Mask';
                mask.setStyle({
                    top:  0,
                    left: 0,
                    width:  tmp.clientWidth + 'px',
                    height: tmp.clientHeight + 'px',
                    display:'none',
                    position:'absolute',
                    backgroundColor:this.maskColour,
                    zIndex:1000
                });

                $(document.body).insert(mask);

            }
            else {
                mask = $(this.idPrefix + 'Mask');
            }

            mask.appear({
                from:0,
                duration: this.fadeDuration,
                to:this.maskOpacity,
                afterFinish: cb || Prototype.emptyFunction
            });


        },
        removeMask: function() {
            var mask = $(this.idPrefix + 'Mask');
            if (mask) {
                mask.fade({
                    from: this.maskOpacity,
                    duration: 0.5,
                    to: 0
                });
            }
        },
        setPosition: function(el, w, h){
            var viewport_width = document.viewport.getWidth();
            var viewport_height = document.viewport.getHeight();
            var vpscroll_top = document.viewport.getScrollOffsets()['top'];

            var new_top = ((viewport_height - h) / 2) + vpscroll_top;

            el.setStyle({
                marginLeft: '-' + Math.round(w / 2) + 'px',
                left: '50%',
                top: new_top + 'px',
                position:'absolute'
            });

        }
    }
}();
