$(document).ready(function () {
	$("li#btnAuth").bind("click", function (event) {
		if ($("div#authLayer").css("display") == "none") {
			Common.cursorPos("authLayer", -15, 150, event);
		} else {
			$("div#authLayer").hide();
		}
	});

	$("li#btnAuthOut").bind("click", function () {
		Auth.setAuth("out");
	});

	$("input#authKey").bind("keydown", function (event) {
		if (event.keyCode == 13) {
			Auth.setAuth("in");
		}
	});

	$("input#btnAuthSubmit").bind("click", function () {
		Auth.setAuth("in");
	});
});

var Auth = {
	key : "out",
	level : 10,

	setAuth : function (mode) {
		if (mode == "in") {
			if ($("input#authKey").val().isempty() === false) {
				this.key = $("input#authKey").val();
				this.level = parseInt($("select#authLevel").val());
			} else {
				alert("인증키를 입력하세요.");
				$("input#authKey").focus();
				return false;
			}
		}

		$.ajax({
			type: "POST"
			, url: "/index.php"
			, data: "uri=authProc&authLevel="+Auth.level+"&authKey="+Auth.key
			, success: function (result) {
				res = jQuery.parseJSON(result);

				switch (res.result) {
					case "success":
						location.replace(location.href);
						break;

					default:
						alert("올바른 인증키가 아닙니다.");
						break;
				}
			}
		});
	}
}
