﻿$(document).ready(function() {
    InitAddFriend();
});

function InitAddFriend() {
    //添加关注
    $('.btnAddFriend').unbind().hover(function() {
        if ($(this).attr('class').indexOf('messagebutton') == -1 && $(this).attr('class').indexOf('addfriend-button') == -1)
            $(this).addClass('over');
    }, function() {
        if ($(this).attr('class').indexOf('messagebutton') == -1 && $(this).attr('class').indexOf('addfriend-button') == -1)
            $(this).removeClass('over');
    }).click(function() {
        if (userID <= 0) {
            ShowLogin();
            return;
        }
        var friendUserID = $(this).attr('userid');
        if (userID == parseInt(friendUserID)) {
            AlertTips('系统提示：', '不能关注自己！');
            return;
        }
        $.ajax({
            type: "POST",
            url: userControllerUrl + '/GetFriendStateAjax/',
            data: { 'friendUserID': friendUserID },
            success: function(data) {
                if (data == 1) {
                    AlertTips('系统提示：', '你们已经彼此关注了！');
                } else if (data == 2) {
                    AlertTips('系统提示：', '你已经在关注他（她）了！');
                } else {
                    if ($('#divAddFriend').length == 0) {
                        ShowLoading();
                        $.ajax({
                            type: "POST",
                            url: commonControllerUrl + '/GetUserControlHtmlAjax/',
                            data: { 'currUserID': friendUserID, 'viewName': '..\\User\\Controls\\ShowLayer\\AddFriend' },
                            success: function(data) {
                                HideLoading();
                                $('body').append(data);
                                if (parseInt(friendUserID) > 0)
                                    ShowAddFriend(friendUserID);
                                else
                                    ShowFindFriend();
                            },
                            error: function(xmlHttpRequest, error) {
                                HideLoading();
                                AlertTips("系统提示：", "对不起，系统故障，我们将尽快处理！", 2);
                            }
                        });
                    } else {
                        if (parseInt(friendUserID) > 0)
                            ShowAddFriend(friendUserID);
                        else
                            ShowFindFriend();
                    }
                }
            },
            error: function(xmlHttpRequest, error) {
                AlertTips("系统提示：", "对不起，系统故障，我们将尽快处理！", 2);
            }
        });
    });    
}
