//initialize popup status
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//load popup with jQuery
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({"opacity": "0.4"});
		$("#backgroundPopup").fadeIn("fast");
		$("#popup").fadeIn("fast");
		popupStatus = 1;
	}
}

//disable popup with jQuery
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("fast");
		$("#popup").fadeOut("fast");
		popupStatus = 0;
		$("#popupContent").html("Loading...");
	}
}

//center popup
function centerPopup(h,w){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = h+18;
	var popupWidth = w;
	//centering
	$("#popup").css({"position": "absolute",
		"top": document.body.scrollTop + document.documentElement.scrollTop,
		"left": 350-popupWidth/2,
		"width" : w,
		"height" : h+18
	});
	//only need force for IE6
	$("#backgroundPopup").css({"height": windowHeight});
}

//close popup on Escape event
$(document).keypress(function(e){
	if(e.keyCode==27&popupStatus==1){
		disablePopup();
	}
});


//activate popup
function showPopup(h,w,u) {
	$.ajax({
		  url: u,
		  cache: false,
		  success: function(html){
		    $("#popupContent").html(html);
		  }
		});
	centerPopup(h,w);
	loadPopup();
}
	


