var popupStatus = 0; 

$(document).ready(function(){  
	$("#lightboxClose").click(function(){
		disablePopup();
	});
	
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});
});
	
function loadPopup(){  
	if(popupStatus==0){  
		centerPopup();
		$("#backgroundPopup").css({"opacity": "0.7"});  
		$("#backgroundPopup").fadeIn("slow");  
		$("#lightbox").fadeIn("slow");  
		popupStatus = 1;  
	}  
}

function disablePopup(){  
	if(popupStatus==1){  
		$("#backgroundPopup").fadeOut("slow");  
		$("#lightbox").fadeOut("slow");  
		popupStatus = 0;  
	}  
}




//centering popup
function centerPopup(){
	//request data for centering
	


	var windowWidth = $(window).width();
	var windowHeight = $(window).height();
	var fullHeight = $.getDocHeight;
    
	var popupHeight = $("#lightbox").height();
	var popupWidth = $("#lightbox").width();

	var scrolltop = $(window).scrollTop();
            
    
    var box_top = (windowHeight/2)-(popupHeight/2);
    if(scrolltop>1){
        box_top+=(scrolltop);
    }
    
    $("#lightbox").css({
	"position": "absolute",
	"top":box_top, 
	"left": windowWidth/2-popupWidth/2
	});
		
	
	//$("#lightbox").center();
	//only need force for IE6
    
    
    
	$("#backgroundPopup").css({
	"height": fullHeight
	});	
	

}


$.getDocHeight = function(){
    return Math.max(
        $(document).height(),
        $(window).height(),
        /* For opera: */
        document.documentElement.clientHeight
    );
};


                 
