var openPop = "";

function loadPopup(id){
  $("#popBackg").css({
    "opacity": "0.7"
  });
  if (openPop != "") { 
    $(openPop).fadeOut("slow", function () {
      if (openPop != id) { 
        centerPopup(id);
        $(id).fadeIn("slow");
        openPop = id;
      } else { openPop = ""; }
    });
  } else {
    centerPopup(id);
//  $("#popBackg").fadeIn("slow");
    $(id).fadeIn("slow");
    openPop = id;
  }
}

function disablePopup(){
  if (openPop != "") {
    $("#popBackg").fadeOut("slow");
    $(openPop).fadeOut("slow");
  }
  openPop = "";
}

function centerPopup(id){
  var windowWidth = $(document).width();
  var windowHeight = $(document).height();
  var popupHeight = $(id).height();
  var popupWidth = $(id).width();

  if (windowHeight>900) windowHeight = 900;
  newTop = windowHeight/2-popupHeight/2;
  newLeft = windowWidth/2-popupWidth/2;

  if (newTop < 50) { newTop = 50; }

  $(id).css({
    "position": "absolute",
    "top": newTop,
    "left": newLeft
  });

  $("#popBackg").css({
    "height": windowHeight
  });
}

$(document).ready(function(){

  $(".popClose").click(function(){
    disablePopup();
  });

  $("#popBackg").click(function(){
    disablePopup();
  });

  $(document).keypress(function(e){
    if(e.keyCode==27){
      disablePopup();
    }
  });
});