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