var statuses = new Array(); //var popupStatus = 0; //0 means disabled; 1 means enabled;

//loading popup with jQuery
function loadPopup(popupId, backgroundId) {
//  if (statuses[popupId] == 0) {
  centerPopup(popupId, backgroundId);
  $("#" + backgroundId).css({"opacity": "0.7"});
  $("#" + backgroundId).fadeIn("fast");
  $("#" + popupId).fadeIn("fast");
//    statuses[popupId] = 1;
//  }
}

//disabling popup with jQuery
function disablePopup(popupId, backgroundId) {
//  if (statuses[popupId] == 1) {
  $("#" + backgroundId).fadeOut("fast");
  $("#" + popupId).fadeOut("fast");
//    statuses[popupId] = 0;
//  }
}

//centering popup
function centerPopup(popupId, backgroundId) {
  var windowWidth = getClientWidth();
  var windowHeight = getClientHeight();
  var popupHeight = $("#" + popupId).height();
  var popupWidth = $("#" + popupId).width();

  var dHeight = windowHeight - popupHeight;
  var dWidth = windowWidth - popupWidth;

  var scrollY = getPageYScroll();
  var scrollX = getPageXScroll();

//  console.log(dHeight);
//  console.log(dWidth);
//  console.log(scrollY);
//  console.log(scrollX);

  $("#" + popupId).css({
    "position": "absolute",
    "top":  dHeight / 2 + scrollY,
    "left": dWidth / 2 + scrollX
  });
  $("#" + backgroundId).css({
    "height": windowHeight  //only need force for IE6
  });
}

function getClientWidth() {
  var width = 0;
  if (document.documentElement && document.documentElement.clientWidth) {
    width = parseInt(document.documentElement.clientWidth);
  }
  else if (window.innerWidth) {
    width = parseInt(window.innerWidth);
  }
  else if (document.body.clientWidth) {
    width = parseInt(document.body.clientWidth);
  }
  return width;
}
function getClientHeight() {
  var height = 0;
  if (document.documentElement && document.documentElement.clientHeight) {
    height = parseInt(document.documentElement.clientHeight);
  }
  else if (window.innerHeight) {
    height = parseInt(window.innerHeight);
  }
  else if (document.body.clientHeight) {
    height = parseInt(document.body.clientHeight);
  }
  return height
}


function getPageXScroll() {
  var xScroll = -1;
  if (window.self.pageXOffset) {
    xScroll = parseInt(window.self.pageXOffset);
  } else if (document.documentElement && document.documentElement.scrollLeft) {
    xScroll = parseInt(document.documentElement.scrollLeft);
  } else if (document.body) {
    xScroll = parseInt(document.body.scrollLeft);
  }
  return xScroll;
}
function getPageYScroll() {
  var yScroll = -1;
  if (window.self.pageYOffset) {
    yScroll = parseInt(window.self.pageYOffset);
  } else if (document.documentElement && document.documentElement.scrollTop) {
    yScroll = parseInt(document.documentElement.scrollTop);
  } else if (document.body) {
    yScroll = parseInt(document.body.scrollTop);
  }
  return yScroll;
}
