//POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

function getDocHeight() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}

//loading popup
function loadPopup(script,width,height){

	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var docHeight = getDocHeight();
	
	var popupHeight = height;
	var popupWidth = width;

	//x & y origin
	$("#popupLayer").css({
		"position": "fixed",
		"top": (windowHeight-popupHeight)/2 + "px",
		"left": (windowWidth-popupWidth)/2 + "px",
		"width": popupWidth + "px",
		"height": popupHeight + "px",
		"z-index": "99",
		"background": "none"
	});
	
	$("#backgroundPopup").css({
		"position": "absolute",
		"width": "100%",
		"height": docHeight + "px",
		"background-color": "#ffffff",
		"opacity": "0.9",
		"z-index": "98"
	});
	
	if (script) {
		$("#popupLayer").load(script);
	}	
	$("#backgroundPopup").fadeIn("slow", function() {
		$("#popupLayer").fadeIn("slow");											  
	});
	


	popupStatus = 1;
	
//	alert(	'W width: ' + windowWidth + '\n' +
//		  	'W height: ' + windowHeight + '\n' +
//			'P width: ' + popupWidth + '\n' +
//			'P height: ' + popupHeight + '\n'
//		  );

}


//disabling popup
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupLayer").fadeOut("slow", function() {
			$("#popupLayer").html("");	
		});
		popupStatus = 0;
	}
}

function closeWindow() {
		$("#popupLayer").fadeOut(1, function() {
			$("#backgroundPopup").fadeOut("slow");
			$("#popupLayer").html("");
		});
		popupStatus = 0;
}

//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){


	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});

});
