﻿/*
    친구 관련 js
    friend.reqeust(custkey,0);
    friend.cancel(custkey,1);
*/
var friend = {
    lockobj: false,
    lock: function () { this.lockobj = true; },
    unlock: function () { this.lockobj = false; },
    request: function (custkey, friendStatus) {
        var _self = this;
        if (_self.lockobj) {
            alert('처리중입니다. 잠시만 기다려주세요.');
            return;
        }

        _self.lock();   //lock 설정
        var param = { method: "FriendRequest", custkey: custkey};
        $j.ajax({
            url: "/api/friend.aspx",
            data: param,
            dataType: 'json',
            error: function (xhr, textStatus, errorThrown) { _self.unlock(); alert('요청이 실패되었습니다.' + errorThrown); },
            success: function (json) {
                _self.unlock();
                if (json.Result == 1) {
                    if (friendStatus == 0) { alert('친구 신청 되었습니다.'); }
                    if (friendStatus == 3) { alert('친구 추가 되었습니다.'); }
                    if (friendStatus == 4) { alert('친구 신청 되었습니다.'); }
                    location.reload();  //새로고침
                } else {
                    alert(json.ErrMsg);
                }
            }
        });

    },
    cancel: function (custkey, friendStatus) {
        var _self = this;
        if (_self.lockobj) {
            alert('처리중입니다. 잠시만 기다려주세요.');
            return;
        }

        if (friendStatus == 1) { if (!confirm("친구 취소 하시겠습니까?")) return; }
        if (friendStatus == 2) { if (!confirm("신청 취소 하시겠습니까?")) return; }
        if (friendStatus == 4) { if (!confirm("즐겨찾기 취소 하시겠습니까?")) return; }

        _self.lock();   //lock 설정
        var param = { method: "FriendCancel", custkey: custkey };
        $j.ajax({
            url: "/api/friend.aspx",
            data: param,
            dataType: 'json',
            error: function (xhr, textStatus, errorThrown) { _self.unlock(); alert('요청이 실패되었습니다.' + errorThrown); },
            success: function (json) {
                _self.unlock();
                if (json.Result == 1) {
                    if (friendStatus == 1) { alert("친구 취소 되었습니다."); }
                    if (friendStatus == 2) { alert("친구 신청 취소 되었습니다."); }
                    if (friendStatus == 4) { alert("즐겨찾기 취소 되었습니다."); }
                    location.reload();  //새로고침
                }
                else {
                    alert(json.ErrMsg);
                }
            }
        });
    }
};
