var popupStatus = 0;

function loadPopup(popupId) {
    if (popupStatus == 0) {
	    $("#fade").fadeIn("fast");
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("fast");
		$("#" + popupId).fadeIn("fast");
		popupStatus = 1;

		if (popupId == "popupRegister")
		    document.getElementById("reg_first_name").focus();
	}
}

function disablePopup(popupId){
    if(popupStatus == 1){
        $("#fade").fadeOut("fast");
	    $("#backgroundPopup").fadeOut("fast");
	    $("#" + popupId).fadeOut("fast");
	    popupStatus = 0;
	}
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupRegister").height();
	var popupWidth = $("#popupRegister").width();
	//centering
	$("#popupRegister").css({
		"position": "absolute",
		//"top": windowHeight/2-popupHeight/2,
		//"left": windowWidth/2-popupWidth/2
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
}


//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
	
	//LOADING POPUP
	//Click the button event!
	$("#button").click(function(){
		//centering with css
		centerPopup();
		//load popup
		loadPopup("popupRegister");
	});
				
	//LOADING POPUP
	//Click the button event!
	$("#button2").click(function(){
		//centering with css
		centerPopup();
		//load popup
		loadPopup("popupRegister");
	});
				
	//CLOSING POPUP
	//Click the x event!
	$("#popupContactClose").click(function(){
		disablePopup("popupRegister");
	});
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup("popupRegister");
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup("popupRegister");
		}
	});
});
