var popupStatus = 0;
var popupClicked = "popupClicked"; // cookie name
function loadPopup(){
	if (popupStatus==0){
		$("#backgroundPopup").css({"opacity": "0.7"});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupContact").fadeIn("slow");
		popupStatus=1;
	}
}
function disablePopup(){
	if (popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupContact").fadeOut("slow");
		popupStatus=0;
		createCookie(popupClicked, "clicked");
	}
}
function centerPopup(){
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupWidth = $("#popupContact").width();
	var popupHeight = $("#popupContact").height();
	$("#popupContact").css({"position":"absolute","top": windowHeight/2-popupHeight/2, "left": windowWidth/2-popupWidth/2});
	$("#backgroundPopup").css({"height": windowHeight});
}
function autoLoadPopup(){
    if(readCookie(popupClicked) != null)
        return;
    centerPopup();
    loadPopup();
}

function createCookie(name,value) {
	var expires = "; expires=Fri, 17 Dec 2020 10:00:00 GMT";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

$(document).ready(function(){
	$("#popupContactClose").click(function(){
		disablePopup();
	});
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});
});