$(function () {
    loadCommon('', null, function() {
        GetLogonService.checkLogin(afterCheckLogin);
    });
});

function uiLogin() {
    showLogonStatus(true, '正在登录……');
    GetLogonService.login(showUser);
}

function uiLogout() {
    showLogonStatus(true, '正在注销……');
    GetLogonService.logout(showUser);
}

function showLogonStatus(doShow, caption) {
    try {
        var logonStatus = $('#logonStatus');
        var logonStatusCaption = $('#logonStatusCaption');

        if (doShow) {
            logonStatusCaption.text(caption);
            logonStatus.css('display', 'block');
        } else
            logonStatus.css('display', 'none');
    } catch (e) {
        alert('错误(showLogonStatus)：' + e.message);
    }
}

function afterCheckLogin(data) {
    showUser(data);

    GetFileService.getFileClassList('', showFileClassList);
}

function showUser(userId) {
    try {
        var logonArea = $('#logonArea');
        var logonUserArea = $('#logonUserArea');

        if (Ext.isEmpty(userId) || (Ext.type(userId) != 'string')) {
            logonArea.css('display', 'block');
            logonUserArea.css('display', 'none');
            $('#password').attr('value', '');
        } else {
            logonArea.css('display', 'none');
            logonUserArea.css('display', 'block');
            $('#logonUserName').text(userId);
        }
    } catch (e) {
        alert('错误(showUser)：' + e.message);
    } finally {
        showLogonStatus(false, null);
    }
}

function showFileClassList(data) {
    buildDownloadMenu(data, 'download/');

    GetFileService.getFileListBrief('', showFileListBrief);
}

function showFileListBrief(data) {
    try {
        var files = Ext.decode(data);

        var fileListBrief = $('#fileListBrief');
        fileListBrief.empty();

        if (files == null)
            return;

        for (var i = 0; i < files.length; i ++) {
            var file = files[i];
            fileListBrief.append('<tr><td class="liststyle2"><img src="image/icon_newdownload.gif">&nbsp;' +
                                 '<a href="download/download_detail.html?softId=' + file["SoftID"] + '">' +
                                 file["SoftName"] + '</a></td></tr>');
        }
        fileListBrief.append('<tr><td class="listmorestyle1"><A href="/download/download_center.html">' +
                             '<img src="image/btn_more.gif"></a></td></tr>');
    } catch(e) {
        alert("错误(showFileListBrief)：" + e.message);
    }
}
