//0 means disabled; 1 means enabled;
var popupStatus = 0;
//loading popup with jQuery 
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus == 0){
		$(".popup_back").css({
			"opacity": "0.3"
		});
		$("#popup_content").fadeIn("slow");
		$(".popup_back").fadeIn("slow");
	}
		popupStatus = 1;
}
//disabling popup with jQuery
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus == 1){
		//document.write(popupStatus);
		$("#popup_content").fadeOut("slow");
		$(".popup_back").fadeOut("slow");
		window.location.reload();		
		popupStatus = 0;
	}
}
//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popup_content").height();
	var popupWidth = $("#popup_content").width();
	//centering
	$("#popup_content").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	$(".popup_back").css({
		"height": windowHeight+300,
		"width": windowWidth-2
	});
}
//CONTROLLING EVENTS
$(document).ready(function(){
	
	//LOADING POPUP
	//Click the button event!
	$("#button").click(function(){
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
	});
	//Click the x event
	$("#popup_close").click(function(){
		disablePopup();
	});
	//Click out event
	$(".popup_back").click(function(){
		disablePopup();
	});
	//Press Escape event
	$(document).keypress(function(e){
		if(e.keyCode == 27 && popupStatus == 1){
			disablePopup();
		}
	});
});
