﻿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();
	var top = ($(window).scrollTop() + ((windowHeight / 2) - (popupHeight / 2) - 100));
	if (top < $(window).scrollTop())
		top = $(window).scrollTop();
	$("#popupContainer").css({
		"position": "absolute",
		"top": top,
		"left": ((windowWidth / 2) - (popupWidth / 2))
	});
	$("#backgroundPopup").css({
		"height": $(document).height()
	});
}

function openAlert(heading, contents) {
    $("#popupContent").html('<h3>' + heading + '</h3><p>' + contents + '</p>');
    centerPopup();
    loadPopup();
   }

function openAlertSimple(contents) {
   	$("#popupContent").html(contents);
   	centerPopup();
   	loadPopup();
}


$(document).ready(function() {
    $("#backgroundPopup").click(function() { disablePopup(); });
    $(document).keypress(function(e) {
        if (e.keyCode == 27 && popupStatus == 1)
            disablePopup();
    });
});
