🎨 Use better implementation, fix comment
[Sone.git] / src / main / resources / static / javascript / sone.js
1 /* Sone JavaScript functions. */
2
3 function ajaxGet(url, data, successCallback, errorCallback) {
4         (function(url, data, successCallback, errorCallback) {
5                 $.ajax({"cache": false, "type": "GET", "url": url, "data": data, "dataType": "json", "success": function(data, textStatus) {
6                         ajaxSuccess();
7                         if (typeof successCallback != "undefined") {
8                                 successCallback(data, textStatus);
9                         }
10                 }, "error": function(xmlHttpRequest) {
11                         if (xmlHttpRequest.status === 403) {
12                                 notLoggedIn = true;
13                         }
14                         if (typeof errorCallback != "undefined") {
15                                 errorCallback();
16                         } else {
17                                 ajaxError();
18                         }
19                 }});
20         })(url, data, successCallback, errorCallback);
21 }
22
23 function registerInputTextareaSwap(inputElement, defaultText, inputFieldName, optional, dontUseTextarea) {
24         $(inputElement).each(function() {
25                 var textarea = $(dontUseTextarea ? "<input type=\"text\" name=\"" + inputFieldName + "\">" : "<textarea name=\"" + inputFieldName + "\"></textarea>").blur(function() {
26                         if ($(this).val() === "") {
27                                 $(this).hide();
28                                 var inputField = $(this).data("inputField");
29                                 inputField.show().removeAttr("disabled").addClass("default");
30                                 inputField.val(defaultText);
31                         }
32                 }).hide().data("inputField", $(this)).val($(this).val());
33                 $(this).data("textarea", textarea).after(textarea);
34                 (function(inputField, textarea) {
35                         inputField.focus(function() {
36                                 $(this).hide().prop("disabled", "disabled");
37                                 /* no, show(), â€śdisplay: block” is not what I need. */
38                                 textarea.prop("style", "display: inline").focus();
39                         });
40                         if (inputField.val() === "") {
41                                 inputField.addClass("default");
42                                 inputField.val(defaultText);
43                         } else {
44                                 inputField.hide().prop("disabled", "disabled");
45                                 textarea.show();
46                         }
47                         $(inputField.get(0).form).submit(function() {
48                                 inputField.prop("disabled", "disabled");
49                                 if (!optional && (textarea.val() === "")) {
50                                         inputField.removeAttr("disabled").focus();
51                                         return false;
52                                 }
53                         });
54                 })($(this), textarea);
55         });
56 }
57
58 /**
59  * Adds a â€ścomment” link to all status lines contained in the given element.
60  *
61  * @param postId
62  *            The ID of the post
63  * @param element
64  *            The element to add a â€ścomment” link to
65  */
66 function addCommentLink(postId, author, element, insertAfterThisElement) {
67         if (($(element).find(".show-reply-form").length > 0) || (getPostElement(element).find(".create-reply").length === 0)) {
68                 return;
69         }
70         (function(postId, author, insertAfterThisElement) {
71                 var separator = $("<span> Â· </span>").addClass("separator");
72                 getTranslation("WebInterface.Button.Comment", function(text) {
73                         var commentElement = $("<div><span>" + text + "</span></div>").addClass("show-reply-form").click(function() {
74                                 var replyElement = sone.find(".post#post-" + postId + " .create-reply");
75                                 replyElement.removeClass("hidden");
76                                 replyElement.removeClass("light");
77                                 (function(replyElement) {
78                                         replyElement.find(":input.reply-input").blur(function() {
79                                                 if ($(this).hasClass("default")) {
80                                                         replyElement.addClass("light");
81                                                 }
82                                         }).focus(function() {
83                                                 replyElement.removeClass("light");
84                                         });
85                                 })(replyElement);
86                                 var textArea = replyElement.find(":input.reply-input").focus().data("textarea");
87                                 if (author !== getCurrentSoneId()) {
88                                         textArea.val(textArea.val() + "@sone://" + author + " ");
89                                 }
90                         });
91                         $(insertAfterThisElement).after(commentElement.clone(true));
92                         $(insertAfterThisElement).after(separator);
93                 });
94         })(postId, author, insertAfterThisElement);
95 }
96
97 var translations = {};
98
99 /**
100  * Retrieves the translation for the given key and calls the callback function.
101  * The callback function takes a single parameter, the translated string.
102  *
103  * @param key
104  *            The key of the translation string
105  * @param callback
106  *            The callback function
107  */
108 function getTranslation(key, callback) {
109         if (key in translations) {
110                 callback(translations[key]);
111                 return;
112         }
113         ajaxGet("getTranslation.ajax", {"key": key}, function(data) {
114                 if ((data != null) && data.success) {
115                         translations[key] = data.value;
116                         callback(data.value);
117                 }
118         });
119 }
120
121 /**
122  * Filters the given Sone ID, replacing all â€ś~” characters by an underscore.
123  *
124  * @param soneId
125  *            The Sone ID to filter
126  * @returns The filtered Sone ID
127  */
128 function filterSoneId(soneId) {
129         return soneId.replace(/[^a-zA-Z0-9-]/g, "_");
130 }
131
132 /**
133  * Updates the status of the given Sone.
134  *
135  * @param soneId
136  *            The ID of the Sone to update
137  * @param status
138  *            The status of the Sone (“idle”, â€śunknown”, â€śinserting”,
139  *            â€śdownloading”)
140  * @param modified
141  *            Whether the Sone is modified
142  * @param locked
143  *            Whether the Sone is locked
144  * @param lastUpdated
145  *            The date and time of the last update (formatted for display)
146  */
147 function updateSoneStatus(soneId, name, status, modified, locked, lastUpdated, lastUpdatedText) {
148     var updateSone = sone.find(".sone." + filterSoneId(soneId));
149         updateSone.toggleClass("unknown", status === "unknown").
150                 toggleClass("idle", status === "idle").
151                 toggleClass("inserting", status === "inserting").
152                 toggleClass("downloading", status === "downloading").
153                 toggleClass("modified", modified);
154         updateSone.find(".lock").toggleClass("hidden", locked);
155         updateSone.find(".unlock").toggleClass("hidden", !locked);
156         if (lastUpdated != null) {
157                 updateSone.find(".last-update span.time").prop("title", lastUpdated).text(lastUpdatedText);
158         } else {
159                 getTranslation("View.Sone.Text.UnknownDate", function(unknown) {
160                         updateSone.find(".last-update span.time").text(unknown);
161                 });
162         }
163         updateSone.find(".profile-link a").text(name);
164 }
165
166 /**
167  * Enhances a â€śdelete” button so that the confirmation is done on the same page.
168  *
169  * @param button
170  *            The button element
171  * @param text
172  *            The text to show on the button
173  * @param deleteCallback
174  *            The callback that actually deletes something
175  */
176 function enhanceDeleteButton(button, text, deleteCallback) {
177         (function(button) {
178                 var newButton = $("<button></button>").addClass("confirm").hide().text(text).click(function() {
179                         $(this).fadeOut("slow");
180                         deleteCallback();
181                         return false;
182                 }).insertAfter(button);
183                 (function(button, newButton) {
184                         button.click(function() {
185                                 button.fadeOut("slow", function() {
186                                         newButton.fadeIn("slow");
187                                         $(document).one("click", function() {
188                                                 if (this !== newButton.get(0)) {
189                                                         newButton.fadeOut(function() {
190                                                                 button.fadeIn();
191                                                         });
192                                                 }
193                                         });
194                                 });
195                                 return false;
196                         });
197                 })(button, newButton);
198         })($(button));
199 }
200
201 /**
202  * Enhances a post’s â€śdelete” button.
203  *
204  * @param button
205  *            The button element
206  * @param postId
207  *            The ID of the post to delete
208  * @param text
209  *            The text to replace the button with
210  */
211 function enhanceDeletePostButton(button, postId, text) {
212         enhanceDeleteButton(button, text, function() {
213                 ajaxGet("deletePost.ajax", { "post": postId, "formPassword": getFormPassword() }, function(data) {
214                         if (data == null) {
215                                 return;
216                         }
217                         if (data.success) {
218                                 sone.find(".post#post-" + postId).slideUp();
219                         } else if (data.error === "invalid-post-id") {
220                                 /* pretend the post is already gone. */
221                                 getPost(postId).slideUp();
222                         } else if (data.error === "auth-required") {
223                                 alert("You need to be logged in.");
224                         } else if (data.error === "not-authorized") {
225                                 alert("You are not allowed to delete this post.");
226                         }
227                 }, function() {
228                         /* ignore error. */
229                 });
230         });
231 }
232
233 /**
234  * Enhances a reply’s â€śdelete” button.
235  *
236  * @param button
237  *            The button element
238  * @param replyId
239  *            The ID of the reply to delete
240  * @param text
241  *            The text to replace the button with
242  */
243 function enhanceDeleteReplyButton(button, replyId, text) {
244         enhanceDeleteButton(button, text, function() {
245                 ajaxGet("deleteReply.ajax", { "reply": replyId, "formPassword": sone.find("#formPassword").text() }, function(data) {
246                         if (data == null) {
247                                 return;
248                         }
249                         if (data.success) {
250                                 sone.find(".reply#reply-" + replyId).slideUp();
251                         } else if (data.error === "invalid-reply-id") {
252                                 /* pretend the reply is already gone. */
253                                 getReply(replyId).slideUp();
254                         } else if (data.error === "auth-required") {
255                                 alert("You need to be logged in.");
256                         } else if (data.error === "not-authorized") {
257                                 alert("You are not allowed to delete this reply.");
258                         }
259                 }, function() {
260                         /* ignore error. */
261                 });
262         });
263 }
264
265 function getFormPassword() {
266         return sone.find("#formPassword").text();
267 }
268
269 /**
270  * Returns the element of the Sone with the given ID.
271  *
272  * @param soneId
273  *            The ID of the Sone
274  * @returns All Sone elements with the given ID
275  */
276 function getSone(soneId) {
277         return sone.find(".sone").filter(function() {
278                 return $(".id", this).text() === soneId;
279         });
280 }
281
282 function getSoneElement(element) {
283         return $(element).closest(".sone");
284 }
285
286 /**
287  * Returns the ID of the sone of the context menu that contains the given
288  * element.
289  *
290  * @param element
291  *            The element within a context menu to get the Sone ID for
292  * @return The Sone ID
293  */
294 function getMenuSone(element) {
295         return $(element).closest(".sone-menu").find(".sone-menu-id").text();
296 }
297
298 /**
299  * Generates a list of Sones by concatening the names of the given sones with a
300  * comma.
301  *
302  * @param sones
303  *            The sones to format
304  * @returns {String} The created string
305  */
306 function generateSoneList(sones) {
307         return sones.reduce((soneList, sone) => soneList + ", " + sone.name, "").substring(2)
308 }
309
310 /**
311  * Returns the ID of the Sone that this element belongs to.
312  *
313  * @param element
314  *            The element to locate the matching Sone ID for
315  * @returns The ID of the Sone, or undefined
316  */
317 function getSoneId(element) {
318         return getSoneElement(element).find(".id").text();
319 }
320
321 /**
322  * Returns the element of the post with the given ID.
323  *
324  * @param postId
325  *            The ID of the post
326  * @returns The element of the post
327  */
328 function getPost(postId) {
329         return sone.find(".post#post-" + postId);
330 }
331
332 function getPostElement(element) {
333         return $(element).closest(".post");
334 }
335
336 function getPostId(element) {
337         return getPostElement(element).prop("id").substr(5);
338 }
339
340 function getPostTime(element) {
341         return getPostElement(element).find(".post-time").text();
342 }
343
344 /**
345  * Returns the author of the post the given element belongs to.
346  *
347  * @param element
348  *            The element whose post to get the author for
349  * @returns The ID of the authoring Sone
350  */
351 function getPostAuthor(element) {
352         return getPostElement(element).find(".post-author").text();
353 }
354
355 /**
356  * Returns the element of the reply with the given ID.
357  *
358  * @param replyId
359  *            The ID of the reply
360  * @returns The element of the reply
361  */
362 function getReply(replyId) {
363         return sone.find(".reply#reply-" + replyId);
364 }
365
366 function getReplyElement(element) {
367         return $(element).closest(".reply");
368 }
369
370 function getReplyId(element) {
371         return getReplyElement(element).prop("id").substr(6);
372 }
373
374 function getReplyTime(element) {
375         return getReplyElement(element).find(".reply-time").text();
376 }
377
378 /**
379  * Returns the author of the reply the given element belongs to.
380  *
381  * @param element
382  *            The element whose reply to get the author for
383  * @returns The ID of the authoring Sone
384  */
385 function getReplyAuthor(element) {
386         return getReplyElement(element).find(".reply-author").text();
387 }
388
389 /**
390  * Returns the notification with the given ID.
391  *
392  * @param notificationId
393  *            The ID of the notification
394  * @returns The notification element
395  */
396 function getNotification(notificationId) {
397         return sone.find("#notification-area .notification#" + notificationId);
398 }
399
400 /**
401  * Returns the notification element closest to the given element.
402  *
403  * @param element
404  *            The element to get the closest notification of
405  * @return The closest notification element
406  */
407 function getNotificationElement(element) {
408         return $(element).closest(".notification");
409 }
410
411 /**
412  * Returns the ID of the notification element.
413  *
414  * @param notificationElement
415  *            The notification element
416  * @returns The ID of the notification
417  */
418 function getNotificationId(notificationElement) {
419         return $(notificationElement).prop("id");
420 }
421
422 /**
423  * Returns the time the notification was last updated.
424  *
425  * @param notificationElement
426  *            The notification element
427  * @returns The last update time of the notification
428  */
429 function getNotificationLastUpdatedTime(notificationElement) {
430         return $(notificationElement).prop("lastUpdatedTime");
431 }
432
433 function likePost(postId) {
434         ajaxGet("like.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data) {
435                 if ((data == null) || !data.success) {
436                         return;
437                 }
438                 sone.find(".post#post-" + postId + " > .inner-part > .status-line .like").addClass("hidden");
439                 sone.find(".post#post-" + postId + " > .inner-part > .status-line .unlike").removeClass("hidden");
440                 updatePostLikes(postId);
441         }, function() {
442                 /* ignore error. */
443         });
444 }
445
446 function unlikePost(postId) {
447         ajaxGet("unlike.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data) {
448                 if ((data == null) || !data.success) {
449                         return;
450                 }
451                 sone.find(".post#post-" + postId + " > .inner-part > .status-line .unlike").addClass("hidden");
452                 sone.find(".post#post-" + postId + " > .inner-part > .status-line .like").removeClass("hidden");
453                 updatePostLikes(postId);
454         }, function() {
455                 /* ignore error. */
456         });
457 }
458
459 function updatePostLikes(postId) {
460         ajaxGet("getLikes.ajax", { "type": "post", "post": postId }, function(data) {
461                 if ((data != null) && data.success) {
462                         sone.find(".post#post-" + postId + " > .inner-part > .status-line .likes").toggleClass("hidden", data.likes === 0);
463                         sone.find(".post#post-" + postId + " > .inner-part > .status-line .likes span.like-count").text(data.likes);
464                         sone.find(".post#post-" + postId + " > .inner-part > .status-line .likes > span").prop("title", generateSoneList(data.sones));
465                 }
466         }, function() {
467                 /* ignore error. */
468         });
469 }
470
471 function likeReply(replyId) {
472         ajaxGet("like.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data) {
473                 if ((data == null) || !data.success) {
474                         return;
475                 }
476                 sone.find(".reply#reply-" + replyId + " .status-line .like").addClass("hidden");
477                 sone.find(".reply#reply-" + replyId + " .status-line .unlike").removeClass("hidden");
478                 updateReplyLikes(replyId);
479         }, function() {
480                 /* ignore error. */
481         });
482 }
483
484 function unlikeReply(replyId) {
485         ajaxGet("unlike.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data) {
486                 if ((data == null) || !data.success) {
487                         return;
488                 }
489                 sone.find(".reply#reply-" + replyId + " .status-line .unlike").addClass("hidden");
490                 sone.find(".reply#reply-" + replyId + " .status-line .like").removeClass("hidden");
491                 updateReplyLikes(replyId);
492         }, function() {
493                 /* ignore error. */
494         });
495 }
496
497 /**
498  * Trusts the Sone with the given ID.
499  *
500  * @param soneId
501  *            The ID of the Sone to trust
502  */
503 function trustSone(soneId) {
504         ajaxGet("trustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data) {
505                 if ((data != null) && data.success) {
506                         updateTrustControls(soneId, data.trustValue);
507                 }
508         });
509 }
510
511 /**
512  * Distrusts the Sone with the given ID, i.e. assigns a negative trust value.
513  *
514  * @param soneId
515  *            The ID of the Sone to distrust
516  */
517 function distrustSone(soneId) {
518         ajaxGet("distrustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data) {
519                 if ((data != null) && data.success) {
520                         updateTrustControls(soneId, data.trustValue);
521                 }
522         });
523 }
524
525 /**
526  * Untrusts the Sone with the given ID, i.e. removes any trust assignment.
527  *
528  * @param soneId
529  *            The ID of the Sone to untrust
530  */
531 function untrustSone(soneId) {
532         ajaxGet("untrustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data) {
533                 if ((data != null) && data.success) {
534                         updateTrustControls(soneId, data.trustValue);
535                 }
536         });
537 }
538
539 /**
540  * Updates the trust controls for all posts and replies of the given Sone,
541  * according to the given trust value.
542  *
543  * @param soneId
544  *            The ID of the Sone to update all trust controls for
545  * @param trustValue
546  *            The trust value for the Sone
547  */
548 function updateTrustControls(soneId, trustValue) {
549         sone.find(".post").each(function() {
550                 if (getPostAuthor(this) === soneId) {
551                         getPostElement(this).find(".post-trust").toggleClass("hidden", trustValue != null);
552                         getPostElement(this).find(".post-distrust").toggleClass("hidden", trustValue != null);
553                         getPostElement(this).find(".post-untrust").toggleClass("hidden", trustValue == null);
554                 }
555         });
556         sone.find(".reply").each(function() {
557                 if (getReplyAuthor(this) === soneId) {
558                         getReplyElement(this).find(".reply-trust").toggleClass("hidden", trustValue != null);
559                         getReplyElement(this).find(".reply-distrust").toggleClass("hidden", trustValue != null);
560                         getReplyElement(this).find(".reply-untrust").toggleClass("hidden", trustValue == null);
561                 }
562         });
563 }
564
565 /**
566  * Bookmarks the post with the given ID.
567  *
568  * @param postId
569  *            The ID of the post to bookmark
570  */
571 function bookmarkPost(postId) {
572         (function(postId) {
573                 ajaxGet("bookmark.ajax", {"formPassword": getFormPassword(), "type": "post", "post": postId}, function(data) {
574                         if ((data != null) && data.success) {
575                                 getPost(postId).find(".bookmark").toggleClass("hidden", true);
576                                 getPost(postId).find(".unbookmark").toggleClass("hidden", false);
577                         }
578                 });
579         })(postId);
580 }
581
582 /**
583  * Unbookmarks the post with the given ID.
584  *
585  * @param postId
586  *            The ID of the post to unbookmark
587  */
588 function unbookmarkPost(postId) {
589         ajaxGet("unbookmark.ajax", {"formPassword": getFormPassword(), "type": "post", "post": postId}, function(data) {
590                 if ((data != null) && data.success) {
591                         getPost(postId).find(".bookmark").toggleClass("hidden", false);
592                         getPost(postId).find(".unbookmark").toggleClass("hidden", true);
593                 }
594         });
595 }
596
597 function updateReplyLikes(replyId) {
598         ajaxGet("getLikes.ajax", { "type": "reply", "reply": replyId }, function(data) {
599                 if ((data != null) && data.success) {
600                         sone.find(".reply#reply-" + replyId + " .status-line .likes").toggleClass("hidden", data.likes === 0);
601                         sone.find(".reply#reply-" + replyId + " .status-line .likes span.like-count").text(data.likes);
602                         sone.find(".reply#reply-" + replyId + " .status-line .likes > span").prop("title", generateSoneList(data.sones));
603                 }
604         }, function() {
605                 /* ignore error. */
606         });
607 }
608
609 /**
610  * Posts a reply and calls the given callback when the request finishes.
611  *
612  * @param sender
613  *            The ID of the sender
614  * @param postId
615  *            The ID of the post the reply refers to
616  * @param text
617  *            The text to post
618  * @param callbackFunction
619  *            The callback function to call when the request finishes (takes 3
620  *            parameters: success, error, replyId)
621  */
622 function postReply(sender, postId, text, callbackFunction) {
623         ajaxGet("createReply.ajax", { "formPassword" : getFormPassword(), "sender": sender, "post" : postId, "text": text }, function(data) {
624                 if (data == null) {
625                         /* TODO - show error */
626                         return;
627                 }
628                 if (data.success) {
629                         callbackFunction(true, null, data.reply, data.sone);
630                 } else {
631                         callbackFunction(false, data.error);
632                 }
633         }, function() {
634                 /* ignore error. */
635         });
636 }
637
638 /**
639  * Ajaxifies the given Sone by enhancing all eligible elements with AJAX.
640  *
641  * @param soneElement
642  *            The Sone to ajaxify
643  */
644 function ajaxifySone(soneElement) {
645         /*
646          * convert all â€śfollow”, â€śunfollow”, â€ślock”, and â€śunlock” links to something
647          * nicer.
648          */
649         $(".follow", soneElement).submit(function() {
650                 var followElement = this;
651                 ajaxGet("followSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
652                         $(followElement).addClass("hidden");
653                         $(followElement).parent().find(".unfollow").removeClass("hidden");
654                 });
655                 return false;
656         });
657         $(".unfollow", soneElement).submit(function() {
658                 var unfollowElement = this;
659                 ajaxGet("unfollowSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
660                         $(unfollowElement).addClass("hidden");
661                         $(unfollowElement).parent().find(".follow").removeClass("hidden");
662                 });
663                 return false;
664         });
665         $(".lock", soneElement).submit(function() {
666                 var lockElement = this;
667                 ajaxGet("lockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
668                         $(lockElement).addClass("hidden");
669                         $(lockElement).parent().find(".unlock").removeClass("hidden");
670                 });
671                 return false;
672         });
673         $(".unlock", soneElement).submit(function() {
674                 var unlockElement = this;
675                 ajaxGet("unlockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
676                         $(unlockElement).addClass("hidden");
677                         $(unlockElement).parent().find(".lock").removeClass("hidden");
678                 });
679                 return false;
680         });
681
682         /* mark Sone as known when clicking it. */
683         $(soneElement).click(function() {
684                 markSoneAsKnown(this);
685         });
686 }
687
688 /**
689  * Ajaxifies the given post by enhancing all eligible elements with AJAX.
690  *
691  * @param postElement
692  *            The post element to ajaxify
693  */
694 function ajaxifyPost(postElement) {
695         $(postElement).find("form").submit(function() {
696                 return false;
697         });
698         $(postElement).find(".create-reply button:submit").click(function() {
699                 var button = $(this);
700                 button.prop("disabled", "disabled");
701                 var sender = $(this.form).find(":input[name=sender]").val();
702                 var inputField = $(this.form).find(":input[name=text]:enabled").get(0);
703                 var postId = getPostId(this);
704                 var text = $(inputField).val();
705                 (function(sender, postId, text, inputField) {
706                         postReply(sender, postId, text, function(success, error, replyId, soneId) {
707                                 if (success) {
708                                         $(inputField).val("");
709                                         loadNewReply(replyId, soneId, postId);
710                                         sone.find(".post#post-" + postId + " .create-reply").addClass("hidden");
711                                         sone.find(".post#post-" + postId + " .create-reply .sender").hide();
712                                         sone.find(".post#post-" + postId + " .create-reply .select-sender").show();
713                                         sone.find(".post#post-" + postId + " .create-reply :input[name=sender]").val(getCurrentSoneId());
714                                         updateReplyTimes(replyId);
715                                 } else {
716                                         alert(error);
717                                 }
718                                 button.removeAttr("disabled");
719                         });
720                 })(sender, postId, text, inputField);
721                 return false;
722         });
723
724         /* replace all â€śdelete” buttons with javascript. */
725         (function(postElement) {
726                 getTranslation("WebInterface.Confirmation.DeletePostButton", function(deletePostText) {
727                         var postId = getPostId(postElement);
728                         enhanceDeletePostButton($(postElement).find(".delete-post button"), postId, deletePostText);
729                 });
730         })(postElement);
731
732         /* convert all â€ślike” buttons to javascript functions. */
733         $(postElement).find(".like-post").submit(function() {
734                 likePost(getPostId(this));
735                 return false;
736         });
737         $(postElement).find(".unlike-post").submit(function() {
738                 unlikePost(getPostId(this));
739                 return false;
740         });
741
742         /* convert trust control buttons to javascript functions. */
743         $(postElement).find(".post-trust").submit(function() {
744                 trustSone(getPostAuthor(this));
745                 return false;
746         });
747         $(postElement).find(".post-distrust").submit(function() {
748                 distrustSone(getPostAuthor(this));
749                 return false;
750         });
751         $(postElement).find(".post-untrust").submit(function() {
752                 untrustSone(getPostAuthor(this));
753                 return false;
754         });
755
756         /* convert bookmark/unbookmark buttons to javascript functions. */
757         $(postElement).find(".bookmark").submit(function() {
758                 bookmarkPost(getPostId(this));
759                 return false;
760         });
761         $(postElement).find(".unbookmark").submit(function() {
762                 unbookmarkPost(getPostId(this));
763                 return false;
764         });
765
766         /* convert â€śshow source” link into javascript function. */
767         $(postElement).find(".show-source").each(function() {
768                 $("a", this).click(function() {
769                         var post = getPostElement(this);
770                         var rawPostText = $(".post-text.raw-text", post);
771                         rawPostText.toggleClass("hidden");
772                         if (rawPostText.hasClass("hidden")) {
773                                 $(".post-text.short-text", post).removeClass("hidden");
774                                 $(".post-text.text", post).addClass("hidden");
775                                 $(".expand-post-text", post).removeClass("hidden");
776                                 $(".shrink-post-text", post).addClass("hidden");
777                         } else {
778                                 $(".post-text.short-text", post).addClass("hidden");
779                                 $(".post-text.text", post).addClass("hidden");
780                                 $(".expand-post-text", post).addClass("hidden");
781                                 $(".shrink-post-text", post).addClass("hidden");
782                         }
783                         return false;
784                 });
785         });
786
787         /* convert â€śshow more” link into javascript function. */
788         $(postElement).find(".expand-post-text").each(function() {
789                 $(this).click(function() {
790                         $(".post-text.text", getPostElement(this)).toggleClass("hidden");
791                         $(".post-text.short-text", getPostElement(this)).toggleClass("hidden");
792                         $(".expand-post-text", getPostElement(this)).toggleClass("hidden");
793                         $(".shrink-post-text", getPostElement(this)).toggleClass("hidden");
794                         return false;
795                 });
796         });
797         $(postElement).find(".shrink-post-text").each(function() {
798                 $(this).click(function() {
799                         $(".post-text.text", getPostElement(this)).toggleClass("hidden");
800                         $(".post-text.short-text", getPostElement(this)).toggleClass("hidden");
801                         $(".expand-post-text", getPostElement(this)).toggleClass("hidden");
802                         $(".shrink-post-text", getPostElement(this)).toggleClass("hidden");
803                         return false;
804                 });
805         });
806
807         /* ajaxify author/post links */
808         $(".post-status-line .permalink a", postElement).click(function() {
809                 if (!$(".create-reply", postElement).hasClass("hidden")) {
810                         var textArea = $(":input.reply-input", postElement).focus().data("textarea");
811                         $(textArea).replaceSelection($(this).prop("href"));
812                 }
813                 return false;
814         });
815
816         /* add â€ścomment” link. */
817         addCommentLink(getPostId(postElement), getPostAuthor(postElement), postElement, $(postElement).find(".post-status-line .permalink-author"));
818
819         /* process all replies. */
820         var replyIds = [];
821         $(postElement).find(".reply").each(function() {
822                 replyIds.push(getReplyId(this));
823                 ajaxifyReply(this);
824         });
825         updateReplyTimes(replyIds.join(","));
826
827         /* process reply input fields. */
828         getTranslation("WebInterface.DefaultText.Reply", function(text) {
829                 $(postElement).find(":input.reply-input").each(function() {
830                         registerInputTextareaSwap(this, text, "text", false, false);
831                 });
832         });
833
834         /* process sender selection. */
835         $(".select-sender", postElement).css("display", "inline");
836         $(".sender", postElement).hide();
837         $(".select-sender button", postElement).click(function() {
838                 $(".sender", postElement).show();
839                 $(".select-sender", postElement).hide();
840                 return false;
841         });
842
843         /* mark everything as known on click. */
844         (function(postElement) {
845                 $(postElement).click(function(event) {
846                         if ($(event.target).hasClass("click-to-show")) {
847                                 return false;
848                         }
849                         markPostAsKnown(postElement, false);
850                 });
851         })(postElement);
852
853         /* hide reply input field. */
854         $(postElement).find(".create-reply").addClass("hidden");
855
856         /* show Sone menu when hovering over the avatar. */
857         $(postElement).find(".post-avatar").mouseover(function() {
858                 if (typeof currentSoneMenuTimeoutHandler !== undefined) {
859                         clearTimeout(currentSoneMenuTimeoutHandler);
860                 }
861                 currentSoneMenuId = getPostId(this);
862                 currentSoneMenuTimeoutHandler = setTimeout(function() {
863                         $(".sone-menu:visible").fadeOut();
864                         $(".sone-post-menu", postElement).mouseleave(function() {
865                                 $(this).fadeOut();
866                         }).fadeIn();
867                 }, 1000);
868         }).mouseleave(function() {
869                 if (currentSoneMenuId === getPostId(this)) {
870                         clearTimeout(currentSoneMenuTimeoutHandler);
871                 }
872         });
873         (function(postElement) {
874                 var soneId = $(".sone-menu-id:first", postElement).text();
875                 $(".sone-post-menu .follow", postElement).click(function() {
876                         var followElement = this;
877                         ajaxGet("followSone.ajax", { "sone": soneId, "formPassword": getFormPassword() }, function() {
878                                 $(followElement).addClass("hidden");
879                                 $(followElement).parent().find(".unfollow").removeClass("hidden");
880                                 sone.find(".sone-menu").each(function() {
881                                         if (getMenuSone(this) === soneId) {
882                                                 $(".follow", this).toggleClass("hidden", true);
883                                                 $(".unfollow", this).toggleClass("hidden", false);
884                                         }
885                                 });
886                         });
887                         return false;
888                 });
889                 $(".sone-post-menu .unfollow", postElement).click(function() {
890                         var unfollowElement = this;
891                         ajaxGet("unfollowSone.ajax", { "sone": soneId, "formPassword": getFormPassword() }, function() {
892                                 $(unfollowElement).addClass("hidden");
893                                 $(unfollowElement).parent().find(".follow").removeClass("hidden");
894                                 sone.find(".sone-menu").each(function() {
895                                         if (getMenuSone(this) === soneId) {
896                                                 $(".follow", this).toggleClass("hidden", false);
897                                                 $(".unfollow", this).toggleClass("hidden", true);
898                                         }
899                                 });
900                         });
901                         return false;
902                 });
903         })(postElement);
904 }
905
906 /**
907  * Ajaxifies the given reply element.
908  *
909  * @param replyElement
910  *            The reply element to ajaxify
911  */
912 function ajaxifyReply(replyElement) {
913         $(replyElement).find(".like-reply").submit(function() {
914                 likeReply(getReplyId(this));
915                 return false;
916         });
917         $(replyElement).find(".unlike-reply").submit(function() {
918                 unlikeReply(getReplyId(this));
919                 return false;
920         });
921         (function(replyElement) {
922                 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(deleteReplyText) {
923                         $(replyElement).find(".delete-reply button").each(function() {
924                                 enhanceDeleteReplyButton(this, getReplyId(replyElement), deleteReplyText);
925                         });
926                 });
927         })(replyElement);
928
929         /* ajaxify author links */
930         $(".reply-status-line .permalink a", replyElement).click(function() {
931                 if (!$(".create-reply", getPostElement(replyElement)).hasClass("hidden")) {
932                         var textArea = $(":input.reply-input", getPostElement(replyElement)).focus().data("textarea");
933                         $(textArea).replaceSelection($(this).prop("href"));
934                 }
935                 return false;
936         });
937
938         addCommentLink(getPostId(replyElement), getReplyAuthor(replyElement), replyElement, $(replyElement).find(".reply-status-line .permalink-author"));
939
940         /* convert â€śshow source” link into javascript function. */
941         $(replyElement).find(".show-reply-source").each(function() {
942                 $("a", this).click(function() {
943                         var reply = getReplyElement(this);
944                         var rawReplyText = $(".reply-text.raw-text", reply);
945                         rawReplyText.toggleClass("hidden");
946                         if (rawReplyText.hasClass("hidden")) {
947                                 $(".reply-text.short-text", reply).removeClass("hidden");
948                                 $(".reply-text.text", reply).addClass("hidden");
949                                 $(".expand-reply-text", reply).removeClass("hidden");
950                                 $(".shrink-reply-text", reply).addClass("hidden");
951                         } else {
952                                 $(".reply-text.short-text", reply).addClass("hidden");
953                                 $(".reply-text.text", reply).addClass("hidden");
954                                 $(".expand-reply-text", reply).addClass("hidden");
955                                 $(".shrink-reply-text", reply).addClass("hidden");
956                         }
957                         return false;
958                 });
959         });
960
961         /* convert â€śshow more” link into javascript function. */
962         $(replyElement).find(".expand-reply-text").each(function() {
963                 $(this).click(function() {
964                         $(".reply-text.text", getReplyElement(this)).toggleClass("hidden");
965                         $(".reply-text.short-text", getReplyElement(this)).toggleClass("hidden");
966                         $(".expand-reply-text", getReplyElement(this)).toggleClass("hidden");
967                         $(".shrink-reply-text", getReplyElement(this)).toggleClass("hidden");
968                         return false;
969                 });
970         });
971         $(replyElement).find(".shrink-reply-text").each(function() {
972                 $(this).click(function() {
973                         $(".reply-text.text", getReplyElement(this)).toggleClass("hidden");
974                         $(".reply-text.short-text", getReplyElement(this)).toggleClass("hidden");
975                         $(".expand-reply-text", getReplyElement(this)).toggleClass("hidden");
976                         $(".shrink-reply-text", getReplyElement(this)).toggleClass("hidden");
977                         return false;
978                 });
979         });
980
981         /* convert trust control buttons to javascript functions. */
982         $(replyElement).find(".reply-trust").submit(function() {
983                 trustSone(getReplyAuthor(this));
984                 return false;
985         });
986         $(replyElement).find(".reply-distrust").submit(function() {
987                 distrustSone(getReplyAuthor(this));
988                 return false;
989         });
990         $(replyElement).find(".reply-untrust").submit(function() {
991                 untrustSone(getReplyAuthor(this));
992                 return false;
993         });
994
995         /* show Sone menu when hovering over the avatar. */
996         $(replyElement).find(".reply-avatar").mouseover(function() {
997                 if (typeof currentSoneMenuTimeoutHandler !== undefined) {
998                         clearTimeout(currentSoneMenuTimeoutHandler);
999                 }
1000                 currentSoneMenuId = getPostId(this) + "-" + getReplyId(this);
1001                 currentSoneMenuTimeoutHandler = setTimeout(function() {
1002                         $(".sone-menu:visible").fadeOut();
1003                         $(".sone-reply-menu", replyElement).mouseleave(function() {
1004                                 $(this).fadeOut();
1005                         }).fadeIn();
1006                 }, 1000);
1007         }).mouseleave(function() {
1008                 if (currentSoneMenuId === getPostId(this) + "-" + getReplyId(this)) {
1009                         clearTimeout(currentSoneMenuTimeoutHandler);
1010                 }
1011         });
1012         (function(replyElement) {
1013                 var soneId = $(".sone-menu-id", replyElement).text();
1014                 $(".sone-menu .follow", replyElement).click(function() {
1015                         var followElement = this;
1016                         ajaxGet("followSone.ajax", { "sone": soneId, "formPassword": getFormPassword() }, function() {
1017                                 $(followElement).addClass("hidden");
1018                                 $(followElement).parent().find(".unfollow").removeClass("hidden");
1019                                 sone.find(".sone-menu").each(function() {
1020                                         if (getMenuSone(this) === soneId) {
1021                                                 $(".follow", this).toggleClass("hidden", true);
1022                                                 $(".unfollow", this).toggleClass("hidden", false);
1023                                         }
1024                                 });
1025                         });
1026                         return false;
1027                 });
1028                 $(".sone-menu .unfollow", replyElement).click(function() {
1029                         var unfollowElement = this;
1030                         ajaxGet("unfollowSone.ajax", { "sone": soneId, "formPassword": getFormPassword() }, function() {
1031                                 $(unfollowElement).addClass("hidden");
1032                                 $(unfollowElement).parent().find(".follow").removeClass("hidden");
1033                                 sone.find(".sone-menu").each(function() {
1034                                         if (getMenuSone(this) === soneId) {
1035                                                 $(".follow", this).toggleClass("hidden", false);
1036                                                 $(".unfollow", this).toggleClass("hidden", true);
1037                                         }
1038                                 });
1039                         });
1040                         return false;
1041                 });
1042         })(replyElement);
1043 }
1044
1045 /**
1046  * Ajaxifies the given notification by replacing the form with AJAX.
1047  *
1048  * @param notification
1049  *            jQuery object representing the notification.
1050  */
1051 function ajaxifyNotification(notification) {
1052         notification.find("form").submit(function() {
1053                 return false;
1054         });
1055         notification.find("input[name=returnPage]").val($.url.attr("relative"));
1056         if (notification.find(".short-text").length > 0) {
1057                 notification.find(".short-text").removeClass("hidden");
1058                 notification.find(".text").addClass("hidden");
1059         }
1060         notification.find("form.mark-as-read button").click(function() {
1061                 var allIds = $(":input[name=id]", this.form).val().split(" ");
1062                 for (var index = 0; index < allIds.length; index += 16) {
1063                         var ids = allIds.slice(index, index + 16).join(" ");
1064                         ajaxGet("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": $(":input[name=type]", this.form).val(), "id": ids});
1065                 }
1066         });
1067         notification.find("a[class^='link-']").each(function() {
1068                 var linkElement = $(this);
1069                 if (linkElement.is("[href^='viewPost']")) {
1070                         var id = linkElement.prop("class").substr(5);
1071                         if (hasPost(id)) {
1072                                 linkElement.prop("href", "#post-" + id).addClass("in-page-link");
1073                         }
1074                 }
1075         });
1076         notification.find("form.dismiss button").click(function() {
1077                 ajaxGet("dismissNotification.ajax", { "formPassword" : getFormPassword(), "notification" : notification.prop("id") }, function() {
1078                         /* dismiss in case of error, too. */
1079                         notification.slideUp();
1080                 }, function() {
1081                         /* ignore error. */
1082                 });
1083         });
1084         return notification;
1085 }
1086
1087 /**
1088  * Returns the notification hash. This hash is used in {@link #getStatus()} to
1089  * determine whether the notifications changed and need to be reloaded.
1090  */
1091 function getNotificationHash() {
1092         return sone.find("#notification-area #notification-hash").text();
1093 }
1094
1095 /**
1096  * Sets the notification hash.
1097  *
1098  * @param notificationHash
1099  *            The new notification hash
1100  */
1101 function setNotificationHash(notificationHash) {
1102         sone.find("#notification-area #notification-hash").text(notificationHash);
1103 }
1104
1105 /**
1106  * Retrieves element IDs from notification elements.
1107  *
1108  * @param notification
1109  *            The notification element
1110  * @param selector
1111  *            The selector of the element containing the ID as text
1112  * @returns All extracted IDs
1113  */
1114 function getElementIds(notification, selector) {
1115         var elementIds = [];
1116         $(selector, notification).each(function() {
1117                 elementIds.push($(this).text());
1118         });
1119         return elementIds;
1120 }
1121
1122 /**
1123  * Compares the given notification elements and calls {@link #markSoneAsKnown()}
1124  * for every ID that is contained in the old notification but not in the new.
1125  *
1126  * @param oldNotification
1127  *            The old notification element
1128  * @param newNotification
1129  *            The new notification element
1130  */
1131 function checkForRemovedSones(oldNotification, newNotification) {
1132         if (getNotificationId(oldNotification) !== "new-sone-notification") {
1133                 return;
1134         }
1135         var oldIds = getElementIds(oldNotification, ".new-sone-id");
1136         var newIds = getElementIds(newNotification, ".new-sone-id");
1137         $.each(oldIds, function(index, value) {
1138                 if ($.inArray(value, newIds) === -1) {
1139                         markSoneAsKnown(getSone(value), true);
1140                 }
1141         });
1142 }
1143
1144 /**
1145  * Compares the given notification elements and calls {@link #markPostAsKnown()}
1146  * for every ID that is contained in the old notification but not in the new.
1147  *
1148  * @param oldNotification
1149  *            The old notification element
1150  * @param newNotification
1151  *            The new notification element
1152  */
1153 function checkForRemovedPosts(oldNotification, newNotification) {
1154         if (getNotificationId(oldNotification) !== "new-post-notification") {
1155                 return;
1156         }
1157         var oldIds = getElementIds(oldNotification, ".post-id");
1158         var newIds = getElementIds(newNotification, ".post-id");
1159         $.each(oldIds, function(index, value) {
1160                 if ($.inArray(value, newIds) === -1) {
1161                         markPostAsKnown(getPost(value), true);
1162                 }
1163         });
1164 }
1165
1166 /**
1167  * Compares the given notification elements and calls
1168  * {@link #markReplyAsKnown()} for every ID that is contained in the old
1169  * notification but not in the new.
1170  *
1171  * @param oldNotification
1172  *            The old notification element
1173  * @param newNotification
1174  *            The new notification element
1175  */
1176 function checkForRemovedReplies(oldNotification, newNotification) {
1177         if (getNotificationId(oldNotification) !== "new-reply-notification") {
1178                 return;
1179         }
1180         var oldIds = getElementIds(oldNotification, ".reply-id");
1181         var newIds = getElementIds(newNotification, ".reply-id");
1182         $.each(oldIds, function(index, value) {
1183                 if ($.inArray(value, newIds) === -1) {
1184                         markReplyAsKnown(getReply(value), true);
1185                 }
1186         });
1187 }
1188
1189 function getStatus() {
1190         var parameters = isViewSonePage() ? {"soneIds": getShownSoneId() } : isKnownSonesPage() ? {"soneIds": getShownSoneIds() } : {};
1191         $.extend(parameters, {
1192                 "elements": JSON.stringify($(".linked-element.not-loaded").map(function () {
1193                         return $(this).prop("title");
1194                 }).toArray())
1195         });
1196         ajaxGet("getStatus.ajax", parameters, function(data) {
1197                 if ((data != null) && data.success) {
1198                         /* process Sone information. */
1199                         $.each(data.sones, function(index, value) {
1200                                 updateSoneStatus(value.id, value.name, value.status, value.modified, value.locked, value.lastUpdatedUnknown ? null : value.lastUpdated, value.lastUpdatedText);
1201                         });
1202                         notLoggedIn = !data.loggedIn;
1203                         if (!notLoggedIn) {
1204                                 showOfflineMarker(!online);
1205                         }
1206                         if (data.notificationHash !== getNotificationHash()) {
1207                                 console.log("Old hash: ", getNotificationHash(), ", new hash: ", data.notificationHash);
1208                                 requestNotifications();
1209                                 /* process new posts. */
1210                                 $.each(data.newPosts, function(index, value) {
1211                                         loadNewPost(value.id, value.sone, value.recipient, value.time);
1212                                 });
1213                                 /* process new replies. */
1214                                 $.each(data.newReplies, function(index, value) {
1215                                         loadNewReply(value.id, value.sone, value.post);
1216                                 });
1217                         }
1218                         if (data.linkedElements) {
1219                                 loadLinkedElements(data.linkedElements)
1220                         }
1221                         /* do it again in 5 seconds. */
1222                         setTimeout(getStatus, 5000);
1223                 } else {
1224                         /* data.success was false, wait 30 seconds. */
1225                         setTimeout(getStatus, 30000);
1226                 }
1227         }, function() {
1228                 statusRequestQueued = false;
1229                 ajaxError();
1230         });
1231 }
1232
1233 function requestNotifications() {
1234         ajaxGet("getNotifications.ajax", {}, function(data) {
1235                 if (data && data.success) {
1236                         /* search for removed notifications. */
1237                         sone.find("#notification-area .notification").each(function() {
1238                                 var notificationId = $(this).prop("id");
1239                                 var foundNotification = false;
1240                                 $.each(data.notifications, function(index, value) {
1241                                         if (value.id === notificationId) {
1242                                                 foundNotification = true;
1243                                                 return false;
1244                                         }
1245                                 });
1246                                 if (!foundNotification) {
1247                                         if (notificationId === "new-sone-notification" && (data.options["ShowNotification/NewSones"] === true)) {
1248                                                 $(".new-sone-id", this).each(function() {
1249                                                         var soneId = $(this).text();
1250                                                         markSoneAsKnown(getSone(soneId), true);
1251                                                 });
1252                                         } else if (notificationId === "new-post-notification" && (data.options["ShowNotification/NewPosts"] === true)) {
1253                                                 $(".post-id", this).each(function() {
1254                                                         var postId = $(this).text();
1255                                                         markPostAsKnown(getPost(postId), true);
1256                                                 });
1257                                         } else if (notificationId === "new-reply-notification" && (data.options["ShowNotification/NewReplies"] === true)) {
1258                                                 $(".reply-id", this).each(function() {
1259                                                         var replyId = $(this).text();
1260                                                         markReplyAsKnown(getReply(replyId), true);
1261                                                 });
1262                                         }
1263                                         $(this).slideUp("normal", function() {
1264                                                 $(this).remove();
1265                                                 /* remove activity when no notifications are visible. */
1266                                                 if (sone.find("#notification-area .notification").length === 0) {
1267                                                         resetActivity();
1268                                                 }
1269                                         });
1270                                 }
1271                         });
1272                         /* process notifications. */
1273                         $.each(data.notifications, function(index, value) {
1274                                 var oldNotification = getNotification(value.id);
1275                                 var notification = ajaxifyNotification(createNotification(value.id, value.lastUpdatedTime, value.text, value.dismissable)).hide();
1276                                 if (oldNotification.length !== 0) {
1277                                         if ((oldNotification.find(".short-text").length > 0) && (notification.find(".short-text").length > 0)) {
1278                                                 var opened = oldNotification.is(":visible") && oldNotification.find(".short-text").hasClass("hidden");
1279                                                 notification.find(".short-text").toggleClass("hidden", opened);
1280                                                 notification.find(".text").toggleClass("hidden", !opened);
1281                                         }
1282                                         checkForRemovedSones(oldNotification, notification);
1283                                         checkForRemovedPosts(oldNotification, notification);
1284                                         checkForRemovedReplies(oldNotification, notification);
1285                                         oldNotification.replaceWith(notification.show());
1286                                 } else {
1287                                         sone.find("#notification-area").append(notification);
1288                                         if (value.id.substring(0, 5) !== "local") {
1289                                                 notification.slideDown();
1290                                                 setActivity();
1291                                         }
1292                                 }
1293                         });
1294                         setNotificationHash(data.notificationHash);
1295                 }
1296         });
1297 }
1298
1299 /**
1300  * Returns the ID of the currently logged in Sone.
1301  *
1302  * @return The ID of the current Sone, or an empty string if no Sone is logged
1303  *         in
1304  */
1305 function getCurrentSoneId() {
1306         return $("#currentSoneId").text();
1307 }
1308
1309 /**
1310  * Returns the content of the page-id attribute.
1311  *
1312  * @returns The page ID
1313  */
1314 function getPageId() {
1315         return sone.find(".page-id").text();
1316 }
1317
1318 /**
1319  * Returns whether the current page is the index page.
1320  *
1321  * @returns {Boolean} <code>true</code> if the current page is the index page,
1322  *          <code>false</code> otherwise
1323  */
1324 function isIndexPage() {
1325         return getPageId() === "index";
1326 }
1327
1328 /**
1329  * Returns the current page of the selected pagination. If no pagination can be
1330  * found with the given selector, {@code 1} is returned.
1331  *
1332  * @param paginationSelector
1333  *            The pagination selector
1334  * @returns The current page of the pagination
1335  */
1336 function getPage(paginationSelector) {
1337         var pagination = $(paginationSelector);
1338         if (pagination.length > 0) {
1339                 return $(".current-page", paginationSelector).text();
1340         }
1341         return 1;
1342 }
1343
1344 /**
1345  * Returns whether the current page is a â€śview Sone” page.
1346  *
1347  * @returns {Boolean} <code>true</code> if the current page is a â€śview Sone”
1348  *          page, <code>false</code> otherwise
1349  */
1350 function isViewSonePage() {
1351         return getPageId() === "view-sone";
1352 }
1353
1354 /**
1355  * Returns the ID of the currently shown Sone. This will only return a sensible
1356  * value if isViewSonePage() returns <code>true</code>.
1357  *
1358  * @returns The ID of the currently shown Sone
1359  */
1360 function getShownSoneId() {
1361         return sone.find(".sone-id").first().text();
1362 }
1363
1364 /**
1365  * Returns the ID of all currently visible Sones. This is mainly used on the
1366  * â€śKnown Sones” page.
1367  *
1368  * @returns The ID of the currently shown Sones
1369  */
1370 function getShownSoneIds() {
1371         var soneIds = [];
1372         sone.find("#known-sones .sone .id").each(function() {
1373                 soneIds.push($(this).text());
1374         });
1375         return soneIds.join(",");
1376 }
1377
1378 /**
1379  * Returns whether the current page is a â€śview post” page.
1380  *
1381  * @returns {Boolean} <code>true</code> if the current page is a â€śview post”
1382  *          page, <code>false</code> otherwise
1383  */
1384 function isViewPostPage() {
1385         return getPageId() === "view-post";
1386 }
1387
1388 /**
1389  * Returns the ID of the currently shown post. This will only return a sensible
1390  * value if isViewPostPage() returns <code>true</code>.
1391  *
1392  * @returns The ID of the currently shown post
1393  */
1394 function getShownPostId() {
1395         return sone.find(".post-id").text();
1396 }
1397
1398 /**
1399  * Returns whether the current page is the â€śknown Sones” page.
1400  *
1401  * @returns {Boolean} <code>true</code> if the current page is the â€śknown
1402  *          Sones” page, <code>false</code> otherwise
1403  */
1404 function isKnownSonesPage() {
1405         return getPageId() === "known-sones";
1406 }
1407
1408 /**
1409  * Returns whether a post with the given ID exists on the current page.
1410  *
1411  * @param postId
1412  *            The post ID to check for
1413  * @returns {Boolean} <code>true</code> if a post with the given ID already
1414  *          exists on the page, <code>false</code> otherwise
1415  */
1416 function hasPost(postId) {
1417         return $(".post#post-" + postId).length > 0;
1418 }
1419
1420 /**
1421  * Returns whether a reply with the given ID exists on the current page.
1422  *
1423  * @param replyId
1424  *            The reply ID to check for
1425  * @returns {Boolean} <code>true</code> if a reply with the given ID already
1426  *          exists on the page, <code>false</code> otherwise
1427  */
1428 function hasReply(replyId) {
1429         return sone.find(".reply#reply-" + replyId).length > 0;
1430 }
1431
1432 function loadNewPost(postId, soneId, recipientId, time) {
1433         if (hasPost(postId)) {
1434                 return;
1435         }
1436         if (!isIndexPage() || (getPage(".pagination-index") > 1)) {
1437                 if (!isViewPostPage() || (getShownPostId() !== postId)) {
1438                         if (!isViewSonePage() || ((getShownSoneId() !== soneId) && (getShownSoneId() !== recipientId)) || (getPage(".post-navigation") > 1)) {
1439                                 return;
1440                         }
1441                 }
1442         }
1443         if (getPostTime(sone.find(".post").last()) > time) {
1444                 return;
1445         }
1446         ajaxGet("getPost.ajax", { "post" : postId }, function(data) {
1447                 if ((data != null) && data.success) {
1448                         if (hasPost(data.post.id)) {
1449                                 return;
1450                         }
1451                         if ((!isIndexPage() || (getPage(".pagination-index") > 1)) && !(isViewSonePage() && ((getShownSoneId() === data.post.sone) || (getShownSoneId() === data.post.recipient) || (getPage(".post-navigation") > 1)))) {
1452                                 return;
1453                         }
1454                         var firstOlderPost = null;
1455                         sone.find(".post").each(function() {
1456                                 if (getPostTime(this) < data.post.time) {
1457                                         firstOlderPost = $(this);
1458                                         return false;
1459                                 }
1460                         });
1461                         var newPost = $(data.post.html).addClass("hidden");
1462                         if ($(".post-author-local", newPost).text() === "true") {
1463                                 newPost.removeClass("new");
1464                         }
1465                         if (firstOlderPost != null) {
1466                                 newPost.insertBefore(firstOlderPost);
1467                         }
1468                         ajaxifyPost(newPost);
1469                         updatePostTimes(data.post.id);
1470                         newPost.slideDown();
1471                         setActivity();
1472                 }
1473         });
1474 }
1475
1476 function loadNewReply(replyId, soneId, postId) {
1477         if (hasReply(replyId)) {
1478                 return;
1479         }
1480         if (!hasPost(postId)) {
1481                 return;
1482         }
1483         ajaxGet("getReply.ajax", { "reply": replyId }, function(data) {
1484                 /* find post. */
1485                 if ((data != null) && data.success) {
1486                         if (hasReply(data.reply.id)) {
1487                                 return;
1488                         }
1489                         sone.find(".post#post-" + data.reply.postId).each(function() {
1490                                 var firstNewerReply = null;
1491                                 $(this).find(".replies .reply").each(function() {
1492                                         if (getReplyTime(this) > data.reply.time) {
1493                                                 firstNewerReply = $(this);
1494                                                 return false;
1495                                         }
1496                                 });
1497                                 var newReply = $(data.reply.html).addClass("hidden");
1498                                 if ($(".reply-author-local", newReply).text() === "true") {
1499                                         newReply.removeClass("new");
1500                                         (function(newReply) {
1501                                                 setTimeout(function() {
1502                                                         markReplyAsKnown(newReply, false);
1503                                                 }, 5000);
1504                                         })(newReply);
1505                                 }
1506                                 if (firstNewerReply != null) {
1507                                         newReply.insertBefore(firstNewerReply);
1508                                 } else {
1509                                         if ($(this).find(".replies .create-reply")) {
1510                                                 $(this).find(".replies .create-reply").before(newReply);
1511                                         } else {
1512                                                 $(this).find(".replies").append(newReply);
1513                                         }
1514                                 }
1515                                 ajaxifyReply(newReply);
1516                                 updateReplyTimes(data.reply.id);
1517                                 newReply.slideDown();
1518                                 setActivity();
1519                                 return false;
1520                         });
1521                 }
1522         });
1523 }
1524
1525 function loadLinkedElements(links) {
1526         var failedElements = links.filter(function(element) {
1527                 return element.failed;
1528         });
1529         if (failedElements.length > 0) {
1530                 failedElements.forEach(function(element) {
1531                         getLinkedElements(element.link).each(function() {
1532                                 $(this).remove()
1533                         });
1534                 });
1535         }
1536         var loadedElements = links.filter(function(element) {
1537                 return !element.loading && !element.failed;
1538         });
1539         if (loadedElements.length > 0) {
1540                 ajaxGet("getLinkedElement.ajax", {
1541                         "elements": JSON.stringify(loadedElements.map(function(element) {
1542                                 return element.link;
1543                         }))
1544                 }, function (data) {
1545                         if ((data != null) && (data.success)) {
1546                                 data.linkedElements.forEach(function (linkedElement) {
1547                                         getLinkedElements(linkedElement.link).each(function() {
1548                                                 $(this).replaceWith(linkedElement.html);
1549                                         });
1550                                 });
1551                         }
1552                 });
1553         }
1554 }
1555
1556 function getLinkedElements(link) {
1557         return $(".linked-element[title='" + link + "']")
1558 }
1559
1560 /**
1561  * Marks the given Sone as known if it is still new.
1562  *
1563  * @param soneElement
1564  *            The Sone to mark as known
1565  * @param skipRequest
1566  *            true to skip the JSON request, false or omit to perform the JSON
1567  *            request
1568  */
1569 function markSoneAsKnown(soneElement, skipRequest) {
1570         if ($(soneElement).hasClass("new")) {
1571                 $(soneElement).removeClass("new");
1572                 if ((typeof skipRequest == "undefined") || !skipRequest) {
1573                         ajaxGet("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "sone", "id": getSoneId(soneElement)});
1574                         requestNotifications();
1575                 }
1576         }
1577 }
1578
1579 function markPostAsKnown(postElements, skipRequest) {
1580         $(postElements).each(function() {
1581                 var postElement = this;
1582                 if ($(postElement).hasClass("new") || ((typeof skipRequest != "undefined"))) {
1583                         (function(postElement) {
1584                                 $(postElement).removeClass("new");
1585                                 if ((typeof skipRequest == "undefined") || !skipRequest) {
1586                                         ajaxGet("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "post", "id": getPostId(postElement)});
1587                                         requestNotifications();
1588                                 }
1589                         })(postElement);
1590                 }
1591                 $(".click-to-show", postElement).removeClass("new");
1592         });
1593         markReplyAsKnown($(postElements).find(".reply"), true);
1594 }
1595
1596 function markReplyAsKnown(replyElements, skipRequest) {
1597         $(replyElements).each(function() {
1598                 var replyElement = this;
1599                 if ($(replyElement).hasClass("new") || ((typeof skipRequest != "undefined"))) {
1600                         (function(replyElement) {
1601                                 $(replyElement).removeClass("new");
1602                                 if ((typeof skipRequest == "undefined") || !skipRequest) {
1603                                         ajaxGet("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "reply", "id": getReplyId(replyElement)});
1604                                         requestNotifications();
1605                                 }
1606                         })(replyElement);
1607                 }
1608         });
1609 }
1610
1611 /**
1612  * Updates the time of the post with the given ID.
1613  *
1614  * @param postId
1615  *            The ID of the post to update
1616  * @param timeText
1617  *            The text of the time to show
1618  * @param refreshTime
1619  *            The refresh time after which to request a new time (in seconds)
1620  * @param tooltip
1621  *            The tooltip to show
1622  */
1623 function updatePostTime(postId, timeText, refreshTime, tooltip) {
1624         if (!getPost(postId).is(":visible")) {
1625                 return;
1626         }
1627         getPost(postId).find(".post-status-line > .time a").html(timeText).prop("title", tooltip);
1628         (function(postId, refreshTime) {
1629                 setTimeout(function() {
1630                         updatePostTimes(postId);
1631                 }, refreshTime * 1000);
1632         })(postId, refreshTime);
1633 }
1634
1635 /**
1636  * Requests new rendered times for the posts with the given IDs.
1637  *
1638  * @param postIds
1639  *            Comma-separated post IDs
1640  */
1641 function updatePostTimes(postIds) {
1642         if (postIds !== "") {
1643         ajaxGet("getTimes.ajax", {"posts": postIds}, function (data) {
1644             if ((data != null) && data.success) {
1645                 $.each(data.postTimes, function (index, value) {
1646                     updatePostTime(index, value.timeText, value.refreshTime, value.tooltip);
1647                 });
1648             }
1649         });
1650     }
1651 }
1652
1653 /**
1654  * Updates the time of the reply with the given ID.
1655  *
1656  * @param postId
1657  *            The ID of the reply to update
1658  * @param timeText
1659  *            The text of the time to show
1660  * @param refreshTime
1661  *            The refresh time after which to request a new time (in seconds)
1662  * @param tooltip
1663  *            The tooltip to show
1664  */
1665 function updateReplyTime(replyId, timeText, refreshTime, tooltip) {
1666         getReply(replyId).find(".reply-status-line > .time").html(timeText).prop("title", tooltip);
1667         (function(replyId, refreshTime) {
1668                 setTimeout(function() {
1669                         updateReplyTimes(replyId);
1670                 }, refreshTime * 1000);
1671         })(replyId, refreshTime);
1672 }
1673
1674 /**
1675  * Requests new rendered times for the posts with the given IDs.
1676  *
1677  * @param postIds
1678  *            Comma-separated post IDs
1679  */
1680 function updateReplyTimes(replyIds) {
1681         if (replyIds !== "") {
1682         ajaxGet("getTimes.ajax", {"replies": replyIds}, function (data) {
1683             if ((data != null) && data.success) {
1684                 $.each(data.replyTimes, function (index, value) {
1685                     updateReplyTime(index, value.timeText, value.refreshTime, value.tooltip);
1686                 });
1687             }
1688         });
1689     }
1690 }
1691
1692 function resetActivity() {
1693         var title = document.title;
1694         if (title.indexOf('(') === 0) {
1695                 setTitle(title.substr(title.indexOf(' ') + 1));
1696         }
1697         iconBlinking = false;
1698 }
1699
1700 function setActivity() {
1701         if (!focus) {
1702                 var title = document.title;
1703                 if (title.indexOf('(') !== 0) {
1704                         setTitle("(!) " + title);
1705                 }
1706                 if (!iconBlinking) {
1707                         setTimeout(toggleIcon, 1500);
1708                         iconBlinking = true;
1709                 }
1710         }
1711 }
1712
1713 /**
1714  * Sets the window title after a small delay to prevent race-condition issues.
1715  *
1716  * @param title
1717  *            The title to set
1718  */
1719 function setTitle(title) {
1720         setTimeout(function() {
1721                 document.title = title;
1722         }, 50);
1723 }
1724
1725 /** Whether the icon is currently showing activity. */
1726 var iconActive = false;
1727
1728 /** Whether the icon is currently supposed to blink. */
1729 var iconBlinking = false;
1730
1731 /**
1732  * Toggles the icon. If the window has gained focus and the icon is still
1733  * showing the activity state, it is returned to normal.
1734  */
1735 function toggleIcon() {
1736         if (focus || !iconBlinking) {
1737                 if (iconActive) {
1738                         changeIcon("images/icon.png");
1739                         iconActive = false;
1740                 }
1741                 iconBlinking = false;
1742         } else {
1743                 iconActive = !iconActive;
1744                 changeIcon(iconActive ? "images/icon-activity.png" : "images/icon.png");
1745                 setTimeout(toggleIcon, 1500);
1746         }
1747 }
1748
1749 /**
1750  * Changes the icon of the page.
1751  *
1752  * @param iconUrl
1753  *            The new URL of the icon
1754  */
1755 function changeIcon(iconUrl) {
1756         $("link[rel=icon]").remove();
1757         $("head").append($("<link>").prop("rel", "icon").prop("type", "image/png").prop("href", iconUrl));
1758         $("iframe[id=icon-update]")[0].src += "";
1759 }
1760
1761 /**
1762  * Creates a new notification.
1763  *
1764  * @param id
1765  *            The ID of the notificaiton
1766  * @param text
1767  *            The text of the notification
1768  * @param dismissable
1769  *            <code>true</code> if the notification can be dismissed by the
1770  *            user
1771  */
1772 function createNotification(id, lastUpdatedTime, text, dismissable) {
1773         var notification = $("<div></div>").addClass("notification").prop("id", id).prop("lastUpdatedTime", lastUpdatedTime);
1774         if (dismissable) {
1775                 var dismissForm = sone.find("#notification-area #notification-dismiss-template").clone().removeClass("hidden").removeAttr("id");
1776                 dismissForm.find("input[name=notification]").val(id);
1777                 notification.append(dismissForm);
1778         }
1779         notification.append(text);
1780         return notification;
1781 }
1782
1783 /**
1784  * Shows the details of the notification with the given ID.
1785  *
1786  * @param notificationId
1787  *            The ID of the notification
1788  */
1789 function showNotificationDetails(notificationId) {
1790         sone.find(".notification#" + notificationId + " .text").removeClass("hidden");
1791         sone.find(".notification#" + notificationId + " .short-text").addClass("hidden");
1792 }
1793
1794 /**
1795  * Deletes the field with the given ID from the profile.
1796  *
1797  * @param fieldId
1798  *            The ID of the field to delete
1799  */
1800 function deleteProfileField(fieldId) {
1801         ajaxGet("deleteProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId}, function(data) {
1802                 if (data && data.success) {
1803                         sone.find(".profile-field#" + data.field.id).slideUp();
1804                 }
1805         });
1806 }
1807
1808 /**
1809  * Renames a profile field.
1810  *
1811  * @param fieldId
1812  *            The ID of the field to rename
1813  * @param newName
1814  *            The new name of the field
1815  * @param successFunction
1816  *            Called when the renaming was successful
1817  */
1818 function editProfileField(fieldId, newName, successFunction) {
1819         ajaxGet("editProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId, "name": newName}, function(data) {
1820                 if (data && data.success) {
1821                         successFunction();
1822                 }
1823         });
1824 }
1825
1826 /**
1827  * Moves the profile field with the given ID one slot in the given direction.
1828  *
1829  * @param fieldId
1830  *            The ID of the field to move
1831  * @param direction
1832  *            The direction to move in (“up” or â€śdown”)
1833  * @param successFunction
1834  *            Function to call on success
1835  */
1836 function moveProfileField(fieldId, direction, successFunction) {
1837         ajaxGet("moveProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId, "direction": direction}, function(data) {
1838                 if (data && data.success) {
1839                         successFunction();
1840                 }
1841         });
1842 }
1843
1844 /**
1845  * Moves the profile field with the given ID up one slot.
1846  *
1847  * @param fieldId
1848  *            The ID of the field to move
1849  * @param successFunction
1850  *            Function to call on success
1851  */
1852 function moveProfileFieldUp(fieldId, successFunction) {
1853         moveProfileField(fieldId, "up", successFunction);
1854 }
1855
1856 /**
1857  * Moves the profile field with the given ID down one slot.
1858  *
1859  * @param fieldId
1860  *            The ID of the field to move
1861  * @param successFunction
1862  *            Function to call on success
1863  */
1864 function moveProfileFieldDown(fieldId, successFunction) {
1865         moveProfileField(fieldId, "down", successFunction);
1866 }
1867
1868 var statusRequestQueued = true;
1869
1870 /**
1871  * Sets the status of the web interface as offline.
1872  */
1873 function ajaxError() {
1874         online = false;
1875         showOfflineMarker(true);
1876         if (!statusRequestQueued) {
1877                 setTimeout(getStatus, 5000);
1878                 statusRequestQueued = true;
1879         }
1880 }
1881
1882 /**
1883  * Sets the status of the web interface as online.
1884  */
1885 function ajaxSuccess() {
1886         online = true;
1887         showOfflineMarker(!online || (initiallyLoggedIn && notLoggedIn));
1888 }
1889
1890 /**
1891  * Shows or hides the offline marker.
1892  *
1893  * @param visible
1894  *            {@code true} to display the offline marker, {@code false} to hide
1895  *            it
1896  */
1897 function showOfflineMarker(visible) {
1898         /* jQuery documentation says toggle() works the other way around?! */
1899         sone.find("#offline-marker").toggle(visible);
1900         if (visible) {
1901                 sone.find("#main").addClass("offline");
1902         } else {
1903                 sone.find("#main").removeClass("offline");
1904         }
1905 }
1906
1907 //
1908 // EVERYTHING BELOW HERE IS EXECUTED AFTER LOADING THE PAGE
1909 //
1910
1911 var sone = $("#sone");
1912 var focus = true;
1913 var online = true;
1914 var initiallyLoggedIn = sone.find("#loggedIn").text() === "true";
1915 var notLoggedIn = !initiallyLoggedIn;
1916
1917 /** ID of the next-to-show Sone context menu. */
1918 var currentSoneMenuId;
1919
1920 /** Timeout handler for the next-to-show Sone context menu. */
1921 var currentSoneMenuTimeoutHandler;
1922
1923 $(document).ready(function() {
1924
1925         /* rip out the status update textarea. */
1926         sone.find(".rip-out").each(function() {
1927                 var oldElement = $(this);
1928                 var newElement = $("<input type='text'/>");
1929                 newElement.prop("class", oldElement.prop("class")).prop("name", oldElement.prop("name"));
1930                 oldElement.before(newElement).remove();
1931         });
1932
1933         /* this initializes the status update input field. */
1934         getTranslation("WebInterface.DefaultText.StatusUpdate", function(defaultText) {
1935                 registerInputTextareaSwap("#sone #update-status .status-input", defaultText, "text", false, false);
1936                 sone.find("#update-status .select-sender").css("display", "inline");
1937                 sone.find("#update-status .sender").hide();
1938                 sone.find("#update-status .select-sender button").click(function() {
1939                         sone.find("#update-status .sender").show();
1940                         sone.find("#update-status .select-sender").hide();
1941                         return false;
1942                 });
1943                 sone.find("#update-status").submit(function() {
1944                         var button = $("button:submit", this);
1945                         button.prop("disabled", "disabled");
1946                         if ($(this).find(":input.default:enabled").length > 0) {
1947                                 return false;
1948                         }
1949                         var sender = $(this).find(":input[name=sender]").val();
1950                         var text = $(this).find(":input[name=text]:enabled").val();
1951                         ajaxGet("createPost.ajax", { "formPassword": getFormPassword(), "sender": sender, "text": text }, function() {
1952                                 button.removeAttr("disabled");
1953                         });
1954                         $(this).find(":input[name=sender]").val(getCurrentSoneId());
1955                         $(this).find(":input[name=text]:enabled").val("").blur();
1956                         $(this).find(".sender").hide();
1957                         $(this).find(".select-sender").show();
1958                         return false;
1959                 });
1960         });
1961
1962         /* ajaxify the search input field. */
1963         getTranslation("WebInterface.DefaultText.Search", function(defaultText) {
1964                 registerInputTextareaSwap("#sone #search input[name=query]", defaultText, "query", false, true);
1965         });
1966
1967         /* ajaxify input field on â€śview Sone” page. */
1968         getTranslation("WebInterface.DefaultText.Message", function(defaultText) {
1969                 registerInputTextareaSwap("#sone #post-message input[name=text]", defaultText, "text", false, false);
1970                 sone.find("#post-message .select-sender").css("display", "inline");
1971                 sone.find("#post-message .sender").hide();
1972                 sone.find("#post-message .select-sender button").click(function() {
1973                         sone.find("#post-message .sender").show();
1974                         sone.find("#post-message .select-sender").hide();
1975                         return false;
1976                 });
1977                 sone.find("#post-message").submit(function() {
1978                         var sender = $(this).find(":input[name=sender]").val();
1979                         var text = $(this).find(":input[name=text]:enabled").val();
1980                         ajaxGet("createPost.ajax", { "formPassword": getFormPassword(), "recipient": getShownSoneId(), "sender": sender, "text": text });
1981                         $(this).find(":input[name=sender]").val(getCurrentSoneId());
1982                         $(this).find(":input[name=text]:enabled").val("").blur();
1983                         $(this).find(".sender").hide();
1984                         $(this).find(".select-sender").show();
1985                         return false;
1986                 });
1987         });
1988
1989         /* Ajaxifies all posts. */
1990         /* calling getTranslation here will cache the necessary values. */
1991         getTranslation("WebInterface.Confirmation.DeletePostButton", function() {
1992                 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function() {
1993                         getTranslation("WebInterface.DefaultText.Reply", function() {
1994                 getTranslation("WebInterface.Button.Comment", function () {
1995                     sone.find(".post").each(function() {
1996                                                 ajaxifyPost(this);
1997                                         });
1998                                 });
1999                         });
2000                 });
2001         });
2002
2003         /* update post times. */
2004         var postIds = [];
2005         sone.find(".post").each(function() {
2006                 postIds.push(getPostId(this));
2007         });
2008         updatePostTimes(postIds.join(","));
2009
2010         /* hides all replies but the latest two. */
2011         if (!isViewPostPage()) {
2012                 getTranslation("WebInterface.ClickToShow.Replies", function(text) {
2013                         sone.find(".post .replies").each(function() {
2014                                 var allReplies = $(this).find(".reply");
2015                                 if (allReplies.length > 2) {
2016                                         var newHidden = false;
2017                                         for (var replyIndex = 0; replyIndex < (allReplies.length - 2); ++replyIndex) {
2018                                                 $(allReplies[replyIndex]).addClass("hidden");
2019                                                 newHidden |= $(allReplies[replyIndex]).hasClass("new");
2020                                         }
2021                                         var clickToShowElement = $("<div></div>").addClass("click-to-show");
2022                                         if (newHidden) {
2023                                                 clickToShowElement.addClass("new");
2024                                         }
2025                                         (function(clickToShowElement, allReplies, text) {
2026                                                 clickToShowElement.text(text);
2027                                                 clickToShowElement.click(function() {
2028                                                         allReplies.removeClass("hidden");
2029                                                         clickToShowElement.addClass("hidden");
2030                                                 });
2031                                         })(clickToShowElement, allReplies, text);
2032                                         $(allReplies[0]).before(clickToShowElement);
2033                                 }
2034                         });
2035                 });
2036         }
2037
2038         sone.find(".sone").each(function() {
2039                 ajaxifySone($(this));
2040         });
2041
2042         /* process all existing notifications, ajaxify dismiss buttons. */
2043         sone.find("#notification-area .notification").each(function() {
2044                 ajaxifyNotification($(this));
2045         });
2046
2047         /* activate status polling. */
2048         setTimeout(getStatus, 5000);
2049
2050         /* reset activity counter when the page has focus. */
2051         $(window).focus(function() {
2052                 focus = true;
2053                 resetActivity();
2054         }).blur(function() {
2055                 focus = false;
2056         });
2057
2058 });