﻿//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;
var popdiv = '';
//loading popup with jQuery magic!
function loadPopup(popupDiv) {
    if (popdiv != '') disablePopup();
    centerPopup(popupDiv);
    $("#backgroundPopup").css({
        "height": windowHeight
    });
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#" + popupDiv).fadeIn("slow");
		popupStatus = 1;
		popdiv = popupDiv;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#" + popdiv).fadeOut("slow");
		popupStatus = 0;
		popdiv = '';
	}
}

//centering popup
function centerPopup(popupDiv) {
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#" + popupDiv).height();
	var popupWidth = $("#" + popupDiv).width();
	//centering
	$("#" + popupDiv).css({
	    "position": "absolute",
	    "top": windowHeight / 2 - popupHeight / 2 + document.documentElement.scrollTop,
	    "left": windowWidth / 2 - popupWidth / 2 + document.documentElement.scrollLeft
	});
}

$(document).ready(function() {
    //Scroll!
    $(window).scroll(function(e) {
        var windowWidth = document.documentElement.clientWidth;
        var windowHeight = document.documentElement.clientHeight;
        if (popdiv != '') {
            //request data for centering
            var popupHeight = $("#" + popdiv).height();
            var popupWidth = $("#" + popdiv).width();
            //centering
            $("#" + popdiv).css({
                "position": "absolute",
                "top": windowHeight / 2 - popupHeight / 2 + document.documentElement.scrollTop,
                "left": windowWidth / 2 - popupWidth / 2 + document.documentElement.scrollLeft
            });
        }
    });
});