How to detect click outside of my popup window??

How to detect click outside of my popup window??

Hi all
   I have been working on this problem all day. I have several pictures on a page. When a user clicks it, a popup window will show up a series of pictures. Inside the popup window, there is a close icon to turn of the popup. Besides, if the user clicks outside of the popup, the popup will turn off as well. Here is what I have so far.The closed icon inside the popup works fine but I can't get "the click outside of the popup" to work. I would really appreciate it if someone would help me about it. Thanks a lot!

My code:

$(document).ready(function(){  
$(".picture ").click(function(){     //when pictures are clicked 
centerPopup();   // center popup
loadPopup();  / / popup jump out
  
$(document).click(function(e){   //try to turn off popup
var click=$(e.target);
var outsideDiv=("#popup").parents();
if(click.is(outsideDiv) || click.is("#close img")){
disablePopup();   //turn off popup   #close img is the icon inside popup  
);
 });
   });
});