function openDialog(html) {
    $("#hidden-body").html(html);
    $("#overlay").css({opacity: 0.0}).show().animate({opacity: 0.6},500);
    setTimeout(function() {
	var h = $("#hidden-body").outerHeight()+40;
	$("#dialog .overlay-dialog").css("width", $("#hidden-body").width()+"px");
	$("#dialog .overlay-dialog").css("height", h+"px");
	$("#dialog .overlay-dialog").css("margin",  -1*((h+(8+6)*2)/2)+"px auto 0 auto");
	$("#dialog .body").html(html);
	$("#dialog").show();
	// $("#dialog").css({opacity: 0.0}).show().animate({opacity: 1.0},500);
    }, 300);
}

function closeDialog() {
    $("#overlay").animate({opacity: "hide"},500);
    $("#dialog").animate({opacity: "hide"},500);
}

$(".overlay-dialog").click(function(e) {
    e.preventDefault();
    e.stopPropagation();
});

function openLoginbox() {
    $("#overlay").css({opacity: 0.0}).show().animate({opacity: 0.8},500);
    $("#loginbox").css({opacity: 0.0}).show().animate({opacity: 1.0},500);
    $(".cannot-overlay").css("visibility","hidden");
    setTimeout(function() {
        $("#loginbox-login").focus();
    }, 600);
}

function closeLoginbox() {
    requireLogin_callback = undefined;
    $("#overlay").animate({opacity: "hide"},500);
    $("#loginbox").animate({opacity: "hide"},500);
    $(".cannot-overlay").css("visibility", "visible");
}

function submitLoginbox(form) {
    $.ajax({
        type: "PUT",
        url: form.action,
        data: $(form).serialize(),
        error: function(xhr, textStatus, errorThrown) {
            eval("var result = "+xhr.responseText);
            $("#loginbox").effect("shake",500);
            $("#loginbox .messages .errors").text(result.messages.join(""));
            setTimeout(function() {
                $("#loginbox-login").focus();
            }, 600);
        },
        success: function(data, dataType) {
            if(requireLogin_callback) {
		eval("var result = "+data);
		after_login();
		
                var func = requireLogin_callback;
                requireLogin_callback = undefined;
		closeLoginbox();
                func();
            }
            else {
                location.reload();
            }
        }
    });
}

var requireLogin_callback = undefined;
function requireLogin(func) {
    requireLogin_callback = undefined;
    if(current_user==undefined) {
        requireLogin_callback = func;
        openLoginbox();
        return false;
    }
    if(func) func();
    return true;
}