PageSourceSearch

https://www.phd2published.com/wp-content/themes/OneCommunity/_inc/buddypress.js?ver=3.2.0

js phd2published.com collected 2026-10-03 21:38:35 UTC 59,816 bytes, 2,076 lines download raw bytes

1/* jshint undef: false, unused:false */
2// AJAX Functions
3var jq = jQuery;
4
5// Global variable to prevent multiple AJAX requests
6var bp_ajax_request = null;
7
8// Global variables to temporarily store newest activities
9var newest_activities = '';
10var activity_last_recorded  = 0;
11
12jq(document).ready( function() {
13	/**** Page Load Actions *******************************************************/
14
15	/* Hide Forums Post Form */
16	if ( '-1' === window.location.search.indexOf('new') && jq('div.forums').length ) {
17		jq('#new-topic-post').hide();
18	} else {
19		jq('#new-topic-post').show();
20	}
21
22	/* Activity filter and scope set */
23	bp_init_activity();
24
25	var objects  = [ 'members', 'groups', 'blogs', 'forums', 'group_members' ],
26		$whats_new = jq('#whats-new');
27
28	/* Object filter and scope set. */
29	bp_init_objects( objects );
30
31	/* @mention Compose Scrolling */
32	if ( $whats_new.length && bp_get_querystring('r') ) {
33		var $member_nicename = $whats_new.val();
34
35		jq('#whats-new-options').animate({
36			height:'50px'
37		});
38
39		$whats_new.animate({
40			height:'50px'
41		});
42
43		jq.scrollTo( $whats_new, 500, {
44			offset:-125,
45			easing:'swing'
46		} );
47
48		$whats_new.val('').focus().val( $member_nicename );
49	}
50
51	/**** Activity Posting ********************************************************/
52
53	/* Textarea focus */
54	$whats_new.focus( function(){
55		jq('#whats-new-options').animate({
56			height:'50px'
57		});
58		jq('#whats-new-form textarea').animate({
59			height:'50px'
60		});
61		jq('#aw-whats-new-submit').prop('disabled', false);
62
63		var $whats_new_form = jq('form#whats-new-form'),
64			$activity_all = jq( '#activity-all' );
65
66		if ( $whats_new_form.hasClass('submitted') ) {
67			$whats_new_form.removeClass('submitted');
68		}
69
70		// Return to the 'All Members' tab and 'Everything' filter,
71		// to avoid inconsistencies with the heartbeat integration
72		if ( $activity_all.length  ) {
73			if ( ! $activity_all.hasClass( 'selected' ) ) {
74				// reset to everything
75				jq( '#activity-filter-select select' ).val( '-1' );
76				$activity_all.children( 'a' ).trigger( 'click' );
77			} else if ( '-1' !== jq( '#activity-filter-select select' ).val() ) {
78				jq( '#activity-filter-select select' ).val( '-1' );
79				jq( '#activity-filter-select select' ).trigger( 'change' );
80			}
81		}
82	});
83
84	/* On blur, shrink if it's empty */
85	$whats_new.blur( function(){
86		if ( document.activeElement !== this ) {
87			if (!this.value.match(/\S+/)) {
88				this.value = '';
89				jq('#whats-new-options').animate({
90					height:'0'
91				});
92				jq('form#whats-new-form textarea').animate({
93					height:'20px'
94				});
95				jq('#aw-whats-new-submit').prop('disabled', true);
96			}
97		}
98	});
99
100	/* New posts */
101	jq('#aw-whats-new-submit').on( 'click', function() {
102		var last_date_recorded = 0,
103			button = jq(this),
104			form   = button.closest('form#whats-new-form');
105
106		form.children().each( function() {
107			if ( jq.nodeName(this, 'textarea') || jq.nodeName(this, 'input') ) {
108				jq(this).prop( 'disabled', true );
109			}
110		});
111
112		/* Remove any errors */
113		jq('div.error').remove();
114		button.addClass('loading');
115		button.prop('disabled', true);
116		form.addClass('submitted');
117
118		/* Default POST values */
119		object = '';
120		item_id = jq('#whats-new-post-in').val();
121		content = jq('#whats-new').val();
122		firstrow = jq( '#buddypress ul.activity-list li' ).first();
123		activity_row = firstrow;
124		timestamp = null;
125
126		// Checks if at least one activity exists
127		if ( firstrow.length ) {
128
129			if ( activity_row.hasClass( 'load-newest' ) ) {
130				activity_row = firstrow.next();
131			}
132
133			timestamp = activity_row.prop( 'class' ).match( /date-recorded-([0-9]+)/ );
134		}
135
136		if ( timestamp ) {
137			last_date_recorded = timestamp[1];
138		}
139
140		/* Set object for non-profile posts */
141		if ( item_id > 0 ) {
142			object = jq('#whats-new-post-object').val();
143		}
144
145		jq.post( ajaxurl, {
146			action: 'post_update',
147			'cookie': bp_get_cookies(),
148			'_wpnonce_post_update': jq('#_wpnonce_post_update').val(),
149			'content': content,
150			'object': object,
151			'item_id': item_id,
152			'since': last_date_recorded,
153			'_bp_as_nonce': jq('#_bp_as_nonce').val() || ''
154		},
155		function(response) {
156
157			form.children().each( function() {
158				if ( jq.nodeName(this, 'textarea') || jq.nodeName(this, 'input') ) {
159					jq(this).prop( 'disabled', false );
160				}
161			});
162
163			/* Check for errors and append if found. */
164			if ( response[0] + response[1] === '-1' ) {
165				form.prepend( response.substr( 2, response.length ) );
166				jq( '#' + form.attr('id') + ' div.error').hide().fadeIn( 200 );
167			} else {
168				if ( 0 === jq('ul.activity-list').length ) {
169					jq('div.error').slideUp(100).remove();
170					jq('#message').slideUp(100).remove();
171					jq('div.activity').append( '<ul id="activity-stream" class="activity-list item-list">' );
172				}
173
174				if ( firstrow.hasClass( 'load-newest' ) ) {
175					firstrow.remove();
176				}
177
178				jq('#activity-stream').prepend(response);
179
180				if ( ! last_date_recorded ) {
181					jq('#activity-stream li:first').addClass('new-update just-posted');
182				}
183
184				if ( 0 !== jq('#latest-update').length ) {
185					var l   = jq('#activity-stream li.new-update .activity-content .activity-inner p').html(),
186						v     = jq('#activity-stream li.new-update .activity-content .activity-header p a.view').attr('href'),
187						ltext = jq('#activity-stream li.new-update .activity-content .activity-inner p').text(),
188						u     = '';
189
190					if ( ltext !== '' ) {
191						u = l + ' ';
192					}
193
194					u += '<a href="' + v + '" rel="nofollow">' + BP_DTheme.view + '</a>';
195
196					jq('#latest-update').slideUp(300,function(){
197						jq('#latest-update').html( u );
198						jq('#latest-update').slideDown(300);
199					});
200				}
201
202				jq('li.new-update').hide().slideDown( 300 );
203				jq('li.new-update').removeClass( 'new-update' );
204				jq('#whats-new').val('');
205
206				// reset vars to get newest activities
207				newest_activities = '';
208				activity_last_recorded  = 0;
209			}
210
211			jq('#whats-new-options').animate({
212				height:'0px'
213			});
214			jq('#whats-new-form textarea').animate({
215				height:'20px'
216			});
217			jq('#aw-whats-new-submit').prop('disabled', true).removeClass('loading');
218		});
219
220		return false;
221	});
222
223	/* List tabs event delegation */
224	jq('div.activity-type-tabs').on( 'click', function(event) {
225		var target = jq(event.target).parent(),
226			scope, filter;
227
228		if ( event.target.nodeName === 'STRONG' || event.target.nodeName === 'SPAN' ) {
229			target = target.parent();
230		} else if ( event.target.nodeName !== 'A' ) {
231			return false;
232		}
233
234		/* Reset the page */
235		jq.cookie( 'bp-activity-oldestpage', 1, {
236			path: '/'
237		} );
238
239		/* Activity Stream Tabs */
240		scope  = target.attr('id').substr( 9, target.attr('id').length );
241		filter = jq('#activity-filter-select select').val();
242
243		if ( scope === 'mentions' ) {
244			jq( '#' + target.attr('id') + ' a strong' ).remove();
245		}
246
247		bp_activity_request(scope, filter);
248
249		return false;
250	});
251
252	/* Activity filter select */
253	jq('#activity-filter-select select').change( function() {
254		var selected_tab = jq( 'div.activity-type-tabs li.selected' ),
255			filter = jq(this).val(),
256			scope;
257
258		if ( !selected_tab.length ) {
259			scope = null;
260		} else {
261			scope = selected_tab.attr('id').substr( 9, selected_tab.attr('id').length );
262		}
263
264		bp_activity_request(scope, filter);
265
266		return false;
267	});
268
269	/* Stream event delegation */
270	jq('div.activity').on( 'click', function(event) {
271		var target = jq(event.target),
272			type, parent, parent_id,
273			li, id, link_href, nonce, timestamp,
274			oldest_page, just_posted;
275
276		/* Favoriting activity stream items */
277		if ( target.hasClass('fav') || target.hasClass('unfav') ) {
278			type      = target.hasClass('fav') ? 'fav' : 'unfav';
279			parent    = target.closest('.activity-item');
280			parent_id = parent.attr('id').substr( 9, parent.attr('id').length );
281
282			target.addClass('loading');
283
284			jq.post( ajaxurl, {
285				action: 'activity_mark_' + type,
286				'cookie': bp_get_cookies(),
287				'id': parent_id
288			},
289			function(response) {
290				target.removeClass('loading');
291
292				target.fadeOut( 200, function() {
293					jq(this).html(response);
294					jq(this).attr('title', 'fav' === type ? BP_DTheme.remove_fav : BP_DTheme.mark_as_fav);
295					jq(this).fadeIn(200);
296				});
297
298				if ( 'fav' === type ) {
299					if ( !jq('.item-list-tabs #activity-favs-personal-li').length ) {
300						if ( !jq('.item-list-tabs #activity-favorites').length ) {
301							jq('.item-list-tabs ul #activity-mentions').before( '<li id="activity-favorites"><a href="#">' + BP_DTheme.my_favs + ' <span>0</span></a></li>');
302						}
303
304						jq('.item-list-tabs ul #activity-favorites span').html( Number( jq('.item-list-tabs ul #activity-favorites span').html() ) + 1 );
305					}
306
307					target.removeClass('fav');
308					target.addClass('unfav');
309
310				} else {
311					target.removeClass('unfav');
312					target.addClass('fav');
313
314					jq('.item-list-tabs ul #activity-favorites span').html( Number( jq('.item-list-tabs ul #activity-favorites span').html() ) - 1 );
315
316					if ( !Number( jq('.item-list-tabs ul #activity-favorites span').html() ) ) {
317						if ( jq('.item-list-tabs ul #activity-favorites').hasClass('selected') ) {
318							bp_activity_request( null, null );
319						}
320
321						jq('.item-list-tabs ul #activity-favorites').remove();
322					}
323				}
324
325				if ( 'activity-favorites' === jq( '.item-list-tabs li.selected').attr('id') ) {
326					target.closest( '.activity-item' ).slideUp( 100 );
327				}
328			});
329
330			return false;
331		}
332
333		/* Delete activity stream items */
334		if ( target.hasClass('delete-activity') ) {
335			li        = target.parents('div.activity ul li');
336			id        = li.attr('id').substr( 9, li.attr('id').length );
337			link_href = target.attr('href');
338			nonce     = link_href.split('_wpnonce=');
339			timestamp = li.prop( 'class' ).match( /date-recorded-([0-9]+)/ );
340			nonce     = nonce[1];
341
342			target.addClass('loading');
343
344			jq.post( ajaxurl, {
345				action: 'delete_activity',
346				'cookie': bp_get_cookies(),
347				'id': id,
348				'_wpnonce': nonce
349			},
350			function(response) {
351
352				if ( response[0] + response[1] === '-1' ) {
353					li.prepend( response.substr( 2, response.length ) );
354					li.children('#message').hide().fadeIn(300);
355				} else {
356					li.slideUp(300);
357
358					// reset vars to get newest activities
359					if ( timestamp && activity_last_recorded === timestamp[1] ) {
360						newest_activities = '';
361						activity_last_recorded  = 0;
362					}
363				}
364			});
365
366			return false;
367		}
368
369		// Spam activity stream items
370		if ( target.hasClass( 'spam-activity' ) ) {
371			li        = target.parents( 'div.activity ul li' );
372			timestamp = li.prop( 'class' ).match( /date-recorded-([0-9]+)/ );
373			target.addClass( 'loading' );
374
375			jq.post( ajaxurl, {
376				action: 'bp_spam_activity',
377				'cookie': encodeURIComponent( document.cookie ),
378				'id': li.attr( 'id' ).substr( 9, li.attr( 'id' ).length ),
379				'_wpnonce': target.attr( 'href' ).split( '_wpnonce=' )[1]
380			},
381
382			function(response) {
383				if ( response[0] + response[1] === '-1' ) {
384					li.prepend( response.substr( 2, response.length ) );
385					li.children( '#message' ).hide().fadeIn(300);
386				} else {
387					li.slideUp( 300 );
388					// reset vars to get newest activities
389					if ( timestamp && activity_last_recorded === timestamp[1] ) {
390						newest_activities = '';
391						activity_last_recorded  = 0;
392					}
393				}
394			});
395
396			return false;
397		}
398
399		/* Load more updates at the end of the page */
400		if ( target.parent().hasClass('load-more') ) {
401			if ( bp_ajax_request ) {
402				bp_ajax_request.abort();
403			}
404
405			jq('#buddypress li.load-more').addClass('loading');
406
407			if ( null === jq.cookie('bp-activity-oldestpage') ) {
408				jq.cookie('bp-activity-oldestpage', 1, {
409					path: '/'
410				} );
411			}
412
413			oldest_page = ( jq.cookie('bp-activity-oldestpage') * 1 ) + 1;
414			just_posted = [];
415
416			jq('.activity-list li.just-posted').each( function(){
417				just_posted.push( jq(this).attr('id').replace( 'activity-','' ) );
418			});
419
420			load_more_args = {
421				action: 'activity_get_older_updates',
422				'cookie': bp_get_cookies(),
423				'page': oldest_page,
424				'exclude_just_posted': just_posted.join(',')
425			};
426
427			load_more_search = bp_get_querystring('s');
428
429			if ( load_more_search ) {
430				load_more_args.search_terms = load_more_search;
431			}
432
433			bp_ajax_request = jq.post( ajaxurl, load_more_args,
434			function(response)
435			{
436				jq('#buddypress li.load-more').removeClass('loading');
437				jq.cookie( 'bp-activity-oldestpage', oldest_page, {
438					path: '/'
439				} );
440				jq('#buddypress ul.activity-list').append(response.contents);
441
442				target.parent().hide();
443			}, 'json' );
444
445			return false;
446		}
447
448		/* Load newest updates at the top of the list */
449		if ( target.parent().hasClass('load-newest') ) {
450
451			event.preventDefault();
452
453			target.parent().hide();
454
455			/**
456			 * If a plugin is updating the recorded_date of an activity
457			 * it will be loaded as a new one. We need to look in the
458			 * stream and eventually remove similar ids to avoid "double".
459			 */
460			activity_html = jq.parseHTML( newest_activities );
461
462			jq.each( activity_html, function( i, el ){
463				if( 'LI' === el.nodeName && jq(el).hasClass( 'just-posted' ) ) {
464					if( jq( '#' + jq(el).attr( 'id' ) ).length ) {
465						jq( '#' + jq(el).attr( 'id' ) ).remove();
466					}
467				}
468			} );
469
470			// Now the stream is cleaned, prepend newest
471			jq( '#buddypress ul.activity-list' ).prepend( newest_activities );
472
473			// reset the newest activities now they're displayed
474			newest_activities = '';
475		}
476	});
477
478	// Activity "Read More" links
479	jq('div.activity').on('click', '.activity-read-more a', function(event) {
480		var target = jq(event.target),
481			link_id = target.parent().attr('id').split('-'),
482			a_id    = link_id[3],
483			type    = link_id[0], /* activity or acomment */
484			inner_class, a_inner;
485
486		inner_class = type === 'acomment' ? 'acomment-content' : 'activity-inner';
487		a_inner = jq('#' + type + '-' + a_id + ' .' + inner_class + ':first' );
488		jq(target).addClass('loading');
489
490		jq.post( ajaxurl, {
491			action: 'get_single_activity_content',
492			'activity_id': a_id
493		},
494		function(response) {
495			jq(a_inner).slideUp(300).html(response).slideDown(300);
496		});
497
498		return false;
499	});
500
501	/**** Activity Comments *******************************************************/
502
503	/* Hide all activity comment forms */
504	jq('form.ac-form').hide();
505
506	/* Hide excess comments */
507	if ( jq('.activity-comments').length ) {
508		bp_legacy_theme_hide_comments();
509	}
510
511	/* Activity list event delegation */
512	jq('div.activity').on( 'click', function(event) {
513		var target = jq(event.target),
514			id, ids, a_id, c_id, form,
515			form_parent, form_id,
516			tmp_id, comment_id, comment,
517			ajaxdata,
518			ak_nonce,
519			show_all_a, new_count,
520			link_href, comment_li, nonce;
521
522		/* Comment / comment reply links */
523		if ( target.hasClass('acomment-reply') || target.parent().hasClass('acomment-reply') ) {
524			if ( target.parent().hasClass('acomment-reply') ) {
525				target = target.parent();
526			}
527
528			id  = target.attr('id');
529			ids = id.split('-');
530
531			a_id = ids[2];
532			c_id = target.attr('href').substr( 10, target.attr('href').length );
533			form = jq( '#ac-form-' + a_id );
534
535			form.css( 'display', 'none' );
536			form.removeClass('root');
537			jq('.ac-form').hide();
538
539			/* Hide any error messages */
540			form.children('div').each( function() {
541				if ( jq(this).hasClass( 'error' ) ) {
542					jq(this).hide();
543				}
544			});
545
546			if ( ids[1] !== 'comment' ) {
547				jq('#acomment-' + c_id).append( form );
548			} else {
549				jq('#activity-' + a_id + ' .activity-comments').append( form );
550			}
551
552			if ( form.parent().hasClass( 'activity-comments' ) ) {
553				form.addClass('root');
554			}
555
556			form.slideDown( 200 );
557			jq.scrollTo( form, 500, {
558				offset:-100,
559				easing:'swing'
560			} );
561			jq('#ac-form-' + ids[2] + ' textarea').focus();
562
563			return false;
564		}
565
566		/* Activity comment posting */
567		if ( target.attr('name') === 'ac_form_submit' ) {
568			form = target.parents( 'form' );
569			form_parent = form.parent();
570			form_id = form.attr('id').split('-');
571
572			if ( !form_parent.hasClass('activity-comments') ) {
573				tmp_id = form_parent.attr('id').split('-');
574				comment_id = tmp_id[1];
575			} else {
576				comment_id = form_id[2];
577			}
578
579			content = jq( '#' + form.attr('id') + ' textarea' );
580
581			/* Hide any error messages */
582			jq( '#' + form.attr('id') + ' div.error').hide();
583			target.addClass('loading').prop('disabled', true);
584			content.addClass('loading').prop('disabled', true);
585
586			ajaxdata = {
587				action: 'new_activity_comment',
588				'cookie': bp_get_cookies(),
589				'_wpnonce_new_activity_comment': jq('#_wpnonce_new_activity_comment').val(),
590				'comment_id': comment_id,
591				'form_id': form_id[2],
592				'content': content.val()
593			};
594
595			// Akismet
596			ak_nonce = jq('#_bp_as_nonce_' + comment_id).val();
597			if ( ak_nonce ) {
598				ajaxdata['_bp_as_nonce_' + comment_id] = ak_nonce;
599			}
600
601			jq.post( ajaxurl, ajaxdata, function(response) {
602				target.removeClass('loading');
603				content.removeClass('loading');
604
605				/* Check for errors and append if found. */
606				if ( response[0] + response[1] === '-1' ) {
607					form.append( jq( response.substr( 2, response.length ) ).hide().fadeIn( 200 ) );
608				} else {
609					var activity_comments = form.parent();
610					form.fadeOut( 200, function() {
611						if ( 0 === activity_comments.children('ul').length ) {
612							if ( activity_comments.hasClass('activity-comments') ) {
613								activity_comments.prepend('<ul></ul>');
614							} else {
615								activity_comments.append('<ul></ul>');
616							}
617						}
618
619						/* Preceding whitespace breaks output with jQuery 1.9.0 */
620						var the_comment = jq.trim( response );
621
622						activity_comments.children('ul').append( jq( the_comment ).hide().fadeIn( 200 ) );
623						form.children('textarea').val('');
624						activity_comments.parent().addClass('has-comments');
625					} );
626					jq( '#' + form.attr('id') + ' textarea').val('');
627
628					/* Increase the "Reply (X)" button count */
629					jq('#activity-' + form_id[2] + ' a.acomment-reply span').html( Number( jq('#activity-' + form_id[2] + ' a.acomment-reply span').html() ) + 1 );
630
631					// Increment the 'Show all x comments' string, if present
632					show_all_a = activity_comments.find('.show-all').find('a');
633					if ( show_all_a ) {
634						new_count = jq('li#activity-' + form_id[2] + ' a.acomment-reply span').html();
635						show_all_a.html( BP_DTheme.show_x_comments.replace( '%d', new_count ) );
636					}
637				}
638
639				jq(target).prop('disabled', false);
640				jq(content).prop('disabled', false);
641			});
642
643			return false;
644		}
645
646		/* Deleting an activity comment */
647		if ( target.hasClass('acomment-delete') ) {
648			link_href = target.attr('href');
649			comment_li = target.parent().parent();
650			form = comment_li.parents('div.activity-comments').children('form');
651
652			nonce = link_href.split('_wpnonce=');
653			nonce = nonce[1];
654
655			comment_id = link_href.split('cid=');
656			comment_id = comment_id[1].split('&');
657			comment_id = comment_id[0];
658
659			target.addClass('loading');
660
661			/* Remove any error messages */
662			jq('.activity-comments ul .error').remove();
663
664			/* Reset the form position */
665			comment_li.parents('.activity-comments').append(form);
666
667			jq.post( ajaxurl, {
668				action: 'delete_activity_comment',
669				'cookie': bp_get_cookies(),
670				'_wpnonce': nonce,
671				'id': comment_id
672			},
673			function(response) {
674				/* Check for errors and append if found. */
675				if ( response[0] + response[1] === '-1' ) {
676					comment_li.prepend( jq( response.substr( 2, response.length ) ).hide().fadeIn( 200 ) );
677				} else {
678					var children  = jq( '#' + comment_li.attr('id') + ' ul' ).children('li'),
679						child_count = 0,
680						count_span, new_count, show_all_a;
681
682					jq(children).each( function() {
683						if ( !jq(this).is(':hidden') ) {
684							child_count++;
685						}
686					});
687					comment_li.fadeOut(200, function() {
688						comment_li.remove();
689					});
690
691					/* Decrease the "Reply (X)" button count */
692					count_span = jq('#' + comment_li.parents('#activity-stream > li').attr('id') + ' a.acomment-reply span');
693					new_count = count_span.html() - ( 1 + child_count );
694					count_span.html(new_count);
695
696					// Change the 'Show all x comments' text
697					show_all_a = comment_li.siblings('.show-all').find('a');
698					if ( show_all_a ) {
699						show_all_a.html( BP_DTheme.show_x_comments.replace( '%d', new_count ) );
700					}
701
702					/* If that was the last comment for the item, remove the has-comments class to clean up the styling */
703					if ( 0 === new_count ) {
704						jq(comment_li.parents('#activity-stream > li')).removeClass('has-comments');
705					}
706				}
707			});
708
709			return false;
710		}
711
712		// Spam an activity stream comment
713		if ( target.hasClass( 'spam-activity-comment' ) ) {
714			link_href  = target.attr( 'href' );
715			comment_li = target.parent().parent();
716
717			target.addClass('loading');
718
719			// Remove any error messages
720			jq( '.activity-comments ul div.error' ).remove();
721
722			// Reset the form position
723			comment_li.parents( '.activity-comments' ).append( comment_li.parents( '.activity-comments' ).children( 'form' ) );
724
725			jq.post( ajaxurl, {
726				action: 'bp_spam_activity_comment',
727				'cookie': encodeURIComponent( document.cookie ),
728				'_wpnonce': link_href.split( '_wpnonce=' )[1],
729				'id': link_href.split( 'cid=' )[1].split( '&' )[0]
730			},
731
732			function ( response ) {
733				// Check for errors and append if found.
734				if ( response[0] + response[1] === '-1' ) {
735					comment_li.prepend( jq( response.substr( 2, response.length ) ).hide().fadeIn( 200 ) );
736
737				} else {
738					var children  = jq( '#' + comment_li.attr( 'id' ) + ' ul' ).children( 'li' ),
739						child_count = 0,
740						parent_li;
741
742					jq(children).each( function() {
743						if ( !jq( this ).is( ':hidden' ) ) {
744							child_count++;
745						}
746					});
747					comment_li.fadeOut( 200 );
748
749					// Decrease the "Reply (X)" button count
750					parent_li = comment_li.parents( '#activity-stream > li' );
751					jq( '#' + parent_li.attr( 'id' ) + ' a.acomment-reply span' ).html( jq( '#' + parent_li.attr( 'id' ) + ' a.acomment-reply span' ).html() - ( 1 + child_count ) );
752				}
753			});
754
755			return false;
756		}
757
758		/* Showing hidden comments - pause for half a second */
759		if ( target.parent().hasClass('show-all') ) {
760			target.parent().addClass('loading');
761
762			setTimeout( function() {
763				target.parent().parent().children('li').fadeIn(200, function() {
764					target.parent().remove();
765				});
766			}, 600 );
767
768			return false;
769		}
770
771		// Canceling an activity comment
772		if ( target.hasClass( 'ac-reply-cancel' ) ) {
773			jq(target).closest('.ac-form').slideUp( 200 );
774			return false;
775		}
776	});
777
778	/* Escape Key Press for cancelling comment forms */
779	jq(document).keydown( function(e) {
780		e = e || window.event;
781		if (e.target) {
782			element = e.target;
783		} else if (e.srcElement) {
784			element = e.srcElement;
785		}
786
787		if( element.nodeType === 3) {
788			element = element.parentNode;
789		}
790
791		if( e.ctrlKey === true || e.altKey === true || e.metaKey === true ) {
792			return;
793		}
794
795		var keyCode = (e.keyCode) ? e.keyCode : e.which;
796
797		if ( keyCode === 27 ) {
798			if (element.tagName === 'TEXTAREA') {
799				if ( jq(element).hasClass('ac-input') ) {
800					jq(element).parent().parent().parent().slideUp( 200 );
801				}
802			}
803		}
804	});
805
806	/**** Directory Search ****************************************************/
807
808	/* The search form on all directory pages */
809	jq( '.dir-search, .groups-members-search' ).on( 'click', function(event) {
810		if ( jq(this).hasClass('no-ajax') ) {
811			return;
812		}
813
814		var target = jq(event.target),
815			css_id, object, template;
816
817		if ( target.attr('type') === 'submit' ) {
818			css_id = jq('.item-list-tabs li.selected').attr('id').split( '-' );
819			object = css_id[0];
820			template = null;
821
822			// The Group Members page specifies its own template
823			if ( event.currentTarget.className === 'groups-members-search' ) {
824				object = 'group_members';
825				template = 'groups/single/members';
826			}
827
828			bp_filter_request( object, jq.cookie('bp-' + object + '-filter'), jq.cookie('bp-' + object + '-scope') , 'div.' + object, target.parent().children('label').children('input').val(), 1, jq.cookie('bp-' + object + '-extras'), null, template );
829
830			return false;
831		}
832	});
833
834	/**** Tabs and Filters ****************************************************/
835
836	/* When a navigation tab is clicked - e.g. | All Groups | My Groups | */
837	jq('div.item-list-tabs').on( 'click', function(event) {
838		if ( jq(this).hasClass('no-ajax')  || jq( event.target ).hasClass('no-ajax') )  {
839			return;
840		}
841
842		var targetElem = ( event.target.nodeName === 'SPAN' ) ? event.target.parentNode : event.target,
843			target       = jq( targetElem ).parent(),
844			css_id, object, scope, filter, search_terms;
845
846		if ( 'LI' === target[0].nodeName && !target.hasClass( 'last' ) ) {
847			css_id = target.attr('id').split( '-' );
848			object = css_id[0];
849
850			if ( 'activity' === object ) {
851				return false;
852			}
853
854			scope = css_id[1];
855			filter = jq('#' + object + '-order-select select').val();
856			search_terms = jq('#' + object + '_search').val();
857
858			bp_filter_request( object, filter, scope, 'div.' + object, search_terms, 1, jq.cookie('bp-' + object + '-extras') );
859
860			return false;
861		}
862	});
863
864	/* When the filter select box is changed re-query */
865	jq('li.filter select').change( function() {
866		var el,
867			css_id, object, scope, filter, search_terms, template,
868			$gm_search;
869
870		if ( jq('.item-list-tabs li.selected').length ) {
871			el = jq('.item-list-tabs li.selected');
872		} else {
873			el = jq(this);
874		}
875
876		css_id = el.attr('id').split('-');
877		object = css_id[0];
878		scope = css_id[1];
879		filter = jq(this).val();
880		search_terms = false;
881		template = null;
882
883		if ( jq('.dir-search input').length ) {
884			search_terms = jq('.dir-search input').val();
885		}
886
887		// The Group Members page has a different selector for its
888		// search terms box
889		$gm_search = jq( '.groups-members-search input' );
890		if ( $gm_search.length ) {
891			search_terms = $gm_search.val();
892			object = 'members';
893			scope = 'groups';
894		}
895
896		// On the Groups Members page, we specify a template
897		if ( 'members' === object && 'groups' === scope ) {
898			object = 'group_members';
899			template = 'groups/single/members';
900		}
901
902		if ( 'friends' === object ) {
903			object = 'members';
904		}
905
906		bp_filter_request( object, filter, scope, 'div.' + object, search_terms, 1, jq.cookie('bp-' + object + '-extras'), null, template );
907
908		return false;
909	});
910
911	/* All pagination links run through this function */
912	jq('#buddypress').on( 'click', function(event) {
913		var target = jq(event.target),
914			el,
915			css_id, object, search_terms, pagination_id, template,
916			url_parameters,	page_number,
917			$gm_search,
918			caller;
919
920		if ( target.hasClass('button') ) {
921			return true;
922		}
923
924		if ( target.parent().parent().hasClass('pagination') && !target.parent().parent().hasClass('no-ajax') ) {
925			if ( target.hasClass('dots') || target.hasClass('current') ) {
926				return false;
927			}
928
929			if ( jq('.item-list-tabs li.selected').length ) {
930				el = jq('.item-list-tabs li.selected');
931			} else {
932				el = jq('li.filter select');
933			}
934
935			css_id = el.attr('id').split( '-' );
936			object = css_id[0];
937			search_terms = false;
938			pagination_id = jq(target).closest('.pagination-links').attr('id');
939			template = null;
940
941			url_parameters = target.attr('href').split( '&' );
942			// The page number is the first parameter.
943			page_number = url_parameters[0].split( '=' );
944			page_number = page_number[1];
945
946			// Search terms
947			if ( jq('div.dir-search input').length ) {
948				search_terms =  jq('.dir-search input').val();
949
950				if ( ! search_terms && bp_get_querystring('s') ) {
951					search_terms = jq('.dir-search input').prop('placeholder');
952				}
953			}
954
955			// The Group Members page has a different selector for
956			// its search terms box
957			$gm_search = jq( '.groups-members-search input' );
958			if ( $gm_search.length ) {
959				search_terms = $gm_search.val();
960				object = 'members';
961			}
962
963			// On the Groups Members page, we specify a template
964			if ( 'members' === object && 'groups' === css_id[1] ) {
965				object = 'group_members';
966				template = 'groups/single/members';
967			}
968
969			// On the Admin > Requests page, we need to reset the object,
970			// since "admin" isn't specific enough
971			if ( 'admin' === object && jq( 'body' ).hasClass( 'membership-requests' ) ) {
972				object = 'requests';
973			}
974
975			if ( pagination_id.indexOf( 'pag-bottom' ) !== -1 ) {
976				caller = 'pag-bottom';
977			} else {
978				caller = null;
979			}
980
981			bp_filter_request( object, jq.cookie('bp-' + object + '-filter'), jq.cookie('bp-' + object + '-scope'), 'div.' + object, search_terms, page_number, jq.cookie('bp-' + object + '-extras'), caller, template );
982
983			return false;
984		}
985
986	});
987
988	/**** New Forum Directory Post **************************************/
989
990	/* Hit the "New Topic" button on the forums directory page */
991	jq('a.show-hide-new').on( 'click', function() {
992		if ( !jq('#new-topic-post').length ) {
993			return false;
994		}
995
996		if ( jq('#new-topic-post').is(':visible') ) {
997			jq('#new-topic-post').slideUp(200);
998		} else {
999			jq('#new-topic-post').slideDown(200, function() {
1000				jq('#topic_title').focus();
1001			} );
1002		}
1003
1004		return false;
1005	});
1006
1007	/* Cancel the posting of a new forum topic */
1008	jq('#submit_topic_cancel').on( 'click', function() {
1009		if ( !jq('#new-topic-post').length ) {
1010			return false;
1011		}
1012
1013		jq('#new-topic-post').slideUp(200);
1014		return false;
1015	});
1016
1017	/* Clicking a forum tag */
1018	jq('#forum-directory-tags a').on( 'click', function() {
1019		bp_filter_request( 'forums', 'tags', jq.cookie('bp-forums-scope'), 'div.forums', jq(this).html().replace( /&nbsp;/g, '-' ), 1, jq.cookie('bp-forums-extras') );
1020		return false;
1021	});
1022
1023	/** Invite Friends Interface ****************************************/
1024
1025	/* Select a user from the list of friends and add them to the invite list */
1026	jq('#send-invite-form').on( 'click', '#invite-list input', function() {
1027		// invites-loop template contains a div with the .invite class
1028		// We use the existence of this div to check for old- vs new-
1029		// style templates.
1030		var invites_new_template = jq( '#send-invite-form > .invite' ).length,
1031			friend_id, friend_action;
1032
1033		jq('.ajax-loader').toggle();
1034
1035		// Dim the form until the response arrives
1036		if ( invites_new_template ) {
1037			jq( this ).parents( 'ul' ).find( 'input' ).prop( 'disabled', true );
1038		}
1039
1040		friend_id = jq(this).val();
1041
1042		if ( jq(this).prop('checked') === true ) {
1043			friend_action = 'invite';
1044		} else {
1045			friend_action = 'uninvite';
1046		}
1047
1048		if ( ! invites_new_template ) {
1049			jq( '.item-list-tabs li.selected' ).addClass( 'loading' );
1050		}
1051
1052		jq.post( ajaxurl, {
1053			action: 'groups_invite_user',
1054			'friend_action': friend_action,
1055			'cookie': bp_get_cookies(),
1056			'_wpnonce': jq('#_wpnonce_invite_uninvite_user').val(),
1057			'friend_id': friend_id,
1058			'group_id': jq('#group_id').val()
1059		},
1060		function(response)
1061		{
1062			if ( jq('#message') ) {
1063				jq('#message').hide();
1064			}
1065
1066			if ( invites_new_template ) {
1067				// With new-style templates, we refresh the
1068				// entire list
1069				bp_filter_request( 'invite', 'bp-invite-filter', 'bp-invite-scope', 'div.invite', false, 1, '', '', '' );
1070			} else {
1071				// Old-style templates manipulate only the
1072				// single invitation element
1073				jq('.ajax-loader').toggle();
1074
1075				if ( friend_action === 'invite' ) {
1076					jq('#friend-list').append(response);
1077				} else if ( friend_action === 'uninvite' ) {
1078					jq('#friend-list li#uid-' + friend_id).remove();
1079				}
1080
1081				jq('.item-list-tabs li.selected').removeClass('loading');
1082			}
1083		});
1084	});
1085
1086	/* Remove a user from the list of users to invite to a group */
1087	jq('#send-invite-form').on('click', 'a.remove', function() {
1088		// invites-loop template contains a div with the .invite class
1089		// We use the existence of this div to check for old- vs new-
1090		// style templates.
1091		var invites_new_template = jq('#send-invite-form > .invite').length,
1092			friend_id = jq(this).attr('id');
1093
1094		jq('.ajax-loader').toggle();
1095
1096		friend_id = friend_id.split('-');
1097		friend_id = friend_id[1];
1098
1099		jq.post( ajaxurl, {
1100			action: 'groups_invite_user',
1101			'friend_action': 'uninvite',
1102			'cookie': bp_get_cookies(),
1103			'_wpnonce': jq('#_wpnonce_invite_uninvite_user').val(),
1104			'friend_id': friend_id,
1105			'group_id': jq('#group_id').val()
1106		},
1107		function(response)
1108		{
1109			if ( invites_new_template ) {
1110				// With new-style templates, we refresh the
1111				// entire list
1112				bp_filter_request( 'invite', 'bp-invite-filter', 'bp-invite-scope', 'div.invite', false, 1, '', '', '' );
1113			} else {
1114				// Old-style templates manipulate only the
1115				// single invitation element
1116				jq('.ajax-loader').toggle();
1117				jq('#friend-list #uid-' + friend_id).remove();
1118				jq('#invite-list #f-' + friend_id).prop('checked', false);
1119			}
1120		});
1121
1122		return false;
1123	});
1124
1125	/** Profile Visibility Settings *********************************/
1126	jq( '.visibility-toggle-link' ).on( 'click', function( event ) {
1127		event.preventDefault();
1128
1129		jq( this ).parent().hide().addClass( 'field-visibility-settings-hide' )
1130			.siblings( '.field-visibility-settings' ).show().addClass( 'field-visibility-settings-open' );
1131	} );
1132
1133	jq( '.field-visibility-settings-close' ).on( 'click', function( event ) {
1134		event.preventDefault();
1135
1136		var settings_div = jq( this ).parent(),
1137			vis_setting_text = settings_div.find( 'input:checked' ).parent().text();
1138
1139		settings_div.hide().removeClass( 'field-visibility-settings-open' )
1140			.siblings( '.field-visibility-settings-toggle' )
1141				.children( '.current-visibility-level' ).text( vis_setting_text ).end()
1142			.show().removeClass( 'field-visibility-settings-hide' );
1143	} );
1144
1145	jq('#profile-edit-form input:not(:submit), #profile-edit-form textarea, #profile-edit-form select, #signup_form input:not(:submit), #signup_form textarea, #signup_form select').change( function() {
1146		var shouldconfirm = true;
1147
1148		jq('#profile-edit-form input:submit, #signup_form input:submit').on( 'click', function() {
1149			shouldconfirm = false;
1150		});
1151
1152		window.onbeforeunload = function(e) {
1153			if ( shouldconfirm ) {
1154				return BP_DTheme.unsaved_changes;
1155			}
1156		};
1157	});
1158
1159	/** Friendship Requests **************************************/
1160
1161	/* Accept and Reject friendship request buttons */
1162	jq('#friend-list a.accept, #friend-list a.reject').on( 'click', function() {
1163		var button   = jq(this),
1164			li         = jq(this).parents('#friend-list li'),
1165			action_div = jq(this).parents('li div.action'),
1166			id         = li.attr('id').substr( 11, li.attr('id').length ),
1167			link_href  = button.attr('href'),
1168			nonce      = link_href.split('_wpnonce=')[1],
1169			action;
1170
1171		if ( jq(this).hasClass('accepted') || jq(this).hasClass('rejected') ) {
1172			return false;
1173		}
1174
1175		if ( jq(this).hasClass('accept') ) {
1176			action = 'accept_friendship';
1177			action_div.children('a.reject').css( 'visibility', 'hidden' );
1178		} else {
1179			action = 'reject_friendship';
1180			action_div.children('a.accept').css( 'visibility', 'hidden' );
1181		}
1182
1183		button.addClass('loading');
1184
1185		jq.post( ajaxurl, {
1186			action: action,
1187			'cookie': bp_get_cookies(),
1188			'id': id,
1189			'_wpnonce': nonce
1190		},
1191		function(response) {
1192			button.removeClass('loading');
1193
1194			if ( response[0] + response[1] === '-1' ) {
1195				li.prepend( response.substr( 2, response.length ) );
1196				li.children('#message').hide().fadeIn(200);
1197			} else {
1198				button.fadeOut( 100, function() {
1199					if ( jq(this).hasClass('accept') ) {
1200						action_div.children('a.reject').hide();
1201						jq(this).html( BP_DTheme.accepted ).contents().unwrap();
1202					} else {
1203						action_div.children('a.accept').hide();
1204						jq(this).html( BP_DTheme.rejected ).contents().unwrap();
1205					}
1206				});
1207			}
1208		});
1209
1210		return false;
1211	});
1212
1213	/* Add / Remove friendship buttons */
1214	jq( '#members-dir-list, #members-group-list, #item-header' ).on('click', '.friendship-button a', function() {
1215		jq(this).parent().addClass('loading');
1216		var fid   = jq(this).attr('id'),
1217			nonce   = jq(this).attr('href'),
1218			thelink = jq(this);
1219
1220		fid = fid.split('-');
1221		fid = fid[1];
1222
1223		nonce = nonce.split('?_wpnonce=');
1224		nonce = nonce[1].split('&');
1225		nonce = nonce[0];
1226
1227		jq.post( ajaxurl, {
1228			action: 'addremove_friend',
1229			'cookie': bp_get_cookies(),
1230			'fid': fid,
1231			'_wpnonce': nonce
1232		},
1233		function(response)
1234		{
1235			var action  = thelink.attr('rel');
1236				parentdiv = thelink.parent();
1237
1238			if ( action === 'add' ) {
1239				jq(parentdiv).fadeOut(200,
1240					function() {
1241						parentdiv.removeClass('add_friend');
1242						parentdiv.removeClass('loading');
1243						parentdiv.addClass('pending_friend');
1244						parentdiv.fadeIn(200).html(response);
1245					}
1246					);
1247
1248			} else if ( action === 'remove' ) {
1249				jq(parentdiv).fadeOut(200,
1250					function() {
1251						parentdiv.removeClass('remove_friend');
1252						parentdiv.removeClass('loading');
1253						parentdiv.addClass('add');
1254						parentdiv.fadeIn(200).html(response);
1255					}
1256					);
1257			}
1258		});
1259		return false;
1260	} );
1261
1262	/** Group Join / Leave Buttons **************************************/
1263
1264	// Confirmation when clicking Leave Group in group headers
1265	jq('#buddypress').on('click', '.group-button .leave-group', function() {
1266		if ( false === confirm( BP_DTheme.leave_group_confirm ) ) {
1267			return false;
1268		}
1269	});
1270
1271	jq('#groups-dir-list').on('click', '.group-button a', function() {
1272		var gid   = jq(this).parent().attr('id'),
1273			nonce   = jq(this).attr('href'),
1274			thelink = jq(this);
1275
1276		gid = gid.split('-');
1277		gid = gid[1];
1278
1279		nonce = nonce.split('?_wpnonce=');
1280		nonce = nonce[1].split('&');
1281		nonce = nonce[0];
1282
1283		// Leave Group confirmation within directories - must intercept
1284		// AJAX request
1285		if ( thelink.hasClass( 'leave-group' ) && false === confirm( BP_DTheme.leave_group_confirm ) ) {
1286			return false;
1287		}
1288
1289		jq.post( ajaxurl, {
1290			action: 'joinleave_group',
1291			'cookie': bp_get_cookies(),
1292			'gid': gid,
1293			'_wpnonce': nonce
1294		},
1295		function(response)
1296		{
1297			var parentdiv = thelink.parent();
1298
1299			// user groups page
1300			if ( ! jq('body.directory').length ) {
1301				window.location.reload();
1302
1303			// groups directory
1304			} else {
1305				jq(parentdiv).fadeOut(200,
1306					function() {
1307						parentdiv.fadeIn(200).html(response);
1308
1309						var mygroups = jq('#groups-personal span'),
1310							add        = 1;
1311
1312						if( thelink.hasClass( 'leave-group' ) ) {
1313							// hidden groups slide up
1314							if ( parentdiv.hasClass( 'hidden' ) ) {
1315								parentdiv.closest('li').slideUp( 200 );
1316							}
1317
1318							add = 0;
1319						} else if ( thelink.hasClass( 'request-membership' ) ) {
1320							add = false;
1321						}
1322
1323						// change the "My Groups" value
1324						if ( mygroups.length && add !== false ) {
1325							if ( add ) {
1326								mygroups.text( ( mygroups.text() >> 0 ) + 1 );
1327							} else {
1328								mygroups.text( ( mygroups.text() >> 0 ) - 1 );
1329							}
1330						}
1331
1332					}
1333				);
1334			}
1335		});
1336		return false;
1337	} );
1338
1339	/** Button disabling ************************************************/
1340
1341	jq('#buddypress').on( 'click', '.pending', function() {
1342		return false;
1343	});
1344
1345	/** Registration ***********************************************/
1346
1347	if ( jq('body').hasClass('register') ) {
1348		var blog_checked = jq('#signup_with_blog');
1349
1350		// hide "Blog Details" block if not checked by default
1351		if ( ! blog_checked.prop('checked') ) {
1352			jq('#blog-details').toggle();
1353		}
1354
1355		// toggle "Blog Details" block whenever checkbox is checked
1356		blog_checked.change(function() {
1357			jq('#blog-details').toggle();
1358		});
1359	}
1360
1361	/** Private Messaging ******************************************/
1362
1363	/** Message search */
1364	jq('.message-search').on( 'click', function(event) {
1365		if ( jq(this).hasClass('no-ajax') ) {
1366			return;
1367		}
1368
1369		var target = jq(event.target),
1370			object;
1371
1372		if ( target.attr('type') === 'submit' || target.attr('type') === 'button' ) {
1373			object = 'messages';
1374
1375			bp_filter_request(
1376				object,
1377				jq.cookie('bp-' + object + '-filter'),
1378				jq.cookie('bp-' + object + '-scope'),
1379				'div.' + object, jq('#messages_search').val(),
1380				1,
1381				jq.cookie('bp-' + object + '-extras')
1382			);
1383
1384			return false;
1385		}
1386	});
1387
1388	/* AJAX send reply functionality */
1389	jq('#send_reply_button').click(
1390		function() {
1391			var order = jq('#messages_order').val() || 'ASC',
1392				offset  = jq('#message-recipients').offset(),
1393				button  = jq('#send_reply_button');
1394
1395			jq(button).addClass('loading');
1396
1397			jq.post( ajaxurl, {
1398				action: 'messages_send_reply',
1399				'cookie': bp_get_cookies(),
1400				'_wpnonce': jq('#send_message_nonce').val(),
1401
1402				'content': jq('#message_content').val(),
1403				'send_to': jq('#send_to').val(),
1404				'subject': jq('#subject').val(),
1405				'thread_id': jq('#thread_id').val()
1406			},
1407			function(response)
1408			{
1409				if ( response[0] + response[1] === '-1' ) {
1410					jq('#send-reply').prepend( response.substr( 2, response.length ) );
1411				} else {
1412					jq('#send-reply #message').remove();
1413					jq('#message_content').val('');
1414
1415					if ( 'ASC' === order ) {
1416						jq('#send-reply').before( response );
1417					} else {
1418						jq('#message-recipients').after( response );
1419						jq(window).scrollTop(offset.top);
1420					}
1421
1422					jq('.new-message').hide().slideDown( 200, function() {
1423						jq('.new-message').removeClass('new-message');
1424					});
1425				}
1426				jq(button).removeClass('loading');
1427			});
1428
1429			return false;
1430		}
1431	);
1432
1433	/* Marking private messages as read and unread */
1434	jq('#mark_as_read, #mark_as_unread').click(function() {
1435		var checkboxes_tosend = '',
1436			checkboxes = jq('#message-threads tr td input[type="checkbox"]'),
1437			currentClass, newClass, unreadCount, inboxCount, unreadCountDisplay, action,
1438			inboxcount, thread_count;
1439
1440		if ( 'mark_as_unread' === jq(this).attr('id') ) {
1441			currentClass = 'read';
1442			newClass = 'unread';
1443			unreadCount = 1;
1444			inboxCount = 0;
1445			unreadCountDisplay = 'inline';
1446			action = 'messages_markunread';
1447		} else {
1448			currentClass = 'unread';
1449			newClass = 'read';
1450			unreadCount = 0;
1451			inboxCount = 1;
1452			unreadCountDisplay = 'none';
1453			action = 'messages_markread';
1454		}
1455
1456		checkboxes.each( function(i) {
1457			if(jq(this).is(':checked')) {
1458				if ( jq('#m-' + jq(this).attr('value')).hasClass(currentClass) ) {
1459					checkboxes_tosend += jq(this).attr('value');
1460					jq('#m-' + jq(this).attr('value')).removeClass(currentClass);
1461					jq('#m-' + jq(this).attr('value')).addClass(newClass);
1462					thread_count = jq('#m-' + jq(this).attr('value') + ' td span.unread-count').html();
1463
1464					jq('#m-' + jq(this).attr('value') + ' td span.unread-count').html(unreadCount);
1465					jq('#m-' + jq(this).attr('value') + ' td span.unread-count').css('display', unreadCountDisplay);
1466
1467					inboxcount = jq('tr.unread').length;
1468
1469					jq('#user-messages span').html( inboxcount );
1470
1471					if ( i !== checkboxes.length - 1 ) {
1472						checkboxes_tosend += ',';
1473					}
1474				}
1475			}
1476		});
1477		jq.post( ajaxurl, {
1478			action: action,
1479			'thread_ids': checkboxes_tosend
1480		});
1481		return false;
1482	});
1483
1484	/* Selecting unread and read messages in inbox */
1485	jq( 'body.messages #item-body div.messages' ).on( 'change', '#message-type-select', function() {
1486		var selection   = this.value,
1487			checkboxes    = jq( 'td input[type="checkbox"]' ),
1488			checked_value = 'checked';
1489
1490		checkboxes.each( function(i) {
1491			checkboxes[i].checked = '';
1492		});
1493
1494		switch ( selection ) {
1495			case 'unread':
1496				checkboxes = jq('tr.unread td input[type="checkbox"]');
1497				break;
1498			case 'read':
1499				checkboxes = jq('tr.read td input[type="checkbox"]');
1500				break;
1501			case '':
1502				checked_value = '';
1503				break;
1504		}
1505
1506		checkboxes.each( function(i) {
1507			checkboxes[i].checked = checked_value;
1508		});
1509	});
1510
1511	/* Bulk delete messages */
1512	jq( 'body.messages #item-body div.messages' ).on( 'click', '.messages-options-nav a', function() {
1513		if ( -1 === jq.inArray( this.id, Array( 'delete_sentbox_messages', 'delete_inbox_messages' ) ) ) {
1514			return;
1515		}
1516
1517		checkboxes_tosend = '';
1518		checkboxes = jq('#message-threads tr td input[type="checkbox"]');
1519
1520		jq('#message').remove();
1521		jq(this).addClass('loading');
1522
1523		jq(checkboxes).each( function(i) {
1524			if( jq(this).is(':checked') ) {
1525				checkboxes_tosend += jq(this).attr('value') + ',';
1526			}
1527		});
1528
1529		if ( '' === checkboxes_tosend ) {
1530			jq(this).removeClass('loading');
1531			return false;
1532		}
1533
1534		jq.post( ajaxurl, {
1535			action: 'messages_delete',
1536			'thread_ids': checkboxes_tosend
1537		}, function(response) {
1538			if ( response[0] + response[1] === '-1' ) {
1539				jq('#message-threads').prepend( response.substr( 2, response.length ) );
1540			} else {
1541				jq('#message-threads').before( '<div id="message" class="updated"><p>' + response + '</p></div>' );
1542
1543				jq(checkboxes).each( function(i) {
1544					if( jq(this).is(':checked') ) {
1545						// We need to uncheck because message is only hidden
1546						// Otherwise, AJAX will be fired again with same data
1547						jq(this).attr( 'checked', false );
1548						jq(this).parent().parent().fadeOut(150);
1549					}
1550				});
1551			}
1552
1553			jq('#message').hide().slideDown(150);
1554			jq('#delete_inbox_messages, #delete_sentbox_messages').removeClass('loading');
1555		});
1556
1557		return false;
1558	});
1559
1560	/* Selecting/Deselecting all messages */
1561	jq('#select-all-messages').click(function(event) {
1562		if( this.checked ) {
1563			jq('.message-check').each(function() {
1564				this.checked = true;
1565			});
1566		} else {
1567			jq('.message-check').each(function() {
1568				this.checked = false;
1569			});
1570		}
1571	});
1572
1573	/* Make sure a 'Bulk Action' is selected before submitting the messages bulk action form */
1574	jq('#messages-bulk-manage').attr('disabled', 'disabled');
1575
1576	/* Remove the disabled attribute from the messages form submit button when bulk action has a value */
1577	jq('#messages-select').on('change', function(){
1578		jq('#messages-bulk-manage').attr('disabled', jq(this).val().length <= 0);
1579	});
1580
1581	/* Star action function */
1582	starAction = function() {
1583		var link = jq(this);
1584
1585		jq.post( ajaxurl, {
1586			action: 'messages_star',
1587			'message_id': link.data('message-id'),
1588			'star_status': link.data('star-status'),
1589			'nonce': link.data('star-nonce'),
1590			'bulk': link.data('star-bulk')
1591		},
1592		function(response) {
1593			if ( 1 === parseInt( response, 10 ) ) {
1594				if ( 'unstar' === link.data('star-status') ) {
1595					link.data('star-status', 'star');
1596					link.removeClass('message-action-unstar').addClass('message-action-star');
1597					link.find('.bp-screen-reader-text').text( BP_PM_Star.strings.text_star );
1598
1599					if ( 1 === BP_PM_Star.is_single_thread ) {
1600						link.prop('title', BP_PM_Star.strings.title_star );
1601					} else {
1602						link.prop('title', BP_PM_Star.strings.title_star_thread );
1603					}
1604
1605				} else {
1606					link.data('star-status', 'unstar');
1607					link.removeClass('message-action-star').addClass('message-action-unstar');
1608					link.find('.bp-screen-reader-text').text(BP_PM_Star.strings.text_unstar);
1609
1610					if ( 1 === BP_PM_Star.is_single_thread ) {
1611						link.prop('title', BP_PM_Star.strings.title_unstar );
1612					} else {
1613						link.prop('title', BP_PM_Star.strings.title_unstar_thread );
1614					}
1615				}
1616			}
1617		});
1618		return false;
1619	};
1620
1621	/* Star actions */
1622	jq('#message-threads').on('click', 'td.thread-star a', starAction );
1623	jq('#message-thread').on('click', '.message-star-actions a', starAction );
1624
1625	/* Star bulk manage - Show only the valid action based on the starred item. */
1626	jq('#message-threads td.bulk-select-check :checkbox').on('change', function() {
1627		var box = jq(this),
1628			star = box.closest('tr').find('.thread-star a');
1629
1630		if ( box.prop('checked') ) {
1631			if( 'unstar' === star.data('star-status') ) {
1632				BP_PM_Star.star_counter++;
1633			} else {
1634				BP_PM_Star.unstar_counter++;
1635			}
1636		} else {
1637			if( 'unstar' === star.data('star-status') ) {
1638				BP_PM_Star.star_counter--;
1639			} else {
1640				BP_PM_Star.unstar_counter--;
1641			}
1642		}
1643
1644		if ( BP_PM_Star.star_counter > 0 && parseInt( BP_PM_Star.unstar_counter, 10 ) === 0 ) {
1645			jq('option[value="star"]').hide();
1646		} else {
1647			jq('option[value="star"]').show();
1648		}
1649
1650		if ( BP_PM_Star.unstar_counter > 0 && parseInt( BP_PM_Star.star_counter, 10 ) === 0 ) {
1651			jq('option[value="unstar"]').hide();
1652		} else {
1653			jq('option[value="unstar"]').show();
1654		}
1655	});
1656
1657	/** Notifications **********************************************/
1658
1659	/* Selecting/Deselecting all notifications */
1660	jq('#select-all-notifications').click(function(event) {
1661		if( this.checked ) {
1662			jq('.notification-check').each(function() {
1663				this.checked = true;
1664			});
1665		} else {
1666			jq('.notification-check').each(function() {
1667				this.checked = false;
1668			});
1669		}
1670	});
1671
1672	/* Make sure a 'Bulk Action' is selected before submitting the form */
1673	jq('#notification-bulk-manage').attr('disabled', 'disabled');
1674
1675	/* Remove the disabled attribute from the form submit button when bulk action has a value */
1676	jq('#notification-select').on('change', function(){
1677		jq('#notification-bulk-manage').attr('disabled', jq(this).val().length <= 0);
1678	});
1679
1680	/* Close site wide notices in the sidebar */
1681	jq('#close-notice').on( 'click', function() {
1682		jq(this).addClass('loading');
1683		jq('#sidebar div.error').remove();
1684
1685		jq.post( ajaxurl, {
1686			action: 'messages_close_notice',
1687			'notice_id': jq('.notice').attr('rel').substr( 2, jq('.notice').attr('rel').length )
1688		},
1689		function(response) {
1690			jq('#close-notice').removeClass('loading');
1691
1692			if ( response[0] + response[1] === '-1' ) {
1693				jq('.notice').prepend( response.substr( 2, response.length ) );
1694				jq( '#sidebar div.error').hide().fadeIn( 200 );
1695			} else {
1696				jq('.notice').slideUp( 100 );
1697			}
1698		});
1699		return false;
1700	});
1701
1702	/* Toolbar & wp_list_pages JavaScript IE6 hover class */
1703	jq('#wp-admin-bar ul.main-nav li, #nav li').mouseover( function() {
1704		jq(this).addClass('sfhover');
1705	});
1706
1707	jq('#wp-admin-bar ul.main-nav li, #nav li').mouseout( function() {
1708		jq(this).removeClass('sfhover');
1709	});
1710
1711	/* Clear BP cookies on logout */
1712	jq('#wp-admin-bar-logout, a.logout').on( 'click', function() {
1713		jq.removeCookie('bp-activity-scope', {
1714			path: '/'
1715		});
1716		jq.removeCookie('bp-activity-filter', {
1717			path: '/'
1718		});
1719		jq.removeCookie('bp-activity-oldestpage', {
1720			path: '/'
1721		});
1722
1723		var objects = [ 'members', 'groups', 'blogs', 'forums' ];
1724		jq(objects).each( function(i) {
1725			jq.removeCookie('bp-' + objects[i] + '-scope', {
1726				path: '/'
1727			} );
1728			jq.removeCookie('bp-' + objects[i] + '-filter', {
1729				path: '/'
1730			} );
1731			jq.removeCookie('bp-' + objects[i] + '-extras', {
1732				path: '/'
1733			} );
1734		});
1735	});
1736
1737	/* if js is enabled then replace the no-js class by a js one */
1738	if( jq('body').hasClass('no-js') ) {
1739		jq('body').attr('class', jq('body').attr('class').replace( /no-js/,'js' ) );
1740	}
1741
1742	/** Activity HeartBeat ************************************************/
1743
1744	// Set the interval and the namespace event
1745	if ( typeof wp !== 'undefined' && typeof wp.heartbeat !== 'undefined' && typeof BP_DTheme.pulse !== 'undefined' ) {
1746
1747		wp.heartbeat.interval( Number( BP_DTheme.pulse ) );
1748
1749		jq.fn.extend({
1750			'heartbeat-send': function() {
1751				return this.bind( 'heartbeat-send.buddypress' );
1752			}
1753		});
1754	}
1755
1756	// Set the last id to request after
1757	var first_item_recorded = 0;
1758	jq( document ).on( 'heartbeat-send.buddypress', function( e, data ) {
1759
1760		first_item_recorded = 0;
1761
1762		// First row is default latest activity id
1763		if ( jq( '#buddypress ul.activity-list li' ).first().prop( 'id' ) ) {
1764			// getting the timestamp
1765			timestamp = jq( '#buddypress ul.activity-list li' ).first().prop( 'class' ).match( /date-recorded-([0-9]+)/ );
1766
1767			if ( timestamp ) {
1768				first_item_recorded = timestamp[1];
1769			}
1770		}
1771
1772		if ( 0 === activity_last_recorded || Number( first_item_recorded ) > activity_last_recorded ) {
1773			activity_last_recorded = Number( first_item_recorded );
1774		}
1775
1776		data.bp_activity_last_recorded = activity_last_recorded;
1777
1778		last_recorded_search = bp_get_querystring('s');
1779
1780		if ( last_recorded_search ) {
1781			data.bp_activity_last_recorded_search_terms = last_recorded_search;
1782		}
1783	});
1784
1785	// Increment newest_activities and activity_last_recorded if data has been returned
1786	jq( document ).on( 'heartbeat-tick', function( e, data ) {
1787
1788		// Only proceed if we have newest activities
1789		if ( ! data.bp_activity_newest_activities ) {
1790			return;
1791		}
1792
1793		newest_activities = data.bp_activity_newest_activities.activities + newest_activities;
1794		activity_last_recorded  = Number( data.bp_activity_newest_activities.last_recorded );
1795
1796		if ( jq( '#buddypress ul.activity-list li' ).first().hasClass( 'load-newest' ) ) {
1797			return;
1798		}
1799
1800		jq( '#buddypress ul.activity-list' ).prepend( '<li class="load-newest"><a href="#newest">' + BP_DTheme.newest + '</a></li>' );
1801	});
1802});
1803
1804/* Setup activity scope and filter based on the current cookie settings. */
1805function bp_init_activity() {
1806	/* Reset the page */
1807	jq.cookie( 'bp-activity-oldestpage', 1, {
1808		path: '/'
1809	} );
1810
1811	if ( null !== jq.cookie('bp-activity-filter') && jq('#activity-filter-select').length ) {
1812		jq('#activity-filter-select select option[value="' + jq.cookie('bp-activity-filter') + '"]').prop( 'selected', true );
1813	}
1814
1815	/* Activity Tab Set */
1816	if ( null !== jq.cookie('bp-activity-scope') && jq('.activity-type-tabs').length ) {
1817		jq('.activity-type-tabs li').each( function() {
1818			jq(this).removeClass('selected');
1819		});
1820		jq('#activity-' + jq.cookie('bp-activity-scope') + ', .item-list-tabs li.current').addClass('selected');
1821	}
1822}
1823
1824/* Setup object scope and filter based on the current cookie settings for the object. */
1825function bp_init_objects(objects) {
1826	jq(objects).each( function(i) {
1827		if ( null !== jq.cookie('bp-' + objects[i] + '-filter') && jq('#' + objects[i] + '-order-select select').length ) {
1828			jq('#' + objects[i] + '-order-select select option[value="' + jq.cookie('bp-' + objects[i] + '-filter') + '"]').prop( 'selected', true );
1829		}
1830
1831		if ( null !== jq.cookie('bp-' + objects[i] + '-scope') && jq('div.' + objects[i]).length ) {
1832			jq('.item-list-tabs li').each( function() {
1833				jq(this).removeClass('selected');
1834			});
1835			jq('#' + objects[i] + '-' + jq.cookie('bp-' + objects[i] + '-scope') + ', #object-nav li.current').addClass('selected');
1836		}
1837	});
1838}
1839
1840/* Filter the current content list (groups/members/blogs/topics) */
1841function bp_filter_request( object, filter, scope, target, search_terms, page, extras, caller, template ) {
1842	if ( 'activity' === object ) {
1843		return false;
1844	}
1845
1846	if ( null === scope ) {
1847		scope = 'all';
1848	}
1849
1850	/* Save the settings we want to remain persistent to a cookie */
1851	jq.cookie( 'bp-' + object + '-scope', scope, {
1852		path: '/'
1853	} );
1854	jq.cookie( 'bp-' + object + '-filter', filter, {
1855		path: '/'
1856	} );
1857	jq.cookie( 'bp-' + object + '-extras', extras, {
1858		path: '/'
1859	} );
1860
1861	/* Set the correct selected nav and filter */
1862	jq('.item-list-tabs li').each( function() {
1863		jq(this).removeClass('selected');
1864	});
1865	jq('#' + object + '-' + scope + ', #object-nav li.current').addClass('selected');
1866	jq('.item-list-tabs li.selected').addClass('loading');
1867	jq('.item-list-tabs select option[value="' + filter + '"]').prop( 'selected', true );
1868
1869	if ( 'friends' === object || 'group_members' === object ) {
1870		object = 'members';
1871	}
1872
1873	if ( bp_ajax_request ) {
1874		bp_ajax_request.abort();
1875	}
1876
1877	bp_ajax_request = jq.post( ajaxurl, {
1878		action: object + '_filter',
1879		'cookie': bp_get_cookies(),
1880		'object': object,
1881		'filter': filter,
1882		'search_terms': search_terms,
1883		'scope': scope,
1884		'page': page,
1885		'extras': extras,
1886		'template': template
1887	},
1888	function(response)
1889	{
1890		/* animate to top if called from bottom pagination */
1891		if ( caller === 'pag-bottom' && jq('#subnav').length ) {
1892			var top = jq('#subnav').parent();
1893			jq('html,body').animate({scrollTop: top.offset().top}, 'slow', function() {
1894				jq(target).fadeOut( 100, function() {
1895					jq(this).html(response);
1896					jq(this).fadeIn(100);
1897				});
1898			});
1899
1900		} else {
1901			jq(target).fadeOut( 100, function() {
1902				jq(this).html(response);
1903				jq(this).fadeIn(100);
1904			});
1905		}
1906
1907		jq('.item-list-tabs li.selected').removeClass('loading');
1908	});
1909}
1910
1911/* Activity Loop Requesting */
1912function bp_activity_request(scope, filter) {
1913	/* Save the type and filter to a session cookie */
1914	jq.cookie( 'bp-activity-scope', scope, {
1915		path: '/'
1916	} );
1917	jq.cookie( 'bp-activity-filter', filter, {
1918		path: '/'
1919	} );
1920	jq.cookie( 'bp-activity-oldestpage', 1, {
1921		path: '/'
1922	} );
1923
1924	/* Remove selected and loading classes from tabs */
1925	jq('.item-list-tabs li').each( function() {
1926		jq(this).removeClass('selected loading');
1927	});
1928	/* Set the correct selected nav and filter */
1929	jq('#activity-' + scope + ', .item-list-tabs li.current').addClass('selected');
1930	jq('#object-nav.item-list-tabs li.selected, div.activity-type-tabs li.selected').addClass('loading');
1931	jq('#activity-filter-select select option[value="' + filter + '"]').prop( 'selected', true );
1932
1933	/* Reload the activity stream based on the selection */
1934	jq('.widget_bp_activity_widget h2 span.ajax-loader').show();
1935
1936	if ( bp_ajax_request ) {
1937		bp_ajax_request.abort();
1938	}
1939
1940	bp_ajax_request = jq.post( ajaxurl, {
1941		action: 'activity_widget_filter',
1942		'cookie': bp_get_cookies(),
1943		'_wpnonce_activity_filter': jq('#_wpnonce_activity_filter').val(),
1944		'scope': scope,
1945		'filter': filter
1946	},
1947	function(response)
1948	{
1949		jq('.widget_bp_activity_widget h2 span.ajax-loader').hide();
1950
1951		jq('div.activity').fadeOut( 100, function() {
1952			jq(this).html(response.contents);
1953			jq(this).fadeIn(100);
1954
1955			/* Selectively hide comments */
1956			bp_legacy_theme_hide_comments();
1957		});
1958
1959		/* Update the feed link */
1960		if ( null !== response.feed_url ) {
1961			jq('.directory #subnav li.feed a, .home-page #subnav li.feed a').attr('href', response.feed_url);
1962		}
1963
1964		jq('.item-list-tabs li.selected').removeClass('loading');
1965
1966	}, 'json' );
1967}
1968
1969/* Hide long lists of activity comments, only show the latest five root comments. */
1970function bp_legacy_theme_hide_comments() {
1971	var comments_divs = jq('div.activity-comments'),
1972		parent_li, comment_lis, comment_count;
1973
1974	if ( !comments_divs.length ) {
1975		return false;
1976	}
1977
1978	comments_divs.each( function() {
1979		if ( jq(this).children('ul').children('li').length < 5 ) {
1980			return;
1981		}
1982
1983		comments_div = jq(this);
1984		parent_li = comments_div.parents('#activity-stream > li');
1985		comment_lis = jq(this).children('ul').children('li');
1986		comment_count = ' ';
1987
1988		if ( jq('#' + parent_li.attr('id') + ' a.acomment-reply span').length ) {
1989			comment_count = jq('#' + parent_li.attr('id') + ' a.acomment-reply span').html();
1990		}
1991
1992		comment_lis.each( function(i) {
1993			/* Show the latest 5 root comments */
1994			if ( i < comment_lis.length - 5 ) {
1995				jq(this).addClass('hidden');
1996				jq(this).toggle();
1997
1998				if ( !i ) {
1999					jq(this).before( '<li class="show-all"><a href="#' + parent_li.attr('id') + '/show-all/" title="' + BP_DTheme.show_all_comments + '">' + BP_DTheme.show_x_comments.replace( '%d', comment_count ) + '</a></li>' );
2000				}
2001			}
2002		});
2003
2004	});
2005}
2006
2007/* Helper Functions */
2008
2009function checkAll() {
2010	var checkboxes = document.getElementsByTagName('input'),
2011		i;
2012
2013	for(i=0; i<checkboxes.length; i++) {
2014		if(checkboxes[i].type === 'checkbox') {
2015			if($('check_all').checked === '') {
2016				checkboxes[i].checked = '';
2017			}
2018			else {
2019				checkboxes[i].checked = 'checked';
2020			}
2021		}
2022	}
2023}
2024
2025/**
2026 * Deselects any select options or input options for the specified field element.
2027 *
2028 * @param {String} container HTML ID of the field
2029 * @since BuddyPress (1.2.0)
2030 */
2031function clear( container ) {
2032	container = document.getElementById( container );
2033	if ( ! container ) {
2034		return;
2035	}
2036
2037	var radioButtons = container.getElementsByTagName( 'INPUT' ),
2038		options = container.getElementsByTagName( 'OPTION' ),
2039		i       = 0;
2040
2041	if ( radioButtons ) {
2042		for ( i = 0; i < radioButtons.length; i++ ) {
2043			radioButtons[i].checked = '';
2044		}
2045	}
2046
2047	if ( options ) {
2048		for ( i = 0; i < options.length; i++ ) {
2049			options[i].selected = false;
2050		}
2051	}
2052}
2053
2054/* Returns a querystring of BP cookies (cookies beginning with 'bp-') */
2055function bp_get_cookies() {
2056	var allCookies = document.cookie.split(';'),  // get all cookies and split into an array
2057		bpCookies      = {},
2058		cookiePrefix   = 'bp-',
2059		i, cookie, delimiter, name, value;
2060
2061	// loop through cookies
2062	for (i = 0; i < allCookies.length; i++) {
2063		cookie    = allCookies[i];
2064		delimiter = cookie.indexOf('=');
2065		name      = jq.trim( unescape( cookie.slice(0, delimiter) ) );
2066		value     = unescape( cookie.slice(delimiter + 1) );
2067
2068		// if BP cookie, store it
2069		if ( name.indexOf(cookiePrefix) === 0 ) {
2070			bpCookies[name] = value;
2071		}
2072	}
2073
2074	// returns BP cookies as querystring
2075	return encodeURIComponent( jq.param(bpCookies) );
2076}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.