var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup(nomCapa){
	//loads popup only if it is disabled
	nomCapa = "#"+nomCapa;
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$(nomCapa).fadeIn("slow");
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(nomCapa){
	//disables popup only if it is enabled
	nomCapa = "#"+nomCapa;
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$(nomCapa).fadeOut("slow");
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(nomCapa){
	//request data for centering
	nomCapa = "#"+nomCapa;
	var windowWidth = document.documentElement.clientWidth-300;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $(nomCapa).height();
	var popupWidth = $(nomCapa).width();

	//centering
	$(nomCapa).css({
		"position": "absolute",
		"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!
	$("#buttonExecutive").click(function(){
		//centering with css
		centerPopup("popupExecutive");
		//load popup
		loadPopup("popupExecutive");
	});
	
	$("#buttonPremium").click(function(){
		//centering with css
		centerPopup("popupPremium");
		//load popup
		loadPopup("popupPremium");
	});
	
	$("#buttonBusiness").click(function(){
		//centering with css
		centerPopup("popupBusiness");
		//load popup
		loadPopup("popupBusiness");
	});
	
	$("#buttonMaster").click(function(){
		//centering with css
		centerPopup("popupMaster");
		//load popup
		loadPopup("popupMaster");
	});
	
	//CLOSING POPUP
	//Click the x event!
	$("#popupExecutiveClose").click(function(){
		disablePopup("popupExecutive");
	});
	
	$("#popupPremiumClose").click(function(){
		disablePopup("popupPremium");
	});
	
	$("#popupBusinessClose").click(function(){
		disablePopup("popupBusiness");
	});
	
	$("#popupMasterClose").click(function(){
		disablePopup("popupMaster");
	});
	
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});

});
