$(document).ready(function() {
	if(!$.browser.msie) {
		$("a.soh").keyup(function(e) {
			if(e.keyCode==13) {
				openOverlay(this);
				$(document).keyup(function(e) {
					if (e.keyCode==27) {
						closeOverlay();
					}
				});
				return false;
			}
		});
				
		$("a.soh").click(function() {
			openOverlay(this);
			$(document).keyup(function(e) {
				if (e.keyCode==27) {
					closeOverlay();
				}
			});
			return false;
		});
	}
});

function openOverlay(e) {
	$("<div />",{
			id: "overlay",
			"class": "overlay",
			css:{opacity:0,"z-index":30000},
			click:function() { closeOverlay(); }
		}).appendTo("body").animate({opacity:.5},150,function() {
			$("<div />",{id:"overlay_top",css:{opacity:0}}).appendTo("body");
			$.ajax({
					url:'http://www.mpsportsclub.com/campaigns/summerofhealth/index.php',
					data:{
						'c':$(e).attr("href").split("?c=")[1],
						"inline":"1"
					},
					dataType:"html",
					timeout:3000,
					success:function(data,status,xhr) {
						$("#overlay_top").append(data).animate({opacity:1},150,function() {
							$("<div />",{
									"class":"overlay_exit",
									text:"X",
									css:{
										opacity:0,
										marginLeft:function() {
											return (parseInt($("#overlay_top").children("div").first().css("marginLeft"))-20)+"px";
										},
										marginTop:function() {
											return (parseInt($("#overlay_top").children("div").first().css("marginTop"))-20)+"px";
										}
									},
									click:function() { closeOverlay(); }
								}).appendTo("#overlay_top")
								.animate({opacity:1},100);
								if(!$.support.boxModel) {
									$("#soh_main [position='fixed']").attr("position","absolute");
								}
						});
					},
					error:function(xhr,status,error) {
						if(confirm("Error!\nThe website encountered the following error: " + status + "\nRedirect to http://www.mpsportsclub.com/campaigns/summerofhealth/?c<?=$c?>?")) {
							window.location("http://www.mpsportsclub.com/campaigns/summerofhealth/?c<?=$c?>");
						}
					}
			});	
		});
}

function closeOverlay() {
	$(document).unbind("keyup");
	$("#overlay_top").stop().fadeOut(300,function() {
		$(this).remove();
		$("#overlay").stop().fadeOut(300,function() {
			$(this).remove();
		});
	});
}
