+var getCount, updateCount, updateCountText, updatePending, updateApproved,
+ updateHtmlTitle, updateDashboardText, adminTitle = document.title,
+ isDashboard = $('#dashboard_right_now').length,
+ titleDiv, titleRegEx;
+
+ getCount = function(el) {
+ var n = parseInt( el.html().replace(/[^0-9]+/g, ''), 10 );
+ if ( isNaN(n) ) {
+ return 0;
+ }
+ return n;
+ };
+
+ updateCount = function(el, n) {
+ var n1 = '';
+ if ( isNaN(n) ) {
+ return;
+ }
+ n = n < 1 ? '0' : n.toString();
+ if ( n.length > 3 ) {
+ while ( n.length > 3 ) {
+ n1 = thousandsSeparator + n.substr(n.length - 3) + n1;
+ n = n.substr(0, n.length - 3);
+ }
+ n = n + n1;
+ }
+ el.html(n);
+ };
+
+ updateApproved = function( diff, commentPostId ) {
+ var postSelector = '.post-com-count-' + commentPostId,
+ noClass = 'comment-count-no-comments',
+ approvedClass = 'comment-count-approved',
+ approved,
+ noComments;
+
+ updateCountText( 'span.approved-count', diff );
+
+ if ( ! commentPostId ) {
+ return;
+ }
+
+ // cache selectors to not get dupes
+ approved = $( 'span.' + approvedClass, postSelector );
+ noComments = $( 'span.' + noClass, postSelector );
+
+ approved.each(function() {
+ var a = $(this), n = getCount(a) + diff;
+ if ( n < 1 )
+ n = 0;
+
+ if ( 0 === n ) {
+ a.removeClass( approvedClass ).addClass( noClass );
+ } else {
+ a.addClass( approvedClass ).removeClass( noClass );
+ }
+ updateCount( a, n );
+ });
+
+ noComments.each(function() {
+ var a = $(this);
+ if ( diff > 0 ) {
+ a.removeClass( noClass ).addClass( approvedClass );
+ } else {
+ a.addClass( noClass ).removeClass( approvedClass );
+ }
+ updateCount( a, diff );
+ });
+ };
+
+ updateCountText = function( selector, diff ) {
+ $( selector ).each(function() {
+ var a = $(this), n = getCount(a) + diff;
+ if ( n < 1 ) {
+ n = 0;
+ }
+ updateCount( a, n );
+ });
+ };
+
+ updateDashboardText = function ( response ) {
+ if ( ! isDashboard || ! response || ! response.i18n_comments_text ) {
+ return;
+ }
+
+ var rightNow = $( '#dashboard_right_now' );
+
+ $( '.comment-count a', rightNow ).text( response.i18n_comments_text );
+ $( '.comment-mod-count a', rightNow ).text( response.i18n_moderation_text )
+ .parent()
+ [ response.in_moderation > 0 ? 'removeClass' : 'addClass' ]( 'hidden' );
+ };
+
+ updateHtmlTitle = function ( diff ) {
+ var newTitle, regExMatch, titleCount, commentFrag;
+
+ titleRegEx = titleRegEx || new RegExp( adminCommentsL10n.docTitleCommentsCount.replace( '%s', '\\([0-9' + thousandsSeparator + ']+\\)' ) + '?' );
+ // count funcs operate on a $'d element
+ titleDiv = titleDiv || $( '<div />' );
+ newTitle = adminTitle;
+
+ commentFrag = titleRegEx.exec( document.title );
+ if ( commentFrag ) {
+ commentFrag = commentFrag[0];
+ titleDiv.html( commentFrag );
+ titleCount = getCount( titleDiv ) + diff;
+ } else {
+ titleDiv.html( 0 );
+ titleCount = diff;
+ }
+
+ if ( titleCount >= 1 ) {
+ updateCount( titleDiv, titleCount );
+ regExMatch = titleRegEx.exec( document.title );
+ if ( regExMatch ) {
+ newTitle = document.title.replace( regExMatch[0], adminCommentsL10n.docTitleCommentsCount.replace( '%s', titleDiv.text() ) + ' ' );
+ }
+ } else {
+ regExMatch = titleRegEx.exec( newTitle );
+ if ( regExMatch ) {
+ newTitle = newTitle.replace( regExMatch[0], adminCommentsL10n.docTitleComments );
+ }
+ }
+ document.title = newTitle;
+ };
+
+ updatePending = function( diff, commentPostId ) {
+ var postSelector = '.post-com-count-' + commentPostId,
+ noClass = 'comment-count-no-pending',
+ noParentClass = 'post-com-count-no-pending',
+ pendingClass = 'comment-count-pending',
+ pending,
+ noPending;
+
+ if ( ! isDashboard ) {
+ updateHtmlTitle( diff );
+ }
+
+ $( 'span.pending-count' ).each(function() {
+ var a = $(this), n = getCount(a) + diff;
+ if ( n < 1 )
+ n = 0;
+ a.closest('.awaiting-mod')[ 0 === n ? 'addClass' : 'removeClass' ]('count-0');
+ updateCount( a, n );
+ });
+
+ if ( ! commentPostId ) {
+ return;
+ }
+
+ // cache selectors to not get dupes
+ pending = $( 'span.' + pendingClass, postSelector );
+ noPending = $( 'span.' + noClass, postSelector );
+
+ pending.each(function() {
+ var a = $(this), n = getCount(a) + diff;
+ if ( n < 1 )
+ n = 0;
+
+ if ( 0 === n ) {
+ a.parent().addClass( noParentClass );
+ a.removeClass( pendingClass ).addClass( noClass );
+ } else {
+ a.parent().removeClass( noParentClass );
+ a.addClass( pendingClass ).removeClass( noClass );
+ }
+ updateCount( a, n );
+ });
+
+ noPending.each(function() {
+ var a = $(this);
+ if ( diff > 0 ) {
+ a.parent().removeClass( noParentClass );
+ a.removeClass( noClass ).addClass( pendingClass );
+ } else {
+ a.parent().addClass( noParentClass );
+ a.addClass( noClass ).removeClass( pendingClass );
+ }
+ updateCount( a, diff );
+ });
+ };