/*
 * 登录控制
 * 
 */

var isLogin = "false";// 用户是否已经登录

$.getJSON("/checkUserSession.jhtml",{}, function(json) {
	isLogin = json.status;
	if (json.status == 'false') {
		$('#und_platform_header_session').replaceWith("<a class='topnav_font' href='/user/signup.jhtml' target='_blank'>免费注册</a> <a class='login' href='#'>登录</a> <a  href='/user/forget_password_first.jhtml'>忘记密码</a> ");
		$(".login").click(function() {
			$("#errorMsg").replaceWith("<div id='errorMsg'></div>");
			$('#loginDialog').dialog('open');
		});
	} else {
		$('#und_platform_header_session').replaceWith("您好，" + json.userName + "　<a href='/logout.jhtml' class='topnav_font'>安全退出</a> ");
	}
});


function checkIfLogin() {
	if (isLogin == 'true') {
		top.location.href = "/mbs/enter.jhtml";
	} else {
		$().ready(function() {
			$("#errorMsg").replaceWith("<div id='errorMsg'></div>");
			$('#loginDialog').dialog('open');
			$('#j_url').val("/mbs/enter.jhtml");
		});
	}
}

$().ready(function() {
	var options = {
		beforeSubmit : showRequest, // pre-submit callback
		success : showResponse,
		 dataType: 'json'
	};
	$('#loginForm').submit(function() {
		$('#loginForm').ajaxSubmit(options);
	});

	$("#loginDialog").dialog( {
		hide:false,
		bgiframe : false,
		resizable : false,
		autoOpen : false,
		// position: 'top',
		height : 270,
		width : 250,
		modal : true,
		open :function(){
			$("#errorMsg").empty();
		},
		buttons : {
			"登录" : function() {
				var bValid = true;
				if (bValid) {
					$('#loginForm').submit();
				}
			}
		}
	});

	function showRequest(formData, jqForm, options) {
		return true;
	}
	;
	function showResponse(responseText, statusText) {
		if (responseText.status == "true") {
			$('#loginDialog').dialog('close');
			window.location.href = responseText.url;
		} else {
			$("#errorMsg").html(responseText.msg);
		}
	}
	;
	$(document).keydown(function(event) {
		if (event.keyCode == '13') {
			$('#loginForm').submit();
		}
	});
});

/*
 * 查看收藏
 */
function favorite(){
	if (isLogin == 'true') {
		top.window.location='/mymember/index.jhtml';
	} else {
		$().ready(function() {
			$("#errorMsg").empty();
			$('#loginDialog').dialog('open');
			$('#j_url').val("/mymember/index.jhtml");
		});
	}
}
