//(c) 2008 Michael Manning  http://plugins.jquery.com/project/parseQuery
jQuery.parseQuery=function(A,B){var C=(typeof A==="string"?A:window.location.search),E={f:function(F){return unescape(F).replace(/\+/g," ")}},B=(typeof A==="object"&&typeof B==="undefined")?A:B,E=jQuery.extend({},E,B),D={};jQuery.each(C.match(/^\??(.*)$/)[1].split("&"),function(F,G){G=G.split("=");G[1]=E.f(G[1]);D[G[0]]=D[G[0]]?((D[G[0]] instanceof Array)?(D[G[0]].push(G[1]),D[G[0]]):[D[G[0]],G[1]]):G[1]});return D};

$(document).ready(function(){
	//create the popup divs
	$("body").append("<div id='backgroundoverlay'></div>");
	$("body").append("<div id='popupcontent'></div>");
	//set click events
	$("a[name]=overlaypopup").click(function(event){
		event.preventDefault();
		//load popup
		loadPopup(event.currentTarget.href);
	});

	
});
var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup(htmlFile){
	//CLOSING POPUP
	//Click the x event!
	$("#popupclose").click(function(){
		disablePopup();
	});
	//Click out event!
	$("#backgroundoverlay").click(function(){
		disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});
	
	//Get width & height query
	var popupWidth = $.parseQuery(htmlFile.split("?")[1]).width ;
	var popupHeight = $.parseQuery(htmlFile.split("?")[1]).height;
	var marginString = "-"+(parseInt(popupHeight)/2) + "px 0 0 -"+parseInt(popupWidth)/2+"px";

	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundoverlay").css({
			"opacity": "0.7"
		});
		$("#popupcontent").load(htmlFile, "",function(){
			//CLOSING POPUP
			//Click the x event!
			$("#popupclose").click(function(event){
				event.preventDefault();
				disablePopup();
			});
	 	});
		$("#popupcontent").css("width",popupWidth +"px");
		$("#popupcontent").css("height",popupHeight +"px");
		$("#popupcontent").css("margin", marginString);
		
		$("#backgroundoverlay").fadeIn("fast");
		$("#popupcontent").fadeIn("fast");
		popupStatus = 1;
	}

	
}

function disablePopup(){
	if(popupStatus==1){
		$("#backgroundoverlay").fadeOut("fast");
		$("#popupcontent").fadeOut("fast");
		popupStatus = 0;
	}
}

