function anchorClick(event) {
	event.preventDefault();
	var url = $(this).attr('href');
	if( $("#backdrop").length == 0 )
	{
		$('body').append('<div id="backdrop"></div>');
		$('#backdrop').css('display', 'none').css('position', 'absolute').css('top', $(window).scrollTop()+'px').css('left', '0px').css('background', '#000000').css('zIndex', '600').css('width', '100%').css('height', '100%').css('opacity', '.9').css('filter', 'alpha(opacity=90)');
		$('#backdrop').fadeIn(500, function () {
			$('body').append('<div id="frame"></div>');
			$('#frame').css('display', 'none').css('background', '#FFFFFF').css('position', 'absolute').css('width', '85%').css('height', '95%').css('left', ($(window).width()*.5)-($('#frame').width()*.5)+'px').css('top', ($(window).scrollTop()+($(window).height()*.5)-($('#frame').height()*.5)) + "px").css('overflow', 'auto').css('zIndex', '601').css('opacity', '1').css('filter', 'alpha(opacity=100)');
			$.ajax({
				type: "GET",
				url: url,
				cache: false,
				error: function(e) { alert("Failure: " + e); },
				success: function(html){
					$("#frame").append(html).find("a, :submit").addClass("popup");
					$(':submit.popup').click(formClick);
					$('a.popup').click(anchorClick);
					$("#frame").fadeIn(500);
				}
			});
		});
	}
	else
	{
		$('#frame').fadeOut(500, function () {
			$(this).remove();
			$('body').append('<div id="frame"></div>');
			$('#frame').css('display', 'none').css('background', '#FFFFFF').css('position', 'absolute').css('width', '85%').css('height', '95%').css('left', ($(window).width()*.5)-($('#frame').width()*.5)+'px').css('top', ($(window).scrollTop()+($(window).height()*.5)-($('#frame').height()*.5)) + "px").css('overflow', 'auto').css('zIndex', '601').css('opacity', '1').css('filter', 'alpha(opacity=100)');
			$.ajax({
				type: "GET",
				url: url,
				cache: false,
				error: function(e) { alert("Failure: " + e); },
				success: function(html){
					$("#frame").append(html).find("a, :submit").addClass("popup");;
					$(':submit.popup').click(formClick);
					$('a.popup').click(anchorClick);
					$('#frame').fadeIn(500);
				}
			});
		});
	}
	$('#backdrop').click(function () {
		$('#frame').fadeOut(500, function () {
			$(this).remove();
			$('#backdrop').fadeOut(500, function () {
				$(this).remove();
			});
		});
	});
}

function formClick(event) {
	event.preventDefault();
	var form = $(this).parents('form:first');
	var url = form.attr('action');
	var postString = "";
	form.find(":input").each(function() {
		postString = postString+$(this).attr("name")+"="+$(this).val()+"&";
	});
	postString = postString.substr(0,postString.length-1);
	
	if( $("#backdrop").length == 0 )
	{
		$('body').append('<div id="backdrop"></div>');
		$('#backdrop').css('display', 'none').css('position', 'absolute').css('top', $(window).scrollTop()+'px').css('left', '0px').css('background', '#000000').css('zIndex', '600').css('width', '100%').css('height', '100%').css('opacity', '.9').css('filter', 'alpha(opacity=90)');
		$('#backdrop').fadeIn(500, function () {
			$('body').append('<div id="frame"></div>');
			$('#frame').css('display', 'none').css('background', '#FFFFFF').css('position', 'absolute').css('width', '85%').css('height', '95%').css('left', ($(window).width()*.5)-($('#frame').width()*.5)+'px').css('top', ($(window).scrollTop()+($(window).height()*.5)-($('#frame').height()*.5)) + "px").css('overflow', 'auto').css('zIndex', '601').css('opacity', '1').css('filter', 'alpha(opacity=100)');
			$.ajax({
				type: "POST",
				url: url,
   				data: postString,
				cache: false,
				error: function(e) { alert("Failure: " + e); },
				success: function(html){
					$("#frame").append(html).find("a, :submit").addClass("popup");
					$(':submit.popup').click(formClick);
					$('a.popup').click(anchorClick);
					$("#frame").fadeIn(500);
				}
			});
		});
	}
	else
	{
		$('#frame').fadeOut(500, function () {
			$(this).remove();
			$('body').append('<div id="frame"></div>');
			$('#frame').css('display', 'none').css('background', '#FFFFFF').css('position', 'absolute').css('width', '85%').css('height', '95%').css('left', ($(window).width()*.5)-($('#frame').width()*.5)+'px').css('top', ($(window).scrollTop()+($(window).height()*.5)-($('#frame').height()*.5)) + "px").css('overflow', 'auto').css('zIndex', '601').css('opacity', '1').css('filter', 'alpha(opacity=100)');
			$.ajax({
				type: "POST",
				url: url,
   				data: postString,
				cache: false,
				error: function(e) { alert("Failure: " + e); },
				success: function(html){
					$("#frame").append(html).find("a, :submit").addClass("popup");
					$(':submit.popup').click(formClick);
					$('a.popup').click(anchorClick);
					$("#frame").fadeIn(500);
				}
			});
		});
	}
	$('#backdrop').click(function () {
		$('#frame').fadeOut(500, function () {
			$(this).remove();
			$('#backdrop').fadeOut(500, function () {
				$(this).remove();
			});
		});
	});
}
	
$(document).ready(function() {
	$(':submit.popup').click(formClick);
	$('a.popup').click(anchorClick);

	$(window).scroll(function() {
		$("#backdrop").css("top", $(window).scrollTop() + "px");
		$("#frame").css("top", ($(window).scrollTop()+($(window).height()*.5)-($('#frame').height()*.5)) + "px");
	});
	
});
