﻿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();
	});
});

function f_scrollTop() {
	return f_filterResults(
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}

function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}
