﻿var Lightbox = function(){
    var priv = {
        $popup : null,
        width : null,
        txtCloseButton : 'Sluiten'
    };
    
    return {        
        Show : function (popupWidth, popupContentId){
            if(popupWidth == null){
                popupWidth = 500;
            }
            priv.width = popupWidth;
                        
            //create popup element, with unique Id
            if($('#lightbox_' + popupContentId).length){
                priv.$popup = $('#lightbox_' + popupContentId);

            } else {
                priv.$popup = $('<div id="lightbox_' + popupContentId + '" class="lightbox">'+$('#'+popupContentId).html()+'</div>');
                $('#'+popupContentId).remove();
            }
                                    
            // create page overlayDiv if it doesnt exist
            if($('#popupOverlay').length == 0){
                $('body').append($('<div id="popupOverlay">'));
                $('#popupOverlay').css({'z-index':'999','background-color':'#4d4d4d','position':'absolute','top':'0','left':'0','opacity':'.4','filter':'alpha(opacity=40)','display':'none'});
            }
            
            var screenWidth = $(window).width();
            var screenHeight = $(window).height();
            //var scrollY = $(document).scrollTop();
            var pageX = $(document).width();
            var pageY = $(document).height();
            
            // set heigth and width of overlay
            $('#popupOverlay').width(pageX);
            $('#popupOverlay').height(pageY);
            $('#popupOverlay').bind("click", 
	            function(){
	                Lightbox.Hide();
	            }
	        );
	        
	        // initialize popup
            if(!priv.$popup.is('.initialized')){
                // add title bar with close button
                var popupHtml = '<div class="header">' + $('.pTitle', priv.$popup).html() + ' <a class="close" href="javascript:void(0);">' + priv.txtCloseButton + '</a></div>';
                popupHtml +=    '<div class="content">' + priv.$popup.html() + '</div>';
                priv.$popup.html(popupHtml);
                priv.$popup.css({'width':priv.width+'px'});
			    
                $('body').append(priv.$popup);
                $("a.close", priv.$popup).bind("click", 
                    function(evt){
                        Lightbox.Hide();
                    }
                );
			    priv.$popup.addClass('initialized');
            }
            
            // set position of popup
            var topPopup = ((screenHeight/2) + 0)-(priv.$popup.height()/2);
            if(topPopup < 0) {
                topPopup = 10;
            }
            
            if($.browser.msie6){ // ie 6
                priv.$popup.css({'left':((screenWidth/2)-(priv.width/2))+'px'});
            } else {
                priv.$popup.css({'top':topPopup+'px','left':((screenWidth/2)-(priv.width/2))+'px'});
            }
                        
            // show overlay if necessary
            $('#popupOverlay').show();
            
            // show the popup
            priv.$popup.show().css("display", "block");
        },
        
        Hide : function () {
            $('#popupOverlay').hide();
            priv.$popup.hide();
        },
        
        Remove : function(){
            $('#popupOverlay').remove();
            priv.$popup.remove();
        },
        
        OnResize : function() {
		    if(priv.$popup != null){
		        var screenWidth = $(window).width();
                var screenHeight = $(window).height();
                //var scrollY = $(document).scrollTop();
                var pageX = $(document).width();
                var pageY = $(document).height();
    		    
                if($('#popupOverlay').length > 0){
                    // set width of overlay
                    $('#popupOverlay').width(pageX);
                }
                
                if(priv.$popup){
                    var topPopup = ((screenHeight/2) + 0)-(priv.$popup.height()/2);
                    if(topPopup < 0) {
                        topPopup = 10;
                    }
                    if($.browser.msie6){ // ie 6
                        priv.$popup.css({'left':((screenWidth/2)-(priv.$popup.width()/2))+'px'});
                    } else {
                        priv.$popup.css({'top':topPopup+'px','left':((screenWidth/2)-(priv.$popup.width()/2))+'px'});
                    }
                }
            }
        }
    }
}(jQuery);