$popupBg = "";
currentlyOpened = "";
$(document).ready(function(){
 $popupBg = $("#popupOpenedBackground");
 currentlyOpened = $popupBg; //set a default to prevent errors
 $('#checkAll').click(function(){//check all checkboxes
  $("INPUT[type='checkbox']").attr('checked', $('#checkAll').is(':checked'));
 });
 $('body').delegate('.openPopup', 'click', function(event){
  closePopup();
  currentlyOpened = $('#' + $(this).attr('data-popup'));
  resizePopup();
  $popupBg.fadeIn('fast');
  currentlyOpened.fadeIn('fast');
 });
 $('body').delegate('.popupBox .closeX', 'click', function(event){
  closePopup();
 });
 $('body').delegate('#popupOpenedBackground', 'click', function(event){
  closePopup();
 });
 function resizePopup(){
  var currentChild = currentlyOpened.children(":first");
  currentlyOpened.css({
   "width": "auto",
   "height": "auto"
  });
  currentChild.css({
   "width": "auto",
   "height": "auto"
  });
  var currentWindowHeight = $(window).height() - 100,
   currentWindowWidth = $(window).width() - 100,
   getFullWidth = currentlyOpened.width(),
   getFullHeight = currentlyOpened.height(),
   getWidth = getFullWidth <= currentWindowWidth ? getFullWidth : currentWindowWidth,
   getHeight = getFullHeight <= currentWindowHeight ? getFullHeight : currentWindowHeight;
  currentlyOpened.css({
   "marginLeft": "-" + getWidth / 2 + "px",
   "marginTop": "-" + getHeight / 2 + "px",
   "left": "50%",
   "top": "50%",
   "position": "fixed",
   "width": getWidth + "px",
   "height": getHeight + "px"
  });
  currentChild.css({
   "width": getWidth - 10 + "px",
   "height": getHeight - 10 + "px",
   "overflow": "auto"
  });

 }
});
function closePopup(){
 currentlyOpened.fadeOut('fast');
 $popupBg.fadeOut('fast');
}
