	var paginate = function (g, c, a) {
    var d = "",
        b = c - 1,
        h = c + 1;
    a = a || 10;
    var m = Math.ceil(a/2);

    d += '<div class="pages"><table border="0" cellpadding="0" cellspacing="0"><tr>';
    d += b < 1 ? '<td><div class="prev">\u4e0a\u4e00\u9875</div></td>' : '<td><div class="prev"><a href="javascript:void(0);" onclick="return ajax_load_comment(' + b + ');">\u4e0a\u4e00\u9875</a></div></td>';

    b = c >m ? c-m : 1;
    
    if ( b>1 && g>a ) d += '<td><div class="pa"><a target="_self" href="javascript:void(0);" onclick="return ajax_load_comment(1);">1</a></div></td>';
    if ( c-1 > m && g>a ) d += '<td><div class="ellipsis">...</div></td>';
    
    if( b > a && g>a ) var bb = b; else var bb = 1;
    if( b > bb && g>a ) bb = b;
    for (var e = bb; e < b + a; e++) {
        if (e > g ) break;
        d += e == c ? '<td><div class="middle">' + e + "</div></td>" : '<td><div class="pa"><a target="_self" href="javascript:void(0);" onclick="return ajax_load_comment(' + e + ');">' + e + "</a></d­iv></td>"
    }

    if ( g-c > m && g>a ) d += '<td><div class="ellipsis">...</div></td>';
    if ( g-c >= m && g>a )  d += '<td><div class="pa"><a target="_self" href="javascript:void(0);" onclick="return ajax_load_comment(' + g + ');">' + g + '</a></div></td>';

    d += h > g ? '<td><div class="next">\u4e0b\u4e00\u9875</div></td>' : '<td><div class="next"><a target="_self" href="javascript:void(0);" onclick="return ajax_load_comment(' + h + ');">\u4e0b\u4e00\u9875</a></div></td>';
    return d + "</tr></table></div>"
};

$(document).ready(function () { /*{/core}*/
   
    $('.c_pwd').each(function () {
        $(this).hide().after($('<input type="password" value="" class="c_pwd" />').focus(function () {
            $(this).hide().prev().show().focus();
        }));
    }).blur(function () {
        var a = $.trim($(this).val()),
            b = $(this).attr('title');
        if (a == b || a.length == 0) {
            $(this).hide().next().show();
        }
    });

    $('.toggle_alt').focus(function (e, c) {
        var a = $.trim($(this).val()),
            b = $(this).attr('title');
        if (a == b) {
            $(this).val('');
        }
    }).blur(function (e, c) {
        var a = $.trim($(this).val()),
            b = $(this).attr('title');
        if (a == '') {
            $(this).val(b)
        };
    });

    $('.comment_write,.comment_reply').each(function () {
        var $w = $(this),
            $t = $w.find('.btn_submit'),
            $g = $w.find('.btn_login'),
            $cl = $('.comment_login'),
            $c = $w.find('.c_content');

        $w.find('.post_type').click(function () {
            var c = $cl.find('input').size() > 0;
            $t.attr('class', 'btn-submit ' + (this.checked ? (c ? 'c_sub3' : 'c_sub2') : 'c_sub1'));
            if (c && this.checked) {
                if ($('.comment_uinfo').toggle().size() == 0) {
                    $cl.toggle();
                }
            } else {
                if ($('.comment_uinfo').show().size() == 0) {
                    $cl.show();
                }
            }
        });

        var q = {};
        $g.click(function (e, v) {/*登录*/
            $t.trigger('click', true);
            return false;
        });

        $t.click(function (e, v) {
            q.action = v ? 'login' : 'comment';
            q.qid = $.trim($('#reply-id').val());
            q.formhash = $('#ipt-formhash').val();

            var a, c = 'subject',
                u = window.location.pathname,
                m = u.match(/^\/(album|singer)\/[0-9a-z]{2,2}\/(album|singer)_(\d+)\.html/i);
            if (m) {
                a = m[3];
                c = m[1] == 'player' ? 'song' : m[1];
            } else if (typeof Instance !== 'undefined') {
                a = Instance.get('song-id');
                c = 'song';
            } else {
                a = window.comment_id || 0;
            }
            if (!a) {
                return false;
            }
            q.id = a;
            q.act = c;

            if (!$w.find('.post_type').attr('checked') && $('.comment_uinfo').size() == 0) {
                var $u = $w.find('.c_user'),
                    $p = $w.find('.c_pwd');
                var u = $.trim($u.val()),
                    p = $.trim($p.val());
                if (u.length < 2 || u == $u.attr('title')) {
                    alert('您没有输入用户名或不合法');
                    $u.focus();
                    return false;
                }

                if (p.length < 1 || p == $p.attr('title')) {
                    alert('您没有输入密码或不合法');
                    $p.focus();
                    return false;
                }

                q.username = u;
                q.password = p;
            }

            if (!$w.find('.post_type').attr('checked')) {
                q.anonymous = 0;
            } else { /* 匿名*/
                q.anonymous = 1;
            }

            /* input */
            if (!v) {
                var c = $.trim($c.val());
                if (c.length < 2 || c == $c.attr('title')) {
                    alert('您没有输入评论内容或不合法');
                    $c.focus();
                    return false;
                }
                q.content = c;
            }

            $.post('/post.php', q, function (d) {
                if (d.error == 0) {
                    if (q.username && $cl.size() > 0 ) {
                        window.authHtml(d.username);
                    }
                    if (q.action == 'comment') {
                        if (d.quotes) var quotes = '<li class="comment_qr">' + d.quotes + '</li>';
                        else var quotes = '';

                        $('#comment-list .comment_list:first').before('<div class="comment_list h" id="comment-list-' + d.reviewid + '"><div class="comment_pic" id="comment-pic-' + d.reviewid + '"><img src="' + d.logo + '" width="48" height="48" /></div><ul class="comment_content" id="comment-content-' + d.reviewid + '"><li class="comment_info"><span>' + d.floor + '楼</span><span>(' + d.time + ')</span>' + d.username + '</li>' + quotes + '<li class="comment_text" id="comment-text-' + d.reviewid + '">' + d.content + '</li><li class="comment_do" id="comment-do-' + d.reviewid + '"></li><li id="comment-remind-' + d.reviewid + '"></li></ul></div>');

                        $('#comment-list')[0].scrollIntoView();
                    }

                }
                else if(d.error == 5) {
                    $('.c_content toggle_alt').val('');
                    alert('您的评论已经成功提交,请等我们管理员审查.');
                }
                else{
                     alert(d.message);
                }
            },
            'json');

            return false;
        });
    });

    $('#comment-list').bind('ajax', function () {

       $('#comment-loading').hide();
        $('a.support,a.against').one('click', function (e) {
            var rid = this.id.replace(/[a-z_\-]+/ig, ''),
                that = this,
                dtype = $(this).attr('class'),
                $r = $('#comment-remind-' + rid).attr('class', 'comment_remind1').html('正在连接...').show();
            $.post("/post.php", {
                'uid': 1,
                'act': act,
                'action': 'ding',
                'id': rid,
                'dtype': dtype
            },
            function (data) {
                if (data.error == 0) {
                    $r.attr('class', 'comment_remind2').html('谢谢您的参与！').hide('slow');
                    $('#ding_' + data.stype + '-' + data.id).html(parseInt($('#ding_' + data.stype + '-' + data.id).html()) + 1);
                    window.setTimeout(function () {
                        $r.attr('class', 'comment_remind2').html('谢谢您的参与！').hide('slow');
                    },
                    2000);
                } else if (data.error = 1) {
                    $r.attr('class', 'comment_remind2').html('您已经参与过了！').hide('slow');
                    window.setTimeout(function () {
                        $r.attr('class', 'comment_remind2').html('您已经参与过了！').hide('slow');
                    },
                    2000);
                    return false;
                } else {
                    alert(data.message);
                    return false;
                }
            },
            "json");
            return false;
        });

        $('a.reply').click(function () {
            var id = this.id.replace(/[a-z_\-]+/ig, '');
            $('#comment-content-' + id).append($('#reply-form'));
            $('#reply-id').val(id);
            return false;
        });

        $('a.report').click(function () {
            var mid = this.id.replace(/[a-z_\-]+/ig, '');
            window.tobox('http://pinglun.1ting.com/report.php?act='+act+'&id='+mid,'_box');
            return false;
        });

        $('#comment-pages a').click(function () {
            var a = $(this).attr('href').match(/(\d+)$/);
            window.setTimeout(function () {
                ajax_load_comment(Instance.get('song-id'), a[1] || 1, 1);
            },
            10);
            return false;
        });
    });

    window.ajax_load_comment = function (b) {
        var a, c = 'subject',
            b = b || 1,
            u = window.location.pathname,
            m = u.match(/^\/(album|singer)\/[0-9a-z]{2,2}\/(album|singer)_(\d+)\.html/i);
        if (m) {
            a = m[3];
            c = m[1] == 'player' ? 'song' : m[1];
        } else if (typeof Instance !== 'undefined') {
            a = Instance.get('song-id');
            c = 'song';
        } else {
            a = window.comment_id || 0;
        }
        if (!a) {
            return false;
        }

        window.setTimeout(function () {
            $.getJSON('http://pinglun.1ting.com/comment.show.php?callback=?&id=' + a + '&page=' + b + '&type=' + c, function (data) {
                if (data.error == 1) {
                    alert(data.message);
                    return false;
                } else if( data.snum > 0 ) {
                    var a = data.items,
                        c = [];
                    $.each(a, function (i, item) {
                        var reviewsID = a[i].reviewsID;

                        if (a[i].quotes) var quotes = '<li class="comment_qr">' + a[i].quotes + '</li>';
                        else var quotes = '';

                        c.push('<div class="comment_list h" id="comment-list-' + reviewsID + '"><div class="comment_pic" id="comment-pic-' + reviewsID + '"><a href="http://box.1ting.com/space.php?uid='+a[i].membersID+'" target="_target"><img src="' + a[i].logo + '" width="48" height="48" /></a></div><ul class="comment_content" id="comment-content-' + reviewsID + '"><li class="comment_info"><span><a href="#" target="_self" class="report" id="report-' + reviewsID + '">举报</a></span><span>' + a[i].floor + '楼</span><span>(' + a[i].createDate + ')</span>' + a[i].username + '</li>' + quotes + '<li class="comment_text" id="comment-text-' + reviewsID + '">' + a[i].content + '</li><li class="comment_do" id="comment-do-' + reviewsID + '"><span class="comment_d2"><a href="#against" target="_self" class="against" id="against-' + reviewsID + '">反对[-<span id="ding_against-' + reviewsID + '">' + a[i].bads + '</span>]</a></span><span class="comment_d1"><a href="#support" target="_self" class="support" id="support-' + reviewsID + '">支持[+<span id="ding_support-' + reviewsID + '">' + a[i].goods + '</span>]</a></span><span class="comment_d3"><a href="#reply" class="reply" id="reply-' + reviewsID + '">回复</a></span></li><li id="comment-remind-' + reviewsID + '"></li></ul></div>');
                    });
                    
                    $('#comment-list').html(c.join('')).append(paginate(Math.ceil(data.snum/20), b));
                    $('#comment-list').trigger('ajax');
                } else if( data.snum == 0 ) {
                    $('#comment-list').html('<div class="comment_list"><div class="comment_pic"><img src="http://img.1ting.com/box/v5/images/master.png" width="48" height="48" alt="" /></div><ul class="comment_content"><li class="comment_info"><b>一听管理员：</b></li><li class="comment_text">暂时还没人评论，第一个就等您了！</li></ul></div>');
                    $('#comment-list').trigger('ajax');
                }
            });
        },100);
        return false;
    };

    if (typeof Instance !== 'undefined') {
        Instance.hook('after_start', function (t) {
            window.setTimeout(function () {
                ajax_load_comment();
            },
            100);
        });
    }
    ajax_load_comment();
    /*{/core}*/
});
