
var SoDaoRecord = {
    prevVal: null,
    getlength: function() {
        var reg = /\(\:\d+\)|./ig;
        var rtn = $('.publisher-txt').val().match(reg);
        var matchnum = 0;
        if (rtn != null) {
            matchnum = rtn.length;
        }
        return matchnum;
    },
    f: function() {
        var curVal = $('.publisher-txt').val();
        if (curVal != this.prevVal) {
            this.prevVal = curVal;
        } else {
            return;
        }
        var l = SoDaoRecord.getlength();
        $('.tip-num span').text(l);
        if (l > 0) {
            if (l > 140) {
                //减少
                var reg = /\(\:\d+\)|./ig;
                var rtn = curVal.match(reg);
                if (rtn != null) {
                    var strTemp = "";
                    for (var i = 0; i < rtn.length; i++) {
                        if (i < 140)
                            strTemp += rtn[i];
                        else {
                            $('.publisher-txt').val(strTemp)
                            break;
                        }
                    }
                }
            }
            $('.publisher .submit2').attr('disabled', '');
        } else {
            $('.publisher .submit2').attr('disabled', 'disabled');
        }
    },
    checkInterval: null,
    chackTextarea: function(obj, num, objTip) {
        obj.keyup(SoDaoRecord.f);
        obj.click(function() { $('#recordInputHelper').slideDown("fast"); SoDaoRecord.bFlagInPublisher = true; });
        obj.focus(function() { $('#recordInputHelper').slideDown("fast"); SoDaoRecord.f(); });
    },
    refresh: true,
    InitRecordInputBox: function(_ref) {
        if (_ref == false) { this.refresh = _ref; }
        this.chackTextarea($('.publisher-txt'), 140, $('.tip-num'));
        this.checkInterval = setInterval(SoDaoRecord.f, 100);
        $('.want-box > .input-texts > a').each(function() {
            $(this).attr('href', 'javascript:;');
            $(this).click(function() {
                (function(o) {
                    $('.publisher-txt').val("我想去" + $(o).text());
                    $('.publisher-txt').focus();
                })($(this))
            });
        });

        $('.doing-box > .input-texts > a').each(function() {
            $(this).attr('href', 'javascript:;');
            $(this).click(function() {
                (function(o) {
                    $('.publisher-txt').val("我正在" + $(o).text());
                    $('.publisher-txt').focus();
                })($(this))
            });
        });
        SoDaoRecord.PublisherStartUp();
        SoDaoRecord.InitPublisherFace();
    }
,
    toggleComment: function(c, u, m) {
        var oReplay = $('#rdiv_' + c + ' > .movement-foot a:first');
        if (oReplay.text() != '收起回复') {
            oReplay.attr('oldtext', oReplay.text());
            oReplay.text("收起回复");
            var random = Math.random();
            sDialog.loading("数据获取中。。。", 10000, 300, 100);
            $.post("GetRecordComment", { cd: c, un: u, md: m, rn: random }, function(d) {
                sDialog.hideLoading();
                $("#rdiv_" + c).append(d);
                SoDaoRecord.commentBoxInit(c);
            });
        }
        else {
            oReplay.text(oReplay.attr('oldtext'));
            $('#comment_' + c).hide('fast', function() { $(this).remove() });
        }
    }
,
    AddARecordComment: function(o, rc, run, md) {
        var chPolicy = $(o).parent().children("input[type='checkbox']").attr('checked');
        var content = $(o).parents(".comment-box").children("input:first").val();
        if (content.length == 0) {
            sDialog.info("请输入评论内容！", "提示", 100, 20, 1500);
            return;
        }
        var rootid = $(o).attr('rid');
        if (typeof (rootid) == 'undefined') { rootid = ""; }
        var r2un = $(o).attr('r2un');
        if (typeof (r2un) == 'undefined') { r2un = ""; }
        var r2uni = $(o).attr('r2uni');
        if (typeof (r2uni) == 'undefined') { r2uni = ""; }
        var random = Math.random();
        sDialog.loading("数据提交中。。。", 10000, 300, 100);
        $.post('AddRecordComment', { "c": content, "p": chPolicy, "rid": rootid, "r2un": r2un, "r2uni": r2uni, 'rc': rc, 'run': run, 'md': md, rd: random }, function(d) {
            sDialog.hideLoading();
            if (d == "0") {
                sDialog.error("评论失败，请稍候再试！", '错误信息', 300, 50);
                return;
            }
            $('.expression-box[cid]').remove();
            $('#comment_' + rc).remove();
            $("#rdiv_" + rc).append(d);
            var oReplay = $('#rdiv_' + rc + ' > .movement-foot a:first');
            var reg = /(\d+)条回复/ig;
            var countTemp = 0;
            if (reg.test(oReplay.attr('oldtext'))) {
                countTemp = RegExp.$1;
            }
            countTemp++;
            oReplay.attr('oldtext', countTemp + "条回复");
            SoDaoRecord.commentBoxInit(rc);
        });
    }
    ,
    DeleteRecordCommentCallBack: function(o, data) {
        sDialog.loading("数据提交中。。。", 10000, 300, 100);
        var random = Math.random();
        $.post('DelRecordComment', { "rc": data.rc, "cc": data.cc, "cp": data.cp, "run": data.run, "md": data.md, rd: random }, function(d) {
            sDialog.hideLoading();
            if (d == 1) {
                $(o).parent('li[rid]').slideUp('fast', function(po) { return function() { $(po).parent('li[rid]').remove(); } } (o));
                var oReplay = $('#rdiv_' + data.rc + ' > .movement-foot a:first');
                var reg = /(\d+)条回复/ig;
                var countTemp = 0;
                if (reg.test(oReplay.attr('oldtext'))) {
                    countTemp = RegExp.$1;
                }
                countTemp--;
                if (countTemp == 0)
                    oReplay.attr('oldtext', "回复");
                else
                    oReplay.attr('oldtext', countTemp + "条回复");
            }
            else {
                sDialog.error("评论删除失败，请稍候再试！", '错误信息', 300, 50);
            }
        });
    },
    DeleteRecordComment: function(o, rc, cc, cp, run, md) {
        delConfirm.show(o, 110, '记录', { 'rc': rc, 'cc': cc, 'cp': cp, 'run': run, 'md': md }, SoDaoRecord.DeleteRecordCommentCallBack);
        return false;
    }
    ,
    DelRecordCallBack: function(o, data) {
        sDialog.loading("数据提交中。。。", 10000, 300, 100);
        var random = Math.random();
        $.post('DelRecord', { "rc": data.rc, "run": data.run, "md": data.md, rd: random }, function(d) {
            sDialog.hideLoading();
            if (d == 1) {
                $(o).parents('li.lists').slideUp('fast', function(po) { return function() { $(po).parents('li.lists').remove(); } } (o));
                //$(o).parents('li.lists').remove();
            }
            else {
                sDialog.error("记录删除失败，请稍候再试！", '错误信息', 300, 50);
            }
        });
    },
    DelRecord: function(o, rc, run, md) {
        delConfirm.show(o, 110, '记录', { 'rc': rc, 'run': run, 'md': md }, SoDaoRecord.DelRecordCallBack);
        return false;
    }
,
    replayComment: function(o, r2un, r2uni, link) {
        var p = $(o).parent().parent().parent();
        p.children('li:last input[type="submit"]').attr('r2un', r2un).attr('r2uni', r2uni).attr('rid', $(o).parent().parent().attr('rid'));
        var input = $('.com-input', p.children('li:last').children('.comment-box'));
        var button = $('.buttoms', p.children('li:last').children('.comment-box'));
        input.focus();
        if (SoDaoRecord.checkLogin('cmt', input[0])) { SoDaoRecord.commentInputBoxClick(input[0]); }
        //input.click();
        input.val("回复" + r2uni + "：");
        button.attr('r2un', r2un);
        button.attr('r2uni', r2uni);
    }
,
    commentInputBoxClick: function(o) {
        if ($.trim($(o).val()) == '我也说一句...') {
            $(o).val('');
        }
        $('li[tid]').removeClass('input-show').addClass('input-normal');
        //alert( $(o).parents("li[tid]").hasClass("input-normal"));
        $(o).parents("li[tid]").addClass("input-show").removeClass("input-normal");
        return;
    }
,
    commentBoxInit: function(c) {
        $(".comment-expression a", $('.movement-comment[id="comment_' + c + '"]')).each(function() {
            var re = /expr(\d+)/ig;
            var o = $('img', $(this));
            if (o.length == 0)
                return;
            var str = o.attr('src');
            var arr = str.match(re);
            var n = RegExp.$1;
            $(this).attr('href', '#').bind('click', { "n": n, "c": c }, function(event) { SoDaoRecord.commentInputClickFace(event.data.n, event.data.c); return false; });
        });


        $(".comment-expression a[class='sel-down']", $('#comment_' + c)).click(function() {
            var cc = $(this).attr('cid');
            var expBox = $('.expression-box').clone();
            $('.expression-box[cid]').remove();
            var ooffset = $(this).offset();
            expBox.attr('cid', cc);
            expBox.appendTo('body');
            expBox.show();
            expBox.css('top', ooffset.top - 10);
            expBox.css('left', ooffset.left - 200);
            $("td a", expBox).each(function() {
                var re = /\/(\d+).gif/ig;
                var o = $('img', $(this));
                if (o.length == 0)
                    return;
                var str = o.attr('src');
                var arr = str.match(re);
                var n = RegExp.$1;
                $(this).attr('href', '#').bind('click', { "n": n, "c": c }, function(event) { SoDaoRecord.commentInputClickFace(event.data.n, event.data.c); $(this).parents(".expression-box").hide(); return false; });
            });
            return false;
        });

        $('.movement-comment[id^=comment]  li[tid]').mouseover(function() {
            SoDaoRecord.bFlagInCommentPublisher = true;
        }).mouseout(function() {
            SoDaoRecord.bFlagInCommentPublisher = false;
        });
    }
,
    InitPublisherFace: function() {
        $(".expression a").each(function() {
            var re = /expr(\d+)/ig;
            var o = $('img', $(this));
            if (o.length == 0)
                return;
            var str = o.attr('src');
            var arr = str.match(re);
            var n = RegExp.$1;
            $(this).attr('href', '#').bind('click', n, function(event) { SoDaoRecord.recordInputClickFace(event.data); $(".expression-box").hide(); return false; });
        });

        $(".expression-list td a").each(function() {
            var re = /\/(\d+).gif/ig;
            var o = $('img', $(this));
            if (o.length == 0)
                return;
            var str = o.attr('src');
            var arr = str.match(re);
            var n = RegExp.$1;
            $(this).attr('href', '#').bind('click', n, function(event) { SoDaoRecord.recordInputClickFace(event.data); $(".expression-box").hide(); $(".expression-box").hide(); return false; });
        });

        $(".expression a[class='sel-down']").attr('href', '#').bind('click', function() { SoDaoRecord.ShowPublisherExpressionBox(); });
    }
,
    ShowPublisherExpressionBox: function() {
        $(".expression-box").show();
        SoDaoRecord.bFlagInFaceBoard = true;
    }
,
    recordInputClickFace: function(i) {
        var o = $('#RecordContent');
        o.val(o.val() + "(:" + i + ")");
        o.focus();
    }
,
    commentInputClickFace: function(i, c) {
        var o = $('.com-input', $('.movement-comment[id="comment_' + c + '"]'));
        o.val(o.val() + '(:' + i + ')');
        o.focus();
    }
,
    GetMyRecordList: function(p) {
        var random = Math.random();
        $.post("GetMyRecordData", { pg: p, rn: random }, function(d) {
            sDialog.hideLoading();
            $(".record-list ul").html(d);
        });
    }
,
    GetMyFriendRecordList: function(p) {
        var random = Math.random();
        $.post("GetMyFriendData", { pg: p, rn: random }, function(d) {
            sDialog.hideLoading();
            $(".record-list ul").html(d);
        });
    }
,
    GetAllRecordList: function(p) {
        var random = Math.random();
        $.post("GetAllRecord", { pg: p, rn: random }, function(d) {
            sDialog.hideLoading();
            $(".record-list ul").html(d);
        });
    }
,
    AddARecord: function(o) {
        var recordContent = $('#RecordContent').val();
        if (recordContent.length == 0) {
            sDialog.info("请输入记录内容！", "提示", 100, 20, 1500);
            return;
        }
        var random = Math.random();
        $.post("/ajax/AddARecord", { rc: recordContent, rd: random }, function(d) {
            if (d.d == 1) {
                sDialog.info("分享记录成功！", "成功提示", 100, 20, 1500);
                $('.publisher-show > .publisher_tools > p').html(d.c);
                if(d.point > 0 || d.expr > 0)
                    sDialog.activePoint(d.point,d.expr,'分享奖励',function(){
                        if(d.IsUpLevel)
                            sDialog.activeLevel(1,'升级提示');
                    });
                if (SoDaoRecord.refresh) {
                    pageClick(1);
                }
            }
            else
                sDialog.info("分享记录失败，请稍候再试！", "提示", 100, 20, 1500);
        }, 'json');
        $('#recordInputHelper').slideUp("fast");
        $('#RecordContent').val('');
    },
    HideAddRecordHelper: function() {
        $('#recordInputHelper').hide();
    }
,
    bFlagInPublisher: false,
    bFlagInCommentPublisher: false,
    bFlagInFaceBoard: false,
    PublisherStartUp: function() {
        $('.publisher').mouseover(function() {
            SoDaoRecord.bFlagInPublisher = true;
        });
        $('.publisher').mouseout(function() {
            SoDaoRecord.bFlagInPublisher = false;
        });

        $(".expression-box").mouseenter(function() {
            SoDaoRecord.bFlagInFaceBoard = true;
        });

        $(".expression-box").mouseleave(function() {
            SoDaoRecord.bFlagInFaceBoard = false;
        });

        $("body").bind("click", function(e) {
            if (!SoDaoRecord.bFlagInPublisher) {
                $('#recordInputHelper').slideUp("fast");
                $(".expression-box").hide();
            }
            if (!SoDaoRecord.bFlagInCommentPublisher && e.target.innerHTML != '回复') {
                if (window.location.href.toLowerCase().indexOf('/record/') > 0) {
                    $('li[tid]').removeClass('input-show').addClass('input-normal');
                    $('.expression-box[cid]').remove();
                    $('.com-input').each(function() {
                        if ($.trim($(this).val()) == '')
                            $(this).val('我也说一句...');
                    });
                }
            }
            if (!SoDaoRecord.bFlagInFaceBoard) {
                $(".expression-box").hide();
                SoDaoRecord.bFlagInFaceBoard = false;
            }
        });
    }
,
    checkLogin: function(cmd, obj) {
        if (SoDaoRecord.visitUser == "") {
            if (obj) { obj.blur(); }
            sDialog.loadIFrame("/User/LoginWindows", "用户登陆", 442, 358, true, null, "", true, "SoDaoRecord.loginCallback", "{cmd:'" + cmd + "'}", "http://www.sodao.com/user/loginCallback");
            return false;
        }
        return true;
    }
,
    visitUser: '',
    PageRefresh: false
,
    loginCallback: function(_wid, _user, _options) {
        refresh = true;
        SoDaoRecord.visitUser = _user;
        sDialog.closef(_wid);
        if (_options) {
            if (_options.cmd == "cmt") {
                document.execCommand('Refresh');
            }
        }
    }
}