﻿

function InviteFriend(userId, friendId, usertype) {
    //alert("abc");
    var iFriend = new Friend(userId, friendId, usertype);
    iFriend.show();
    iFriend.List();
}


function RemoveFriend(userId, friendId) {
    //alert("abc");
    var iFriend = new Friend(userId, friendId, "Pop");
    iFriend.show();
    iFriend.List1();
}

Friend = function(userId, friendId, usertype) {
    this.UserId = userId;
    this.FriendId = friendId;
    this.UserType = usertype;
    this.isie = (document.all) ? true : false;
    this._saveTabIndexes = new Array();
    this._saveDesableSelect = new Array();
    this._tagWithTabIndex = new Array('A', 'AREA', 'BUTTON', 'INPUT', 'OBJECT', 'SELECT', 'TEXTAREA', 'IFRAME');
    this.backgroundElement = null;
    this.foregroundElement = null;
    this.scrollLeft = (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
    this.scrollTop = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
    this.Dialog = null;
    this.DialogTitle = null;
    this.DialogContent = null;
    this.dialogId = "D_Friend_Dialog";
    this.dialogTitleId = "D_Friend_Title";
    this.dialogIdContentId = "D_Friend_Content";
    this.clientWidth = null;
    this.clientHeight = null;
    this.init();
};


Friend.prototype.init = function() {

    var sBox = '\
            <table class="pop_dialog_table" style="display: none" id="' + this.dialogId + '">\
                <tr>\
                    <td class="pop_topleft">\
                    </td>\
                    <td class="pop_border">\
                    </td>\
                    <td class="pop_topright">\
                    </td>\
                </tr>\
                <tr>\
                    <td class="pop_border">\
                    </td>\
                    <td class="pop_content">\
                        <div class="divMsgFrameCss">\
                            <div class="divMsgTitleCss" id="' + this.dialogTitleId + '">\
                                </div>\
                            <div class="divMsgMainCss" id="' + this.dialogIdContentId + '">\
                            </div>\
                        </div>\
                    </td>\
                    <td class="pop_border">\
                    </td>\
                </tr>\
                <tr>\
                    <td class="pop_bottomleft">\
                    </td>\
                    <td class="pop_border">\
                    </td>\
                    <td class="pop_bottomright">\
                    </td>\
                </tr>\
            </table>\  ';

    var oDiv = document.createElement('span');
    oDiv.innerHTML = sBox;
    document.body.appendChild(oDiv);

    this.DialogTitle = $get(this.dialogTitleId);
    this.DialogContent = $get(this.dialogIdContentId);
    this.Dialog = $get(this.dialogId);

};

Friend.prototype.List = function() {
    var self = this;
    this.loading();
    if (this.UserId > 0 && this.UserType == "Pop") {
        Fuwoo.BopWeb.Pop.WebService.Friend.GetUserDetail(this.FriendId, function(result) { self.bulidList(result); });
    }
    else {
        this.showMSG(-1);
    }
};

Friend.prototype.List1 = function() {
    var self = this;
    this.loading();
    if (this.UserId > 0 && this.UserType == "Pop") {
        Fuwoo.BopWeb.Pop.WebService.Friend.GetUserDetail(this.FriendId, function(result) { self.bulidList1(result); });
    }
    else {
        this.showMSG(-1);
    }
};


Friend.prototype.bulidList = function(result) {
    var sBox = '\
                <div style="padding:5px;display:block;overflow:hidden">\
                <div style="float:left; margin-right:10px;">\
                <img  src="' + result.UserPortraitPath + '"/></div>\
                <div style="font-size:9pt; float:left;  height:120px;">\
                <table style="width: 100%;" >\
                <tr>\
                <td style="text-align: right; width: 20%;color: #808080;">\
                会员名称:\
                </td>\
                <td id="tdInviteUserName">' + result.NickName + '\
                </td>\
                </tr>\
                <tr>\
                <td style="text-align: right; width: 20%; vertical-align: top; padding-top: 5px;color: #808080;">\
                申请留言:\
                </td>\
                <td style="padding-top: 5px;">\
                <textarea name="textfield" id="tbApplyContent" class="TextBoxCss" style="height:50px;width:250px"></textarea>\
                </td>\
                </tr>\
                </table>\
                </td>\
                </tr>\
                </table></div></div>';

    var mBox = '\
                    <div class="divMsgBottomCss">\
                        <input type="button" name="Submit" value="确定" id="btnFriendClick" class="inputsubmit" />\
                            <input type="button" name="Submit" value="取消" id="btnFriendCancel" class="inputaux" />\
                        </div>';
    this.innerContent(sBox + mBox);
    this.title("加为好友");
    this.addEvent();
    this.middle(this.Dialog);
};


Friend.prototype.bulidList1 = function(result) {
    var sBox = '\
                <div style="padding:5px;display:block;overflow:hidden">\
                <div style="float:left; margin-right:10px;">\
                <img  src="' + result.UserPortraitPath + '"/></div>\
                <div style="font-size:9pt; float:left;  height:120px;">\
                <table style="width: 100%;" >\
                <tr>\
                <td style="text-align: right; width: 20%;color: #808080;">\
                会员名称:\
                </td>\
                <td id="tdInviteUserName">' + result.NickName + '\
                </td>\
                </tr>\
                </table>\
                </td>\
                </tr>\
                </table></div></div>';

    var mBox = '\
                    <div class="divMsgBottomCss">\
                        <input type="button" name="Submit" value="确定" id="btnFriend1Click" class="inputsubmit" />\
                            <input type="button" name="Submit" value="取消" id="btnFriend1Cancel" class="inputaux" />\
                        </div>';
    this.innerContent(sBox + mBox);
    this.title("确定要解除好友关系?");
    this.addEvent1();
    this.middle(this.Dialog);
};

Friend.prototype.addEvent = function() {
    var self = this;

    var clickbutton = $get("btnFriendClick");
    this.addEventListener(clickbutton, "click", function(e) { return self.onclick(); }, false);

    var btnCancel = $get("btnFriendCancel");
    this.addEventListener(btnCancel, "click", function(e) { return self.close(); }, false);

};

Friend.prototype.addEvent1 = function() {
    var self = this;

    var clickbutton = $get("btnFriend1Click");
    this.addEventListener(clickbutton, "click", function(e) { return self.onclick1(); }, false);

    var btnCancel = $get("btnFriend1Cancel");
    this.addEventListener(btnCancel, "click", function(e) { return self.close(); }, false);

};

Friend.prototype.onclick = function()
{
    var self = this;
    this.btndisable();
    var content = $get("tbApplyContent").value;
    Fuwoo.BopWeb.Pop.WebService.Friend.AddFriend(this.UserId, this.FriendId, content, function(result) { self.showMSG(result); });
};

Friend.prototype.onclick1 = function()
{
    var self = this;
    //this.btndisable();

    Fuwoo.BopWeb.Pop.WebService.Friend.RemoveFriend(this.UserId, this.FriendId, function(result) { self.showMSG(result); });
};

Friend.prototype.PastInt = function(obj) {
    if (obj == null)
        return 0;
    else
        return parseInt(obj);
};

//显示GIFT窗体
Friend.prototype.show = function() {
    this.Dialog.style.display = '';
    this.Dialog.style.position = 'absolute';
    this.Dialog.style.zIndex = 10001;
    this.middle(this.Dialog);
    this.setBG();
};

//隐藏背景不可遮盖物
Friend.prototype.disableTab = function() {
    var i = 0;
    var tagElements;
    var tagElementsInPopUp = new Array();
    Array.clear(this._saveTabIndexes);

    //Save all popup's tag in tagElementsInPopUp
    for (var j = 0; j < this._tagWithTabIndex.length; j++) {
        tagElements = document.getElementsByTagName(this._tagWithTabIndex[j]);
        for (var k = 0; k < tagElements.length; k++) {
            tagElementsInPopUp[i] = tagElements[k];
            i++;
        }
    }

    i = 0;
    for (var j = 0; j < this._tagWithTabIndex.length; j++) {
        tagElements = document.getElementsByTagName(this._tagWithTabIndex[j]);
        for (var k = 0; k < tagElements.length; k++) {
            if (Array.indexOf(tagElementsInPopUp, tagElements[k]) == -1) {
                this._saveTabIndexes[i] = { tag: tagElements[k], index: tagElements[k].tabIndex };
                tagElements[k].tabIndex = "-1";
                i++;
            }
        }
    }

    //IE6 Bug with SELECT element always showing up on top
    i = 0;
    if ((Sys.Browser.agent === Sys.Browser.InternetExplorer) && (Sys.Browser.version < 7)) {
        //Save SELECT in PopUp
        var tagSelectInPopUp = new Array();
        for (var j = 0; j < this._tagWithTabIndex.length; j++) {
            tagElements = document.getElementsByTagName('SELECT');
            for (var k = 0; k < tagElements.length; k++) {
                tagSelectInPopUp[i] = tagElements[k];
                i++;
            }
        }

        i = 0;

        Array.clear(this._saveDesableSelect);
        tagElements = document.getElementsByTagName('SELECT');
        for (var k = 0; k < tagElements.length; k++) {
            if (Array.indexOf(tagSelectInPopUp, tagElements[k]) == -1) { }
            this._saveDesableSelect[i] = { tag: tagElements[k], visib: this.getCurrentStyle(tagElements[k], 'visibility') };
            tagElements[k].style.visibility = 'hidden';
            i++;

        }

    }
};

//恢复背景不可遮盖物
Friend.prototype.restoreTab = function() {
    for (var i = 0; i < this._saveTabIndexes.length; i++) {
        this._saveTabIndexes[i].tag.tabIndex = this._saveTabIndexes[i].index;
    }
    Array.clear(this._saveTabIndexes);

    //IE6 Bug with SELECT element always showing up on top
    if ((Sys.Browser.agent === Sys.Browser.InternetExplorer) && (Sys.Browser.version < 7)) {
        for (var k = 0; k < this._saveDesableSelect.length; k++) {
            this._saveDesableSelect[k].tag.style.visibility = this._saveDesableSelect[k].visib;
        }
        Array.clear(this._saveDesableSelect);
    }
};


//获取当前样式
Friend.prototype.getCurrentStyle = function(element, attribute, defaultValue) {
    var currentValue = null;
    if (element) {
        if (element.currentStyle) {
            currentValue = element.currentStyle[attribute];
        } else if (document.defaultView && document.defaultView.getComputedStyle) {
            var style = document.defaultView.getComputedStyle(element, null);
            if (style) {
                currentValue = style[attribute];
            }
        }

        if (!currentValue && element.style.getPropertyValue) {
            currentValue = element.style.getPropertyValue(attribute);
        }
        else if (!currentValue && element.style.getAttribute) {
            currentValue = element.style.getAttribute(attribute);
        }
    }

    if ((!currentValue || currentValue == "" || typeof (currentValue) === 'undefined')) {
        if (typeof (defaultValue) != 'undefined') {
            currentValue = defaultValue;
        }
        else {
            currentValue = null;
        }
    }
    return currentValue;
};

//设置窗体居中
Friend.prototype.middle = function(element) {
    switch (Sys.Browser.agent) {
        case Sys.Browser.InternetExplorer:
            this.clientWidth = document.documentElement.clientWidth;
            this.clientHeight = document.documentElement.clientHeight;
            break;
        case Sys.Browser.Safari:
            this.clientWidth = window.innerWidth;
            this.clientHeight = window.innerHeight;
            break;
        case Sys.Browser.Opera:
            this.clientWidth = Math.min(window.innerWidth, document.body.clientWidth);
            this.clientHeight = Math.min(window.innerHeight, document.body.clientHeight);
            break;
        default:  // Sys.Browser.Firefox, etc.
            this.clientWidth = Math.min(window.innerWidth, document.documentElement.clientWidth);
            this.clientHeight = Math.min(window.innerHeight, document.documentElement.clientHeight);
            break;
    }
    var foregroundelementwidth = element.offsetWidth ? element.offsetWidth : element.scrollWidth;

    xCoord = ((this.clientWidth - foregroundelementwidth) / 2);

    var foregroundelementheight = element.offsetHeight ? element.offsetHeight : element.scrollHeight;

    yCoord = ((this.clientHeight - foregroundelementheight) / 2);

    if (element.style.position == 'absolute') {
        xCoord += this.scrollLeft;
    }
    element.style.left = (xCoord) + 'px';


    if (element.style.position == 'absolute') {
        yCoord += this.scrollTop;
    }
    element.style.top = yCoord + 'px';
};

//设置透明背景
Friend.prototype.setBG = function() {
    this.backgroundElement = document.createElement('div');
    this.backgroundElement.id = "D_Gift_Bg";
    this.backgroundElement.style.display = '';
    this.backgroundElement.style.position = 'absolute';
    this.backgroundElement.style.left = '0px';
    this.backgroundElement.style.top = '0px';
    this.backgroundElement.className = "backgroundElement";
    this.backgroundElement.style.zIndex = 10000;

    this.backgroundElement.style.width = Math.max(Math.max(document.documentElement.scrollWidth, document.body.scrollWidth), this.clientWidth) + 'px';
    this.backgroundElement.style.height = Math.max(Math.max(document.documentElement.scrollHeight, document.body.scrollHeight), this.clientHeight) + 'px';
    this.disableTab();
    document.body.appendChild(this.backgroundElement);
};

//关闭
Friend.prototype.close = function() {
    //    document.body.removeChild(this.backgroundElement);
    //    document.body.removeChild(this.giftDialog);
    this.backgroundElement.style.display = 'none';
    this.Dialog.style.display = 'none';
    this.restoreTab();
};

//窗体标题
Friend.prototype.title = function(msgstr) {

    this.DialogTitle.innerHTML = msgstr;
};

//窗体载入动画
Friend.prototype.loading = function() {
    this.DialogContent.innerHTML = "<span class='msgBigError'>正在加载数据，请等待...</span>";
};

//窗体内容添加
Friend.prototype.innerContent = function(txt) {
    this.DialogContent.innerHTML = txt;
};

//设置按钮不可用
Friend.prototype.btndisable = function() {
$get("btnFriendClick").disabled = "disabled";
};

//显示最后结果信息
Friend.prototype.showMSG = function(result)
{
    var titlemsg = null;
    var xBox = null;
    switch (parseInt(result))
    {
        case 0:
            titlemsg = "错误";
            xBox = '<span class="msgBigError">服务器忙，请稍候再试！</span>';
        case -1:
            titlemsg = "错误";
            xBox = '<span class="msgBigError">您必须先登录才能使用此功能。</span>';
            break;
        case -100:
            titlemsg = "错误";
            xBox = '<span class="msgBigError">您已被对方加为好友，请查看陌生人。</span>';
            break;
        case -11:
            titlemsg = "完成";
            xBox = '<span class="msgBigError">成功解除好友关系。</span>';
            break;
        default:
            titlemsg = "完成";
            xBox = '<span class="msgBigError">加为好友成功，等待对方确认。</span>';
    }

    var mBox = '<div class=\"divMsgBottomCss\">\
                        <input type="button" name="Submit" value="关闭" class="inputaux" onclick="javascript:location.reload();"/>\
                        </div>';
    this.innerContent(xBox + mBox);
    this.title(titlemsg);
    this.middle(this.Dialog);
};


Friend.prototype.addEventListener = function(element, eventType, handler, capture) {
    try {
        if (element.addEventListener)
            element.addEventListener(eventType, handler, capture);
        else if (element.attachEvent)
            element.attachEvent("on" + eventType, handler);
    }
    catch (e) { }
};


Friend.prototype.addClassName = function(ele, className) {
    if (!ele || !className || (ele.className && ele.className.search(new RegExp("\\b" + className + "\\b")) != -1))
        return;
    ele.className += (ele.className ? " " : "") + className;
};

Friend.prototype.removeClassName = function(ele, className) {
    if (!ele || !className || (ele.className && ele.className.search(new RegExp("\\b" + className + "\\b")) == -1))
        return;
    ele.className = ele.className.replace(new RegExp("\\s*\\b" + className + "\\b", "g"), "");
};

Friend.prototype.hasClassName = function(ele, className) {
    if (!ele || !className || !ele.className || ele.className.search(new RegExp("\\b" + className + "\\b")) == -1)
        return false;
    return true;
};