var SoDaoComment = {
    inCommentPublishing: false,
    sending: false,
    commentAction:
    {
        comm: { add: "/Comment/AddComment", del: "/Comment/DeleteComment" }

        //share: { add: "/Comment/AddShareComment", del: "/Comment/DeleteComment" },
        //guestbook: { add: "/Comment/AddGuestBookComment", del: "/Comment/DeleteComment" }
    },
    //    getCommentAction: function(cmttype) {
    //        if (cmttype == "share")
    //            return this.commentAction.share;
    //        if (cmttype == "guestbook")
    //            return this.commentAction.guestbook;
    //        return this.commentAction.comm;
    //    },
    loadComment: function(c, container, cmttype, ou) {
        var random = Math.random();
        $.get("/Comment/List", { oc: c, ou: ou, cmtt: cmttype, rn: random }, function(d) {
            container.append(d);
            var input = $('.com-input', $("#comment_" + c));
            SoDaoExpressionBox.init(input, function(face) {
                sodaoCheck._word(input.get(0), 'curCmtWord_' + c);
            });
        });
        $("body").unbind("click");
        $("body").bind("click", function() {
            if (!SoDaoComment.inCommentPublishing)
                $(".input-show").each(function() {
                    var input = $('.com-input', $(this));
                    if (input.val().trim() == "")
                        $(this).removeClass("input-show").addClass("input-normal");
                });
        });
    },
    submitComment: function(event, smbbtn) {
        var event = window.event ? window.event : event;
        if (event.keyCode == 13 && !this.sending) { smbbtn.click(); };
    },
    addComment: function(o, oc, md, cmttype, rtp, addCallback, ou, ouc) {
        var content = $(o).parents(".comment-box").children(".com-input").val();

        if (content.length == 0) {
            sDialog.info("请输入评论内容！", "提示", 100, 20, 1500);
            return;
        }
        var rootid = $(o).parents("li[rid]").attr('rid');
        if (typeof (rootid) == 'undefined') { 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 = ""; }
        if (typeof (ou) == 'undefined') { ou = ""; }
        if (typeof (ouc) == 'undefined') { ouc = ""; }
        var random = Math.random();
        var action = SoDaoComment.commentAction.comm;
        this.sending = true;
        $(o).attr('disabled', 'disabled');

        if (!content.startWith('回复.{1,20}：?')) {
            r2un = "";
            r2uni = "";
            rootid = "";
        } else if (rootid == "") {
            r2un = "";
            r2uni = "";
        }

        $.post(action.add, { "c": content, "rid": rootid, "r2un": r2un, "r2uni": r2uni, 'oc': oc, 'ou': ou, 'ouc': ouc, cmtt: cmttype, 'md': md, "rtp": rtp, rd: random }, function(d) {
            var container = $('#comment_' + oc);
            container.html("");
            container.append(d);
            //SoDaoComment.commentBoxInit(oc);
            var input = $('.com-input', container);
            //var subbtn = $(".buttoms", container);
            $(o).removeAttr('disabled');
            SoDaoExpressionBox.init(input);
            SoDaoComment.sending = false;
            SoDaoComment.postZoneCount(cmttype, ouc, oc, ou, 1); //update zone
            if (addCallback) { addCallback(d); }
        });
    },
    postZoneCount: function(cmtp, ocmtp, oc, ou, added) {
        if (cmtp.toLowerCase() == 'zone') {
            $.post("/zone/" + ou + "/UpdateCommentCount", { "c": oc, "ot": ocmtp, "i": added, rd: Math.random() }, function(d) { });
        }
    },
    deleteComment: function(o, oc, cc, md, cmttype, cmtu) {
        sDialog.loading("数据提交中。。。");
        var random = Math.random();
        var action = SoDaoComment.commentAction.comm;
        var ajax = {
            url: action.del, data: { "oc": oc, "cc": cc, "md": md, "cmtp": cmttype, "cmtu": cmtu, rd: random }, type: 'POST', dataType: 'json', cache: false,
            success: function(json, statusText) {
                sDialog.hideLoading();
                if (json.status == "1") {
                    $(o).parent('li[rid]').slideUp("fast", function() { $(this).remove(); });
                    if (cmttype.toLowerCase() == 'zone') {
                        SoDaoComment.postZoneCount(cmttype, json.ownerCmtType, oc, json.ownerId, 1); //update zone
                    }
                }
                else {
                    sDialog.error("错误信息", '评论删除失败，请稍候再试！', 300, 50);
                }
            }
        };
        $.ajax(ajax);
    },
    replayComment: function(o, r2un, r2uni, link) {
        var p = $(o).parents('li[rid]').parent();
        p.children('li:last input[type="submit"]').attr('r2un', r2un).attr('r2uni', r2uni);
        var input = $('.com-input', p.children('li:last').children('.comment-box'));
        var button = $('.buttoms', p.children('li:last').children('.comment-box'));
        input.focus();
        var val = input.val();
        if (val.startWith('回复.{1,20}：?')) {
            var reg = new RegExp('^回复.{1,20}：', "ig");
            input.val(val.replace(reg, '回复' + r2uni + '：'));
        } else {
            input.val('回复' + r2uni + '：' + val);
        }
        //input.val("回复"+r2uni+"：");
        button.attr('r2un', r2un);
        button.attr('r2uni', r2uni);
    },
    commentInputBoxClick: function(o) {
        if ($.trim($(o).val()) == '添加回复...') {
            $(o).val('');
        }
        $(".input-show").each(function() {
            var input = $('.com-input', $(this));
            if (input.val().trim() == "")
                $(this).removeClass("input-show").addClass("input-normal");
        });
        var inputDiv = $(o).parents(".input-normal");
        inputDiv.removeClass("input-normal").addClass("input-show");
        $(".comWrap").scrollTop(10000);
        this.inCommentPublishing = true;
        var input = $('.com-input', inputDiv);
        inputDiv.mousemove(function() { SoDaoComment.inCommentPublishing = true; });
        inputDiv.bind("mouseleave", function() { if (input.val().trim() == "") SoDaoComment.inCommentPublishing = false; });
    },
    doSomething: function(o) {
        return (function() { $(o).parents(".input-show").removeClass("input-show").addClass("input-normal"); });
    },
    commentBoxMouseOut: function(o) {
        if ($.trim($(o).children('input:first').val()) == '') {
            $(o).children('input:first').val('添加回复...');
        }
        var functRef = SoDaoComment.doSomething(o);
        setTimeout(functRef, 200);
    }
}
var SoDaoGuestBook = {
    sending: false,
    add: function(btn, form, callback) {
        sDialog.loading("留言提交中...");
        this.sending = true;
        var ajax = {
            url: form.attr('action'), data: form.serialize(), type: 'POST', dataType: 'json', cache: false,
            success: function(json, statusText) {
                sDialog.hideLoading();
                SoDaoGuestBook.sending = false;
                $(btn).removeAttr('disabled');
                if (json.text == "ok") {
                    if (callback) { callback(btn, json); }
                    if (json.point > 0 || json.exp > 0) {
                        sDialog.activePoint(json.point, json.exp, "留言加分", function() { if (json.uplevel) { sDialog.activeLevel(1, "用户升级"); } });
                    }                    
                } else {
                    sDialog.error(json.text, "留言错误", 300, 50);
                }
            }
        };
        $.ajax(ajax);
    },
    del: function(obj, options) {
        sDialog.loading("删除处理中...");
        var ajax = {
            url: "/comment/delGuestBook", data: options, type: 'GET', dataType: 'json', cache: false,
            success: function(json, statusText) {
                sDialog.hideLoading();
                if (json.text == "ok") {
                    $(obj).parent().slideUp("fast", function() {
                        var _liInput = $(this).find(_cmtInput.liCmtInput());
                        if (_liInput.attr('id')) {
                            _liInput.appendTo(document.body);
                        }
                        $(this).remove();                                                
                    });
                }
                else {
                    sDialog.error(json.text, "错误提示", 300, 50);
                }
            }
        };
        $.ajax(ajax);
    }
}