﻿var popupStatus = 0;

function loadPopup() {
    if (popupStatus == 0) {
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("fast");
		$("#popupContainer").fadeIn("normal");
		popupStatus = 1;
	}
}

function disablePopup() {
    if (popupStatus == 1) {
        $("#popupContainer").fadeOut("fast");
        $("#backgroundPopup").fadeOut("normal");
		popupStatus = 0;
	}
}

function centerPopup() {
    var windowHeight = document.documentElement.clientHeight;
    var windowWidth = document.documentElement.clientWidth;
    var popupHeight = $("#popupContainer").height();
    var popupWidth = $("#popupContainer").width();
    $("#popupContainer").css({
        "position": "absolute",
        "top": ($(document).scrollTop() + ((windowHeight / 2) - (popupHeight / 2) - 100)),
        "left": ((windowWidth / 2) - (popupWidth / 2))
    });
    $("#backgroundPopup").css({
        "height": $(document).height()
    });
}

function openAlert(heading, contents) {
    $("#popupContent").html('<h3>' + heading + '</h3><p>' + contents + '</p>');
    centerPopup();
    loadPopup();
}

$(document).ready(function() {
    $("#backgroundPopup").click(function() { disablePopup(); });
    $(document).keypress(function(e) {
        if (e.keyCode == 27 && popupStatus == 1)
            disablePopup();
    });
});