function getContextPath() {
	var pathName = document.location.pathname;
	var index = pathName.substr(1).indexOf("/");
	var result = pathName.substr(0, index + 1);
	return result;
}

jQuery(function($) {
	$(document).on('click', '.toolbar a[data-target]', function(e) {
		e.preventDefault();
		var target = $(this).data('target');
		$('.widget-box.visible').removeClass('visible');// hide others
		$(target).addClass('visible');// show target
	});

	// 登录页面切换背景图
	$('#btn-login-dark').on('click', function(e) {
		$('body').attr('class', 'login-layout');
		e.preventDefault();
	});
	$('#btn-login-light').on('click', function(e) {
		$('body').attr('class', 'login-layout light-login');
		e.preventDefault();
	});
	$('#btn-login-blur').on('click', function(e) {
		$('body').attr('class', 'login-layout blur-login');
		e.preventDefault();
	});

	// 验证找回密码表单
	$("#retrieveButton").bind("click", function() {
		$('#validationRetrieveForm').submit();
	});
	$('#validationRetrieveForm').validate({
		errorElement : 'div',
		errorClass : 'help-block',
		focusInvalid : false,
		ignore : "",
		rules : {
			email : {
				required : true,
				email : true
			}
		},
		messages : {
			email : {
				required : "请填写邮箱",
				email : "请填写正确的邮箱"
			}
		},
		highlight : function(e) {
			$(e).closest('label').removeClass('has-info').addClass('has-error');
		},
		success : function(e) {
			$(e).closest('label').removeClass('has-error');// .addClass('has-info');
			$(e).remove();
		},
		errorPlacement : function(error, element) {
			error.insertAfter(element.parent());
		},
		submitHandler : function(form) {
			$.ajax({
				dataType : "json",
				url : getContextPath() + "/sys/sysuser/retrievePassword",
				type : "post",
				data : {
					email : $('#retrieveEmail').val()
				},
				complete : function(xmlRequest) {
					var returninfo = eval("(" + xmlRequest.responseText + ")");
					if (returninfo.result == 1) {
						$("#retrieveTip").html("找回密码邮件已经发到你的邮箱");
					} else if (returninfo.result == -1) {
						$("#retrieveTip").html("密保邮箱不存在，请重新输入");
					}
				}
			});
		},
		invalidHandler : function(form) {
		}
	});

	// 按回车键触发登录事件
	$(document).keydown(function(event) {
		var key = window.event ? event.keyCode : event.which;
		if (key == 13) {
			$('#validationLoginForm').submit();
		}
	});

	// 验证登录表单
	$("#loginButton").bind("click", function() {
		$('#validationLoginForm').submit();
	});
	$('#validationLoginForm').validate({
		errorElement : 'div',
		errorClass : 'help-block',
		focusInvalid : false,
		ignore : "",
		rules : {
			userName : {
				required : true,
			},
			password : {
				required : true,
			}
		},
		messages : {
			userName : {
				required : "请填写用户名",
			},
			password : {
				required : "请输入密码",
			}
		},
		highlight : function(e) {
			$(e).closest('label').removeClass('has-info').addClass('has-error');
		},
		success : function(e) {
			$(e).closest('label').removeClass('has-error');// .addClass('has-info');
			$(e).remove();
		},
		errorPlacement : function(error, element) {
			error.insertAfter(element.parent());
			//error.appendTo('#invalid');
		},
		submitHandler : function(form) {
			$.ajax({
				dataType : "json",
				url : getContextPath() + "/sys/sysuser/login",
				type : "post",
				data : {
					userName : $('#userName').val(),
					password : $('#password').val(),
					rememberMe : $("input[name='rememberMe']:checked").val()
				},
				complete : function(xmlRequest) {
					var returninfo = eval("(" + xmlRequest.responseText + ")");
					if (returninfo.result == 1) {
						document.location.href = getContextPath() + "/sys/sysuser/home";
					} else if (returninfo.result == -1) {
						$("#loginTip").html("用户名有误或已被禁用");
					} else if (returninfo.result == -2) {
						$("#loginTip").html("密码错误");
					} else {
						$("#loginTip").html("服务器错误");
					}
				}
			});
		},
		invalidHandler : function(form) {
		}
	});

	$('[data-rel=tooltip]').tooltip({
		container : 'body'
	});

});
