﻿var popupStatus = 0;

function loadPopup(){
	if(popupStatus==0){
		$("#backgroundpopup").css({
			"opacity": "0.5"
		});
		$("#backgroundpopup").fadeIn("slow");
		$("#popupwindow").fadeIn("slow");
		popupStatus = 1;
	}
}

function disablePopup(){
	if(popupStatus==1){
		$("#backgroundpopup").fadeOut("slow");
		$("#popupwindow").fadeOut("slow");
		popupStatus = 0;
	}
}

function centerPopup(){
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupwindow").height();
	var popupWidth = $("#popupwindow").width();
	$("#popupwindow").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/1.2,
		"left": windowWidth/2-popupWidth/2
	});
	
	$("#backgroundpopup").css({
		"height": windowHeight
	});
	
}

$(document).ready(function(){
	
	$("#button").click(function(){
		centerPopup();
		loadPopup();
	});
				
	$("#popupwindowclose").click(function(){
		disablePopup();
	});
	$("#backgroundpopup").click(function(){
		disablePopup();
	});
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});

});
