var popupStatus = 0;

function loadPopup(pid){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({ "opacity": "0.7" });
		$("#backgroundPopup").fadeIn("slow");
		$(pid).fadeIn("slow");
		popupStatus = 1;
	}
}

function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popup").fadeOut("slow");
		$("#popupPeriod").fadeOut("slow");
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(pid){
	//request data for centering
	var topOffset = window.pageYOffset || document.documentElement.scrollTop || 0;
	
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $(pid).height();
	var popupWidth = $(pid).width();
	//centering
	$(pid).css({
		"position": "absolute",
		"top": topOffset + windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6	
	$("#backgroundPopup").css({ "height": windowHeight });
}

$(document).ready(function(){
	
	$("a.plan-chooser").click(function(){
		$("#ifr").attr('src', 'vpnwizzard/start.php');
		centerPopup("#popup");
		loadPopup("#popup");
		
		return false;
	});
	
	$("div.usa a").click(function(){ return OpenPopup(1); });
	$("div.uk a").click(function(){ return OpenPopup(2); });
	$("div.can a").click(function(){ return OpenPopup(7); });
	$("div.ger a").click(function(){ return OpenPopup(3); });
	$("div.hk a").click(function(){ return OpenPopup(8); });
	$("div.usauk a").click(function(){ return OpenPopup(5); });
	$("div.ukopen a").click(function(){ return OpenPopup(6); });
	
	//CLOSING POPUP
	//Click the x event!
	$(".popupClose").click(function(){
		disablePopup();
	});
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});
});

function OpenPopup(plan) {
  $("#ifr").attr('src', 'vpnwizzard/select_period.php?server=' + plan);
		centerPopup("#popup");
		loadPopup("#popup");
		return false;
}
