﻿var popupStatus = 0;

var popupContainerID = "#PopupContainer";
var popupBackgroundID = "#PopupBackground";
var popupHeaderID = "#PopupHeader";

function loadPopup() {
	if (popupStatus == 0) {
		$(popupBackgroundID).css({
			"opacity": "0.7"
		});
		$("body").css({"overflow": "hidden"});
		$(popupBackgroundID).show();
		$(popupContainerID).show();
		popupStatus = 1;
	}
}

function disablePopup() {
	if (popupStatus == 1) {
		$(popupContainerID).hide();
		$(popupBackgroundID).hide();
		popupStatus = 0;
		$("body").css({ "overflow": "auto" });
	}
}

function centerPopup() {
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $(popupContainerID).height();
	var popupWidth = $(popupContainerID).width();
	$(popupContainerID).css({
		"position": "absolute",
		"top": ((windowHeight / 2) - (popupHeight / 2)) + $(document).scrollTop(),
		"left": windowWidth / 2 - popupWidth / 2
	});

	$(popupBackgroundID).css({
		"height": windowHeight
	});

}

$(document).ready(function() {

	$("#button").click(function() {
		centerPopup();
		loadPopup();
	});

	$("#PopupCloseButton").click(function() {
		disablePopup();
	});
	$(popupBackgroundID).click(function() {
		disablePopup();
	});

	$(document).keypress(function(e) {
		if (e.keyCode == 27 && popupStatus == 1) {
			disablePopup();
		}
	});

});