﻿///用户头像弹出层
var UserTip = {
    //初始化
    Init: function () {
        $('.avatarimg').unbind();

        $('.avatarimg').each(function () {
            $this = $(this);
            if ($this.parent().tagName() == "a") {
                if ($this.parent().parent().attr("name") != "user-tip-span") {
                    $this.parent().wrap('<span name="user-tip-span"></span>');
                }
            } else {
                if ($this.parent().attr("name") != "user-tip-span") {
                    $this.wrap('<span name="user-tip-span"></span>');
                }
            }
        });

        $('.avatarimg').hover(function () {
            $(this).stopTime();
            $(this).oneTime("600ms", function () {
                UserTip.ShowTip(this);
            });
        }, function () {
            $(this).stopTime();
            $(this).oneTime("800ms", function () {
                UserTip.HideTip(this);
            });
        });
        $(".button").initButton();
    },
    //父窗口span，如果图片父标签为a，则在a外面创建一个span，否则直接在外面创建一个span
    parentSpan: null,
    //提示框
    tipDialog: null,
    //初始化提示层代码
    InitTipHtml: function ($this) {
        var uid = $this.attr("uid"); //Id
        var imgsrc = $this.attr("src"); //头像地址        
        var uname = $this.attr("uname"); //名称

        xkcom.get("http://www.xunkoo.com/UserTip/GetUserTipInfo", { uid: kuid, tipuid: uid }, function (data) {

            if (uname == null) {
                uname = data.uNickName;
            }
            UserTip.tipDialog = jQuery('<table class="uc-image-menu" cellspacing="0" cellpadding="0" border="0" style="display:none;"></table>');
            var tiphtml = '<tr><td class="ucim-tdtl">&nbsp;</td><td class="ucim-tdtc">&nbsp;</td><td class="ucim-tdtr">&nbsp;</td></tr>';
            tiphtml += '<tr>';
            tiphtml += '<td class="ucim-tdll">&nbsp;</td>';
            tiphtml += '<td class="ucim-main clearfix">';
            tiphtml += '<div class="ucim-curr" style="margin-top:-7px;margin-left:5px;"><div class="cover">◆</div><div class="bg">◆</div></div>';
            tiphtml += '<div class="ucim-mtop clearfix">';
            tiphtml += ' <div class="ucim-mleft">';
            tiphtml += '<div><img src="' + imgsrc + '" style="width:80px; height:80px;" alt="" /></div>';
            tiphtml += ' <div class="attnum"><b>粉丝：</b>' + data.FollowForMeNumber + '</div>';
            tiphtml += ' </div>';
            tiphtml += '<div class="ucim-mright">';
            tiphtml += '<div class="ucim-mright-rt">';
            if (kuid == -1) {
                tiphtml += '<a href="javascript:;" onclick="xkcom.login(); return false;"><span class="add-friend" title="加为好友"></span></a>';
            } else {
                if (uid != kuid) {
                    if (data.IsFriend) {
                        tiphtml += '<span class="add-friend-yet" title="已是好友"></span>';
                    } else {
                        tiphtml += '<a href="javascript:;" onclick="UserTip.ShowAddFriend(' + uid + ',\'' + data.uNickName + '\',\'' + imgsrc + '\'); return false;"><span class="add-friend" title="加为好友"></span></a>';
                    }
                }
            }
            if (kuid == -1) {
                tiphtml += '<a href="javascript:;" onclick="xkcom.login(); return false;"><span class="send-message" title="站内互动"></span></a>';
            } else {
                tiphtml += '<a href="javascript:;" onclick="UserTip.ShowMessage(' + uid + ',\'' + data.uNickName + '\'); return false;"><span class="send-message" title="站内互动"></span></a>';
            }
            tiphtml += '</div>';
            tiphtml += '<div><a href="http://space.xunkoo.com/' + uid + '" class="user-name">' + uname + '</a>';
            if (data.Gender == 0) {
                tiphtml += '<span class="user-sex-male" title="男"></span></div>';
            } else {
                tiphtml += '<span class="user-sex-female" title="女"></span></div>';
            }
            tiphtml += '<div class="user-school">' + data.SchoolName + '</div>';
            var lv3 = parseInt(data.Grade / 16);
            var lv2 = parseInt((data.Grade % 16) / 4);
            var lv1 = parseInt((data.Grade % 16) % 4);
            tiphtml += '<div title="' + data.Grade + '">';
            for (var i = 0; i < lv3; i++) {
                tiphtml += '<span class="level_icon3"></span>';
            }
            for (var i = 0; i < lv2; i++) {
                tiphtml += '<span class="level_icon2"></span>';
            }
            for (var i = 0; i < lv1; i++) {
                tiphtml += '<span class="level_icon1"></span>';
            }
            tiphtml += '</div>';
            //            tiphtml += '<div><span class="level_icon1"></span><span class="level_icon1"></span><span class="level_icon2"></span><span class="level_icon2"></span><span class="level_icon2"></span></div>';
            tiphtml += '<div class="user-update">' + (data.New == null ? "" : data.New) + '</div>';
            tiphtml += '</div>';
            tiphtml += '</div>';
            tiphtml += '<div class="ucim-mbottom clearfix">';

            var $medalslen = data.Modals.length;

            tiphtml += '<div class="ucim-brb"><a href="javascript:;" class="user-xpage-up user-xpage-up-disabled" onclick="UserTip.MedalMove(this,\'left\'); return false;">&nbsp;</a><a href="javascript:;" class="user-xpage-down" onclick="UserTip.MedalMove(this,\'right\'); return false;">&nbsp;</a></div>';


            if (kuid == -1) {
                tiphtml += '<div class="ucim-bl">';
                tiphtml += '<a href="javascript:;" class="add-atten" onclick="xkcom.login(); return false;">加关注</a>';
                tiphtml += ' </div>';

                tiphtml += '<div class="ucim-br" style="width:156px;">';
            } else {
                if (!data.IsFriend && uid != kuid) {
                    tiphtml += '<div class="ucim-bl">';
                    if (data.IsFollow) {
                        tiphtml += '  <span class="cancel-atten">已关注 | <a href="javascript:;" onclick="UserTip.RemoveFollow(' + uid + '); return false;">取消</a></span>';
                        tiphtml += '<a href="javascript:;" class="add-atten" style="display:none;" onclick="UserTip.AddFollow(' + uid + ',\'' + data.uNickName + '\'); return false;">加关注</a>';
                    } else {
                        tiphtml += '  <span class="cancel-atten" style="display:none;">已关注 | <a href="javascript:;" onclick="UserTip.RemoveFollow(' + uid + '); return false;">取消</a></span>';
                        tiphtml += '<a href="javascript:;" class="add-atten" onclick="UserTip.AddFollow(' + uid + ',\'' + data.uNickName + '\'); return false;">加关注</a>';
                    }
                    tiphtml += ' </div>';

                    tiphtml += '<div class="ucim-br" style="width:156px;">';
                } else {
                    tiphtml += '<div class="ucim-br" style="width:234px;">';
                }
            }

            tiphtml += '<div class="jquery-medals" style="width:' + (26 * $medalslen) + 'px;margin-left:0px;">';
            for (var k in data.Modals) {
                $medal = data.Modals[k];
                if ($medal.IsExists) {
                    tiphtml += '<a href="http://www.xunkoo.com/medal/mymedal"><img src="http://www.xunkoo.com/images/icon/medal/24/' + $medal.MedalId + '.gif" title="' + $medal.MedalName + '" style="height:24px; width:24px; margin-right:2px;"></a>';
                } else {
                    tiphtml += '<a href="http://www.xunkoo.com/medal"><img src="http://www.xunkoo.com/images/icon/medal/24/' + $medal.MedalId + '-1.gif" title="' + $medal.MedalName + '"  style="height:24px; width:24px; margin-right:2px;"></a>';
                }
            }
            tiphtml += '</div>';
            tiphtml += '</div>';


            tiphtml += ' </div>';
            tiphtml += '</td>';
            tiphtml += '<td class="ucim-tdrr"></td>';
            tiphtml += '</tr>';
            tiphtml += '<tr><td class="ucim-tdbl">&nbsp;</td><td class="ucim-tdbc">&nbsp;</td><td class="ucim-tdbr">&nbsp;</td></tr>';

            UserTip.tipDialog.append(tiphtml);

            UserTip.parentSpan.prepend(UserTip.tipDialog);

            UserTip.tipDialog.bind("mouseover", function () {
                $this.stopTime();
            });
            UserTip.tipDialog.bind("mouseleave", function () {
                UserTip.HideTip($this);
            });
            UserTip.tipDialog.bind("outerClick", function () {
                UserTip.HideTip($this);
            });




            var bodyheight = $(document).height(); //内容高度
            var bodywidth = $(document).width(); //内容宽度
            var marginleft = (bodywidth - 970) / 2; //内容距离左边距

            var thismargintop = $this.offset().top; //上边距
            var thismarginleft = $this.offset().left - marginleft; //左边距(相对于内容)
            var thisheight = $this.height();
            var thiswidth = $this.width();

            var tipwidth = UserTip.tipDialog.width();
            var tipheight = UserTip.tipDialog.height();
            var corner = UserTip.tipDialog.find(".ucim-curr");

            if ((thismargintop + thisheight + tipheight) <= bodyheight) {
                if ((thismarginleft + tipwidth) <= 970) {
                    UserTip.tipDialog.css("margin-top", thisheight);
                    UserTip.tipDialog.css("margin-left", 0);
                    corner.addClass("ucim-curr-top");
                } else {
                    UserTip.tipDialog.css("margin-top", thisheight);
                    UserTip.tipDialog.css("margin-left", 0 - tipwidth + thiswidth);
                    corner.css("margin-left", tipwidth - 30);
                    corner.addClass("ucim-curr-top");
                }
            } else {
                if ((thismarginleft + tipwidth) <= 970) {
                    UserTip.tipDialog.css("margin-top", 0 - tipheight);
                    UserTip.tipDialog.css("margin-left", 0);
                    corner.css("margin-top", tipheight - 16);
                    corner.addClass("ucim-curr-bottom");
                } else {
                    UserTip.tipDialog.css("margin-top", 0 - tipheight);
                    UserTip.tipDialog.css("margin-left", 0 - tipwidth + thiswidth);
                    corner.css("margin-top", tipheight - 16);
                    corner.css("margin-left", tipwidth - 30);
                    corner.addClass("ucim-curr-bottom");
                }
            }
            //            UserTip.tipDialog.show();
            UserTip.tipDialog.fadeIn("slow");

        });
    },
    //移动勋章位置
    MedalMove: function (obj, move) {
        $this = $(obj);

        $medals = $this.parent().parent().find(".jquery-medals");
        $width = $medals.width();
        $parentwidth = $medals.parent().width();
        $left = parseInt($medals.css("margin-left"));
        if (move == "right") {
            $this.parent().find(".user-xpage-up").removeClass("user-xpage-up-disabled");
            if (($width + $left) > $parentwidth) {
                $medals.animate({ "margin-left": ($left - $parentwidth) }, 200);
                //                $medals.css("margin-left", ($left - $parentwidth));
            }
            if (($width + $left - $parentwidth) <= $parentwidth) {
                $this.addClass("user-xpage-down-disabled");
            }

        } else {
            $this.parent().find(".user-xpage-down").removeClass("user-xpage-down-disabled");
            if ($left < 0) {
                $medals.animate({ "margin-left": ($left + $parentwidth) }, 200);
                //                $medals.css("margin-left", ($left + $parentwidth));
            }
            if (($left + $parentwidth) >= 0) {
                $this.addClass("user-xpage-up-disabled");
            }
        }
    },

    //显示提示框
    ShowTip: function (obj) {
        var $this = $(obj);
        if ($this.parent().tagName() == "a") {
            UserTip.parentSpan = $this.parent().parent();
        } else {
            UserTip.parentSpan = $this.parent();
        }

        if (UserTip.parentSpan.children(".uc-image-menu").length > 0) {
            UserTip.tipDialog = UserTip.parentSpan.children(".uc-image-menu");
            UserTip.tipDialog.fadeIn("slow");
        } else {
            UserTip.InitTipHtml($this);
        }

    },
    //隐藏提示框
    HideTip: function (obj) {
        var $this = $(obj);
        if ($this.parent().tagName() == "a") {
            $ParentSpan = $this.parent().parent();

            if ($ParentSpan.find(".uc-image-menu").length > 0) {
                $tipDialog = $ParentSpan.find(".uc-image-menu");
                $tipDialog.hide();
            }

        } else {
            $ParentSpan = $this.parent();

            if ($ParentSpan.find(".uc-image-menu").length > 0) {
                $tipDialog = $ParentSpan.find(".uc-image-menu");
                $tipDialog.hide();
            }
        }

        //        UserTip.tipDialog.hide();
        //        UserTip.tipDialog.fadeOut("slow");
    },
    //加关注
    AddFollow: function (fuid, fname) {
        xkcom.get("http://www.xunkoo.com/Connections/AddFollow", { fuid: fuid, FollowNickName: fname }, function (data) {
            if (data.status == 1) {//成功关注
                UserTip.tipDialog.find(".cancel-atten").show();
                UserTip.tipDialog.find(".add-atten").hide();
                var n = $("#me-follow").html();
                $("#me-follow").html((parseInt(n) + 1));
            } else {
                var err = "";
                if (data.status == 0) {
                    err = "刷新一下你会发现你已经是Ta的粉丝了";
                } else if (data.status == 2) {
                    err = "你们已是密友，不需要再关注了";
                } else {
                    err = "对方拒绝陌生人关注";
                }
                xkcom.alert(err);
            }
        });
    },
    //取消关注
    RemoveFollow: function (fuid, fname) {
        xkcom.get("http://www.xunkoo.com/Connections/RemoveFollow", { fuid: fuid }, function (data) {
            if (data.status) {//成功取消关注
                UserTip.tipDialog.find(".cancel-atten").hide();
                UserTip.tipDialog.find(".add-atten").show();
                var n = $("#me-follow").html();
                $("#me-follow").html((parseInt(n) - 1));
            } else {
                xkcom.alert("关注可能已不存在，请刷新页面后再试");
            }
        });
    },
    //显示加好友对话框
    ShowAddFriend: function (fuid, fname, imgurl) {
        var html = '<div class="ucall_add_friend clearfix">';
        html += '<div class="ucall_add_friend_left"><img src="' + imgurl + '" style="width:100px; height:100px;" /></div>';
        html += '<div class="ucall_add_friend_right">';
        html += '<div class="ucall_add_friend_tips">有什么想跟TA说：</div>';
        html += '<div><textarea id="ucall-message"></textarea></div>';
        html += '<div class="ucall_add_friend_status clearfix">';
        html += '<span id="txt-tip-add-friend">还可输入<b>50</b>个字</span>';
        html += '请求对方确认后，既可成为好友';
        html += '</div>';
        html += '<div class="ucall_add_friend_btn uc-button-a-n6-h22">';
        html += '<a id="ucall-subimit" href="javascript:;" class="button">确定</a> <a id="ucall-cancel" href="javascript:;" class="button" onclick="Boxy.get(this).unload(); return false;">取消</a>';
        html += '</div>';
        html += '</div>';
        html += '</div>';
        var dialog = new Boxy(html, { title: "将" + fname + "加为好友?", afterShow: function () {
            $("#ucall-message").bind("keyup", function () {
                xkcom.MaxLimit("#ucall-message", "#txt-tip-add-friend", 50);
            });
            $(".button").initButton();
        }
        });
        $("#ucall-subimit").click(function () {
            var message = $.trim($("#ucall-message").val());
            if (message.length > 50) {
                return;
            }
            xkcom.get("http://www.xunkoo.com/Connections/SendFriendRequest", { uid: kuid, fuid: fuid, message: message }, function (data) {
                $("#ucall-cancel").click();
                xkcom.TimerAlert("好友请求已发送。");
            });
            return false;
        });
    },

    //显示站内信息弹出框
    ShowMessage: function (fuid, fname, selectindex) {
        //打招呼表情
        var Emoticons = ['Hi~~', '捏一下', '扮鬼脸', '飞吻', '扁', '抛媚眼', '拥抱', '挠痒痒', '鄙视你', '踩一脚', '美女啊', '帅哥啊', '做个躬', '干一杯', '施舍你'];
        var html = '<div class="msg-Interactives" style="padding:0px;">';

        html += '<ul class="msg-options">';
        html += '<li id="msg-option-1" class="msg-option msg-optionSelected" name="sms">站内信</li>';
        html += '<li id="msg-option-2" class="msg-option" name="hi">打招呼</li>';

        html += '</ul><div id="msg-close" onclick="Boxy.get(this).unload();"></div>';

        html += '<div id="msg-sms" class="msg-InteractivesBody"><div style="padding:10px;"><textarea  id="msg-message" ></textarea></div>';
        html += '<div style="padding:0px 10px 0px 0px; text-align:right" class="uc-button-a-n6-h22">';
        html += '<span class="msg-error" style="display:none;">你还是对Ta说点什么吧</span>';
        html += '<span class="msg-lengthtip">还可输入<b>100</b>个字</span>';
        html += '<a id="msg-submit" href="javascript:;" class="button">确定</a>&nbsp;';
        html += '<a id="msg-cancel" href="javascript:;" class="button"  onclick="Boxy.get(this).unload(); return false;">取消</a></div></div>';

        html += '<div id="msg-hi" class="msg-InteractivesBody" style="display:none;"><div style="padding:10px;" class="clearfix">';
        for (var i = 0; i < Emoticons.length; i++) {
            html += '<li class="e' + (i + 1) + '" onclick="UserTip.SendMessage(1,' + fuid + ',\'' + fname + '\',' + (i + 1) + ')">' + Emoticons[i] + '</li>';
        }
        html += '</div></div>';

        html += '<div>';
        new Boxy(html, { draggable: false, afterShow: function () {
            if (selectindex != null && selectindex != 1) {
                $(".msg-option").removeClass("msg-optionSelected");
                $("#msg-option-" + selectindex).addClass("msg-optionSelected");
                var t = $("#msg-option-" + selectindex).attr("name");
                $(".msg-InteractivesBody").hide();
                $("#msg-" + t).show();
            }
            $(".msg-option").bind("click", function () {
                $(".msg-option").removeClass("msg-optionSelected");
                $(this).addClass("msg-optionSelected");
                var t = $(this).attr("name");
                $(".msg-InteractivesBody").hide();
                $("#msg-" + t).show();
            });
            $("#msg-message").bind("keyup", function () {
                xkcom.MaxLimit("#msg-message", ".msg-lengthtip", 100);
            });
            $("#msg-submit").bind("click", function () {
                var message = $.trim($("#msg-message").val());
                if (message.length > 0) {
                    $(".msg-error").hide();
                    if (message.length > 100) {
                        return;
                    }
                    UserTip.SendMessage(0, fuid, fname, message);
                } else {
                    $(".msg-error").show();
                }
                return false;
            });
            $(".button").initButton();
        }
        });
    },
    //发送站内信
    SendMessage: function (type, fuid, fname, message) {
        xkcom.get("http://www.xunkoo.com/message/SendMessage", { type: type, uid: kuid, fuid: fuid, fname: fname, message: message }, function (data) {
            if (data.status) {
                $("#msg-close").click();
                if (type == 0) {
                    xkcom.TimerAlert("您给<b>" + fname + "</b>的站内信已送出");
                } else {
                    xkcom.TimerAlert("您给<b>" + fname + "</b>打了个招呼");
                }
            } else {
                xkcom.alert("发送失败");
            }
        });
    }
};

