/**
 * 引入toaster方法
 */
document.write("<script language=javascript src='static/js/common/toastr.js'></script>");

/**
 *  获取用户信息
 */
function getAccountInfo() {
	$.get('/account', function(data) {
		if (data.content) {
            window.loginInfo = data;
			$('.x-nologin').hide();
			$('.x-login').show();

            $('.x-login').find('.title').text(data.content.name);

            $('.title').click(function () {
				window.location.href = "/resetPassword";
            });
		}
	});
}

/**
 * 退出登录
 */
function Logout() {
    $.ajaxSetup({cache : false });
    clearAllCookie();
    var config = {
        cache: false,
        headers: {
            'Cache-Control': 'no-cache',
            'Pragma': 'no-cache',
        },
        ifModified :true ,
    };
    $.ajax('logout', {
        method: 'POST',
        async: false,
        config: config,
        success: function(data) {
            window.location.href = data.url || '/index';
        },
        error: function (res) {
            toastr.error(res.responseText);
        }
    });
}

//清除所有cookie函数
function clearAllCookie() {
    var keys = document.cookie.match(/[^ =;]+(?=\=)/g);
    if (keys) {
        for(var i = keys.length; i--;) {
            document.cookie = keys[i] + '=0;expires=' + new Date(0).toUTCString();
        }
    }
}

$(function() {
	'use strict';
	
	// 监听页面滚动
	$(window).scroll(function() {
		if($(window).scrollTop() >= 400) {
			$('#nav').addClass('on');
		} else {
			$('#nav').removeClass('on');
		}
	});
	
	// 查询登录信息
	getAccountInfo();

	$('.link-logout').click(Logout);
});