var LightboxDiv = function(wrapper,trigger,position,transition) {
	this.wrapper = $(wrapper);
  this.trig = $(trigger);
  this.position = position || 'top';
  this.transition = transition || 'slide';
 
  this.body = $('body');
  this.close = $(wrapper + ' .lbclose');
  this.display = 0;
  this.activateTrigger();
  this.activateClose();
  this.bindResize();
  this.bindScroll();
  this.addExtraWrapper();
  this.addOpaqueLayerDiv();
  this.applyCss();	
};


LightboxDiv.prototype = {
    updateDisplayStatus: function() {
        if(this.display == 0){this.display = 1;}
        else {this.display = 0;}
    },

    bindScroll:function() {
    		var me = this;
    		$(window).bind('scroll',function() {
            if(me.display == 1) {
            		clearTimeout(me.scrollTimer);
            		var closure = function() {
            			me.repositionElements();	
            		};
            		me.scrollTimer = setTimeout(closure,200);
            }
        });
    },
    bindResize: function() {
        var me = this;
        $(window).bind('resize',function() {
            if(me.display == 1) {
                me.repositionElements();
            }
        });
    },
    activateTrigger: function() {
        var me = this;
        this.trig.bind('click', function() {
                if(me.display == 0){me.displayForm();}
                return false;
        });
    },
    activateClose: function() {
        var me = this;
        this.close.bind('click',function(){
            if(me.closeTimer != null) {
                clearTimeout(me.closeTimer);
            }
            me.closeForm();
        });
    },

    toggleForm: function() {
        switch(this.transition) {
            case 'slide':
                this.wrapper.animate({height: 'toggle',opacity: 'toggle'},500,'swing');
                break;
            case 'fade':
                this.wrapper.animate({opacity: 'toggle'},150,'swing');
                break;
            default:
                break;
        }   
        this.updateDisplayStatus();
    },
    displayForm: function() {
        if(this.display == 0) {
            this.repositionElements();
            this.toggleForm();
            this.toggleOpaqueLayer();
        }
    },
    closeForm: function() {
        var me = this;
        if(this.display == 1){
            this.toggleForm();
            this.toggleOpaqueLayer();
            this.trig.unbind();
            this.activateTrigger();
        }
    },
    addOpaqueLayerDiv: function() {
        this.body.append("<div id='opaquelayer'></div>");
        this.ol = $('#opaquelayer');
        this.ol.css({
            minWidth:'100%',
            minHeight:'100%',
            height:'100%',
            opacity:'0.5',
            backgroundColor:'#000000',
            zIndex:'100',
            position:'absolute',
            top:'0',
            left:'0',
            margin:'0',
            padding:'0',
            display:'none'
        });
    },
    toggleOpaqueLayer: function() {
        this.ol.animate({opacity:'toggle'},50,'linear');
    },
    repositionElements: function() {
        this.findWinHeight();
        this.findDocHeight();
        this.findWrapHeight();
        this.findScrollTop();
        this.findDistFromTop();
        this.repositionOpaqueLayer();
        this.repositionWrapper();
    },
    findWinHeight: function() {
        this.winHeight = $(window).height();
    },
    findDocHeight: function() {
        this.docHeight = $(document).height();
    },
    findScrollTop: function() {
        this.scrollTop = $(window).scrollTop();
    },
    findWrapHeight: function() {
        this.wrapHeight = this.wrapper.height();
    },
    repositionOpaqueLayer: function() {
        var h = this.docHeight;
        this.ol.css('height',h+'px');
    },
    repositionWrapper: function() {
            if(this.position == 'center') {
                this.wrapper.css({
                    position:'relative',
                    margin: this.fromTop+'px auto 0 auto'
                });
            }
    },
    findDistFromTop: function() {
        var dist = 0;
        if(this.winHeight > this.wrapHeight && this.position != 'top'){
            var diff = this.winHeight - this.wrapHeight;
            dist = Math.floor((diff-5)/2);
        }
        this.fromTop = dist + this.scrollTop;
	  },
	  addExtraWrapper: function() {
        this.wrapper.wrap("<div id='"+this.wrapper.attr('id')+"extrawrapper'></div>");
        this.extrawrapper = $("#"+this.wrapper.attr('id')+"extrawrapper");
    },
    applyCss: function() {
        switch(this.position) {
            case 'top':
                this.wrapper.css({
                    position:'relative',
                    margin:'0px auto'
                });
                break;
        }
        this.extrawrapper.css({
                    position:'absolute',
                    top:'0',
                    left:'0',
                    width:'100%',
                    zIndex:'1000'
        });
    }
	    
	    
};
