﻿var comment = {};
comment.GetContainer = function(itemId, itemType) {
    return $(['comment_', itemId, '_', itemType].join(''));
};
comment.GetAllItemComments = function(uid, itemId, itemType, t) {
    if(typeof t=='undefined' || !t)t=0;
    new $Ajax.Request('/Ajax/Comment/GetItemAllComments.aspx', {
        method: 'post',
        parameters: { uid: uid, id: itemId, type: itemType, t: t },
        onComplete: function(transport) {
            var dom = comment.GetContainer(itemId, itemType);
            if (dom) dom.update(transport.responseText);
            var trigger = $(['showcomment_', itemId, '_', itemType].join(''));
            Event.stopObserving(trigger, 'click');

            if (!trigger.oCount) {
                var myregexp = /\[(\d+)\]/im;
                var match = myregexp.exec(trigger.innerHTML);
                if (match != null) {
                    trigger.oCount = parseInt(match[1]);
                    if (trigger.oCount < 0) trigger.oCount = 0;
                } else {
                    trigger.oCount = 0;
                }
            }
            trigger.oHTML = '评论[' + trigger.oCount + ']';
            trigger.nHTML = '收起评论';
            trigger.innerHTML = trigger.nHTML;
            function toggle(event) {
                if (dom.style.display == 'none') {
                    trigger.innerHTML = trigger.nHTML;
                    dom.show();
                }
                else {
                    trigger.innerHTML = trigger.oHTML;
                    dom.hide();
                }
            }
            trigger.onclick = null;
            trigger.observe('click', toggle);
            trigger.href='javascript:void(0);';

            //BindWaterMark(['addcomment_', itemId, '_', itemType].join(''), '添加评论……');
            BindClose(dom);
            ShrinkBox('expandbox_' + itemId + '_' + itemType, '添加评论……', function(event, dom) {
                comment.AddComment(dom.ContentField.value, uid, itemId, itemType);
            }, '发表');
        }
    });
};
comment.GetAllItemCommentsOfDefault = function(uid, itemId, itemType, t) {
    if (typeof t == 'undefined' || !t) t = 0;
    new $Ajax.Request('/Ajax/Comment/GetItemAllCommentsOfDefault.aspx', {
        method: 'post',
        parameters: { uid: uid, id: itemId, type: itemType, t: t },
        onComplete: function(transport) {
            var dom = comment.GetContainer(itemId, itemType);
            if (dom) dom.update(transport.responseText);
            var trigger = $(['showcomment_', itemId, '_', itemType].join(''));
            Event.stopObserving(trigger, 'click');

            if (!trigger.oCount) {
                var myregexp = /\[(\d+)\]/im;
                var match = myregexp.exec(trigger.innerHTML);
                if (match != null) {
                    trigger.oCount = parseInt(match[1]);
                    if (trigger.oCount < 0) trigger.oCount = 0;
                } else {
                    trigger.oCount = 0;
                }
            }
            trigger.oHTML = '评论[' + trigger.oCount + ']';
            trigger.nHTML = '收起评论';
            trigger.innerHTML = trigger.nHTML;
            function toggle(event) {
                if (dom.style.display == 'none') {
                    trigger.innerHTML = trigger.nHTML;
                    dom.show();
                }
                else {
                    trigger.innerHTML = trigger.oHTML;
                    dom.hide();
                }
            }
            trigger.onclick = null;
            trigger.observe('click', toggle);
            trigger.href = 'javascript:void(0);';

            //BindWaterMark(['addcomment_', itemId, '_', itemType].join(''), '添加评论……');
            BindClose(dom);
            ShrinkBox('expandbox_' + itemId + '_' + itemType, '添加评论……', function(event, dom) {
                comment.AddComment(dom.ContentField.value, uid, itemId, itemType);
            }, '发表');
        }
    });
};
comment.GetAllItemCommentsForDefault = function(uid, itemId, itemType, t, microbourl) {
    if (typeof t == 'undefined' || !t) t = 0;
    if (typeof microbourl == 'undefined' || !microbourl) microbourl = "";
    
    new $Ajax.Request('/Ajax/Comment/GetItemAllCommentsForDefault.aspx', {
        method: 'post',
        parameters: { uid: uid, id: itemId, type: itemType, t: t, url: microbourl },
        onComplete: function(transport) {
            var dom = comment.GetContainer(itemId, itemType);
            if (dom) dom.update(transport.responseText);
            var trigger = $(['showcomment_', itemId, '_', itemType].join(''));
            Event.stopObserving(trigger, 'click');

            if (!trigger.oCount) {
                var myregexp = /\[(\d+)\]/im;
                var match = myregexp.exec(trigger.innerHTML);
                if (match != null) {
                    trigger.oCount = parseInt(match[1]);
                    if (trigger.oCount < 0) trigger.oCount = 0;
                } else {
                    trigger.oCount = 0;
                }
            }
            trigger.oHTML = '评论[' + trigger.oCount + ']';
            trigger.nHTML = '收起评论';
            trigger.innerHTML = trigger.nHTML;
            function toggle(event) {
                if (dom.style.display == 'none') {
                    trigger.innerHTML = trigger.nHTML;
                    dom.show();
                }
                else {
                    trigger.innerHTML = trigger.oHTML;
                    dom.hide();
                }
            }
            trigger.onclick = null;
            trigger.observe('click', toggle);
            trigger.href = 'javascript:void(0);';

            //BindWaterMark(['addcomment_', itemId, '_', itemType].join(''), '添加评论……');
            BindClose(dom);
            ShrinkBoxForDefault('expandbox_' + itemId + '_' + itemType, '添加评论……', function(event, dom) {
            comment.AddCommentForDefault(dom.ContentField.value, uid, itemId, itemType, null, 10, microbourl);
            }, '发表');
        }
    });
};

comment.GetItemComments = function(uid, itemId, itemType) {

    var pageIndex = 1;
    if (location.hash && location.hash != '') {
        var myregexp = /\#cp(\d+)/m;
        var match = myregexp.exec(location.hash);
        if (match != null) {
            pageIndex = match[1];
        } else {
            pageIndex = 1;
        }
    }
    //if(pageIndex!=1)
    //    location.hash='#cp' + pageIndex;
    new $Ajax.Request('/Ajax/Comment/GetItemComments.aspx', {
        method: 'post',
        parameters: { uid: uid, id: itemId, type: itemType, page: pageIndex },
        onComplete: function(transport) {
            var dom = comment.GetContainer(itemId, itemType);
            if (dom) {
                dom.update(transport.responseText);
                dom.getElementsBySelector('textarea').each(function(s) {
                    dom.ContentField = s;
                    dom.Submit = function() {
                        if(s.value!=s.watermark && !s.value.match(/^[\s　]*$/i)){
                            comment.AddComment(s.value, uid, itemId, itemType,
                                function(transport) {
                                    if (transport.responseText == 'ok') {
                                        comment.GetItemComments(uid, itemId, itemType, 1);
                                    }
                                }
                            );
                        }
                    };
                    BindWaterMark(s, '发表评论');
                    BindContentBox(dom.ContentField, dom.Submit, false);
                    if($('addcomment'))$('addcomment').onclick = dom.Submit;
                });
                BindClose(dom);
                if (dom.hashMonitor) {
                    clearInterval(dom.hashMonitor);
                }
                if(pageIndex!=1)
                    dom.hash = '#cp' + pageIndex;
                dom.hashMonitor = setInterval(function() {
                    var pi = 1;
                    if (location.hash && location.hash != '') {
                        var myregexp = /\#cp(\d+)/m;
                        var match = myregexp.exec(location.hash);
                        if (match != null) {
                            pi = match[1];
                        } else {
                            pi = 1;
                        }
                    }
                    if (location.hash != dom.hash && pageIndex!=pi) {
                        comment.GetItemComments(uid, itemId, itemType);
                        pageIndex=pi;
                        dom.hash = '#cp' + pageIndex;
                    }
                }, 50);
            }
        }
    });
};


comment.GetCombinedItemComments = function(ilist) {
    new $Ajax.Request('/Ajax/Comment/GetMultipleItemCommentsCombined.aspx', {
        method: 'post',
        parameters: 'i=' + ilist.plist.join('&i='),
        onComplete: function(transport) {
            clist = transport.responseText.split('<--##-->');
            var i = 0;
            $(ilist).each(
                function(s) {
                    var dom = comment.GetContainer(s.iid, s.it);
                    var itemId = s.iid;
                    var itemType = s.it;
                    var uid = s.uid;

                    if (dom) dom.update(clist[i]);
                    if (dom.getElementsBySelector('.removed').length > 0 && $('expandbox_' + itemId + '_' + itemType)) {
                        $($('expandbox_' + itemId + '_' + itemType).parentNode).remove();
                    } 
                    else {
                        BindClose(dom);
                        if (dom.getElementsBySelector('.noprivacy').length > 0 && $('expandbox_' + itemId + '_' + itemType)) {
                            $('expandbox_' + itemId + '_' + itemType).remove();
                        } 
                        else {
                            var s = function() {
                                ShrinkBox('expandbox_' + itemId + '_' + itemType, '添加评论……', function(event, dom) {
                                    comment.AddComment(dom.ContentField.value, uid, itemId, itemType, function(transport) {
                                        s();
                                        comment.GetAllItemCommentsOfDefault(uid, itemId, itemType, 3);
                                    }, 3);
                                }, '发表');
                            }
                            s();
                        }
                    }
                    i++;
                }
            );
        }
    });
};

comment.SetReply=function(uid, uname, itemId, itemType, c, t){
    var dom = comment.GetContainer(itemId, itemType);
    dom.replyTo={uname: uname, c: c, flag: '回复'+uname+'：'};
    //var eb=$('expandbox_' + itemId + '_' + itemType);
    ExpandBox('expandbox_' + itemId + '_' + itemType, '添加评论……', function(event, dom) {
        comment.AddComment(dom.ContentField.value, uid, itemId, itemType, null, t);
    }, '发表');
    if(dom.getElementsBySelector('textarea').length>0){
        dom.getElementsBySelector('textarea').each(function(s) {
            dom.ContentField = s;
            s.focus();
            setTimeout(function(){
            s.value='回复'+uname+'：';
            }, 1);
        });
    }else{
        if($('expandbox_' + itemId + '_' + itemType))
            $('expandbox_' + itemId + '_' + itemType).getElementsBySelector('textarea').each(function(s) {
                dom.ContentField = s;
                s.focus();
                setTimeout(function(){
                s.value='回复'+uname+'：';
                }, 1);
            });
    }
};
comment.SetReplyOfDefault = function(uid, uname, itemId, itemType, c, t) {
    var dom = comment.GetContainer(itemId, itemType);
    dom.replyTo = { uname: uname, c: c, flag: '回复' + uname + '：' };
    //var eb=$('expandbox_' + itemId + '_' + itemType);
    ExpandBox('expandbox_' + itemId + '_' + itemType, '添加评论……', function(event, dom) {
        comment.AddCommentOfDefault(dom.ContentField.value, uid, itemId, itemType, null, t);
    }, '发表');
    if (dom.getElementsBySelector('textarea').length > 0) {
        dom.getElementsBySelector('textarea').each(function(s) {
            dom.ContentField = s;
            s.focus();
            setTimeout(function() {
                s.value = '回复' + uname + '：';
            }, 1);
        });
    } else {
        if ($('expandbox_' + itemId + '_' + itemType))
            $('expandbox_' + itemId + '_' + itemType).getElementsBySelector('textarea').each(function(s) {
                dom.ContentField = s;
                s.focus();
                setTimeout(function() {
                    s.value = '回复' + uname + '：';
                }, 1);
            });
    }
};

comment.SetReplyForDefault = function(uid, uname, itemId, itemType, c, t) {
    var dom = comment.GetContainer(itemId, itemType);
    dom.replyTo = { uname: uname, c: c, flag: '回复' + uname + '：' };
    //var eb=$('expandbox_' + itemId + '_' + itemType);
    ExpandBoxForDefault('expandbox_' + itemId + '_' + itemType, '添加评论……', function(event, dom) {
        comment.AddCommentForDefault(dom.ContentField.value, uid, itemId, itemType, null, t);
    }, '发表');
    if (dom.getElementsBySelector('textarea').length > 0) {
        dom.getElementsBySelector('textarea').each(function(s) {
            dom.ContentField = s;
            s.focus();
            setTimeout(function() {
                s.value = '回复' + uname + '：';
            }, 1);
        });
    } else {
        if ($('expandbox_' + itemId + '_' + itemType))
            $('expandbox_' + itemId + '_' + itemType).getElementsBySelector('textarea').each(function(s) {
                dom.ContentField = s;
                s.focus();
                setTimeout(function() {
                    s.value = '回复' + uname + '：';
                }, 1);
            });
    }
};

comment.AddComment = function(content, uid, itemId, itemType, onComplete, t) {
    if (!content.match(/^[\s　]*$/i) && !IsSensitive(content)) {
        var dom = comment.GetContainer(itemId, itemType);

        ShrinkBox('expandbox_' + itemId + '_' + itemType, '添加评论……', function(event, dom) {
            comment.AddComment(dom.ContentField.value, uid, itemId, itemType, function(transport) {
                s();
                comment.GetAllItemComments(uid, itemId, itemType, 3);
            }, 3);
        }, '发表');


        var r = (dom && dom.replyTo && content.indexOf(dom.replyTo.flag) == 0) ? true : false;
        new $Ajax.Request(r ? '/Ajax/Comment/AddCommentReply.aspx' : '/Ajax/Comment/AddComment.aspx', {
            method: 'post',
            parameters: r ? { uid: uid, content: content, c: dom.replyTo.c} : { uid: uid, content: content, id: itemId, type: itemType },
            onComplete: function(transport) {
                if (transport.responseText == 'ok') {
                    if (typeof onComplete == 'undefined' || !onComplete) {
                        var trigger = $(['showcomment_', itemId, '_', itemType].join(''));
                        if (trigger) trigger.oCount++;
                        comment.GetAllItemComments(uid, itemId, itemType, t);
                    } else {
                        onComplete(transport);
                    }
                } else if (transport.responseText == 'noprivacy') {
                    alert('您无权评论');
                }
            }
        });
    }
};
comment.AddCommentOfDefault = function(content, uid, itemId, itemType, onComplete, t) {
    if (!content.match(/^[\s　]*$/i) && !IsSensitive(content)) {
        var dom = comment.GetContainer(itemId, itemType);

        ShrinkBox('expandbox_' + itemId + '_' + itemType, '添加评论……', function(event, dom) {
            comment.AddComment(dom.ContentField.value, uid, itemId, itemType, function(transport) {
                s();
                comment.GetAllItemComments(uid, itemId, itemType, 3);
            }, 3);
        }, '发表');


        var r = (dom && dom.replyTo && content.indexOf(dom.replyTo.flag) == 0) ? true : false;
        new $Ajax.Request(r ? '/Ajax/Comment/AddCommentReply.aspx' : '/Ajax/Comment/AddComment.aspx', {
            method: 'post',
            parameters: r ? { uid: uid, content: content, c: dom.replyTo.c} : { uid: uid, content: content, id: itemId, type: itemType },
            onComplete: function(transport) {
                if (transport.responseText == 'ok') {
                    if (typeof onComplete == 'undefined' || !onComplete) {
                        var trigger = $(['showcomment_', itemId, '_', itemType].join(''));
                        if (trigger) trigger.oCount++;
                        comment.GetAllItemCommentsOfDefault(uid, itemId, itemType, t);
                    } else {
                        onComplete(transport);
                    }
                } else if (transport.responseText == 'noprivacy') {
                    alert('您无权评论');
                }
            }
        });
    }
};

comment.AddCommentForDefault = function(content, uid, itemId, itemType, onComplete, t, microbourl) {
    if (!content.match(/^[\s　]*$/i) && !IsSensitive(content)) {
        var dom = comment.GetContainer(itemId, itemType);

        ShrinkBox('expandbox_' + itemId + '_' + itemType, '添加评论……', function(event, dom) {
            comment.AddComment(dom.ContentField.value, uid, itemId, itemType, function(transport) {
            s();
                comment.GetAllItemCommentsForDefault(uid, itemId, itemType, 10,microbourl);
            }, 3);
        }, '发表');


        var r = (dom && dom.replyTo && content.indexOf(dom.replyTo.flag) == 0) ? true : false;
        new $Ajax.Request(r ? '/Ajax/Comment/AddCommentReply.aspx' : '/Ajax/Comment/AddComment.aspx', {
            method: 'post',
            parameters: r ? { uid: uid, content: content, c: dom.replyTo.c} : { uid: uid, content: content, id: itemId, type: itemType },
            onComplete: function(transport) {
                if (transport.responseText == 'ok') {
                    if (typeof onComplete == 'undefined' || !onComplete) {
                        var trigger = $(['showcomment_', itemId, '_', itemType].join(''));
                        if (trigger) trigger.oCount++;
                        comment.GetAllItemCommentsForDefault(uid, itemId, itemType, t,microbourl);
                    } else {
                        onComplete(transport);
                    }
                } else if (transport.responseText == 'noprivacy') {
                    alert('您无权评论');
                }
            }
        });
    }
};
comment.ShowDeleteComment = function(id, uid, itemId, itemType) {
    if ($('popWin_DeleteComment')) {
        $modal('', '', 'yulu_del2');
        $T.UI.ModalPopup.DOM.id = 'popWin';
        $T.UI.ModalPopup.DOM.innerHTML = $('popWin_DeleteComment').innerHTML;
        $T.UI.ModalPopup.DOM.comment = { id: id, uid: uid, itemId: itemId, itemType: itemType };
        Reposition();
        return;
    } else {
        new $Ajax.Request('/Ajax/Comment/DeleteComment.aspx', {
            method: 'post',
            parameters: { show: 1 },
            onComplete: function(transport) {
                $modal('', '', 'yulu_del2');
                $T.UI.ModalPopup.DOM.id = 'popWin';
                $T.UI.ModalPopup.DOM.innerHTML = transport.responseText;
                $T.UI.ModalPopup.DOM.comment = { id: id, uid: uid, itemId: itemId, itemType: itemType };
                Reposition();
            }
        });
    }
};

comment.ShowDeleteCommentForDefault = function(id, uid, itemId, itemType) {
    if ($('popWin_DeleteComment')) {
        $modal('', '');
        $T.UI.ModalPopup.DOM.id = 'popWin';
        $T.UI.ModalPopup.DOM.innerHTML = $('popWin_DeleteComment').innerHTML;
        $T.UI.ModalPopup.DOM.comment = { id: id, uid: uid, itemId: itemId, itemType: itemType };
        Reposition();
        return;
    } else {
        new $Ajax.Request('/Ajax/Comment/DeleteCommentForDefault.aspx', {
            method: 'post',
            parameters: { show: 1 },
            onComplete: function(transport) {
                $modal('', '');
                $T.UI.ModalPopup.DOM.id = 'popWin';
                $T.UI.ModalPopup.DOM.innerHTML = transport.responseText;
                $T.UI.ModalPopup.DOM.comment = { id: id, uid: uid, itemId: itemId, itemType: itemType };
                Reposition();
            }
        });
    }
};

comment.DeleteComment = function(id, uid, itemId, itemType, onComplete) {
    new $Ajax.Request('/Ajax/Comment/DeleteComment.aspx', {
        method: 'post',
        parameters: { uid: uid, id: itemId, type: itemType, c: id },
        onComplete: function(transport) {
            if (transport.responseText == 'ok') {
                if (typeof onComplete == 'undefined' || !onComplete) {
                    var trigger = $(['showcomment_', itemId, '_', itemType].join(''));
                    if(trigger){
                        trigger.oCount--;
                        if (trigger.oCount < 0) trigger.oCount = 0;
                    }
                    comment.GetAllItemComments(uid, itemId, itemType);
                    $modal.close();
                } else {
                    transport.uid=uid;
                    transport.itemId=itemId;
                    transport.itemType=itemType;
                    onComplete(transport);
                }
            }
        }
    });
};

comment.DeleteCommentForDefault = function(id, uid, itemId, itemType, onComplete) {
    new $Ajax.Request('/Ajax/Comment/DeleteComment.aspx', {
        method: 'post',
        parameters: { uid: uid, id: itemId, type: itemType, c: id },
        onComplete: function(transport) {
            if (transport.responseText == 'ok') {
                if (typeof onComplete == 'undefined' || !onComplete) {
                    var trigger = $(['showcomment_', itemId, '_', itemType].join(''));
                    if (trigger) {
                        trigger.oCount--;
                        if (trigger.oCount < 0) trigger.oCount = 0;
                    }
                    comment.GetAllItemCommentsForDefault(uid, itemId, itemType);
                    $modal.close();
                } else {
                    transport.uid = uid;
                    transport.itemId = itemId;
                    transport.itemType = itemType;
                    onComplete(transport);
                }
            }
        }
    });
};

comment.HashPage = function(page) {
    location.hash = '#cp' + page;
};

comment.__callback = function(transport) {
    if (transport.responseText == 'ok') {
        comment.GetItemComments(transport.uid, transport.itemId, transport.itemType);
        $modal.close();
    }
};
