var activePopup = null; 
function loadPopup(p) {
  if (activePopup == null) {
    $("#popupBackground").css( {
        "opacity": "0.7"
        });
    $("#popupBackground").fadeIn("slow");
    $(p).slideToggle("slow");
    activePopup = p; 
  }
}

function disablePopup(p) {
  if (activePopup == p) {
    $("#popupBackground").fadeOut("slow");
		$(p).fadeOut("slow");
    activePopup = null;
  }
}

function centerPopup(p){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $(p).height()/2;
	var popupWidth = $(p).width();
	//centering
	$(p).css({
		"position": "absolute",
    "top": (windowHeight/2-popupHeight/2)/2,
		"left": windowWidth/2-popupWidth/2,
    //"height": Math.min(document.documentElement.clientHeight/1.5, $(this).height()),
    "overflow": "auto"
	});
	//only need force for IE6
	$("#backgroundPopup").css({
		"height": windowHeight
	});
}

$(document).ready(function(){
    $("div.modbox").hide();
    $("div.modbox").css({
position: "fixed",
_position: "fixed",
//height: Math.min(document.documentElement.clientHeight/1.5, $(this).height()),
  overflow: "auto"
});
    $("a[href='#pagewrapper']").text("Close me");
	//LOADING POPUP
	//Click the button event!
  var p = "#megaseeds_anc";
	$("a.modbox").click(function(){
    p = $(this).attr("href");
		//centering with css
		centerPopup(p);
		//load popup
		loadPopup(p);
	});
	//CLOSING POPUP
	$("a[href='#pagewrapper']").click(function(){
		disablePopup(p);
	});
	//Click out event!
	$("#popupBackground").click(function(){
		disablePopup(p);
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus[p]==1){
			disablePopup(p);
		}
	});
});
