Add getReply() function that works the same as getPost().
[Sone.git] / src / main / resources / static / javascript / sone.js
1 /* Sone JavaScript functions. */
2
3 /* jQuery overrides. */
4 oldGetJson = jQuery.prototype.getJSON;
5 jQuery.prototype.getJSON = function(url, data, successCallback, errorCallback) {
6         if (typeof errorCallback == "undefined") {
7                 return oldGetJson(url, data, successCallback);
8         }
9         if (jQuery.isFunction(data)) {
10                 errorCallback = successCallback;
11                 successCallback = data;
12                 data = null;
13         }
14         return jQuery.ajax({
15                 data: data,
16                 error: errorCallback,
17                 success: successCallback,
18                 url: url
19         });
20 }
21
22 function isOnline() {
23         return $("#sone").hasClass("online");
24 }
25
26 function registerInputTextareaSwap(inputElement, defaultText, inputFieldName, optional, dontUseTextarea) {
27         $(inputElement).each(function() {
28                 textarea = $(dontUseTextarea ? "<input type=\"text\" name=\"" + inputFieldName + "\">" : "<textarea name=\"" + inputFieldName + "\"></textarea>").blur(function() {
29                         if ($(this).val() == "") {
30                                 $(this).hide();
31                                 inputField = $(this).data("inputField");
32                                 inputField.show().removeAttr("disabled").addClass("default");
33                                 inputField.val(defaultText);
34                         }
35                 }).hide().data("inputField", $(this)).val($(this).val());
36                 $(this).after(textarea);
37                 (function(inputField, textarea) {
38                         inputField.focus(function() {
39                                 $(this).hide().attr("disabled", "disabled");
40                                 /* no, show(), “display: block” is not what I need. */
41                                 textarea.attr("style", "display: inline").focus();
42                         });
43                         if (inputField.val() == "") {
44                                 inputField.addClass("default");
45                                 inputField.val(defaultText);
46                         } else {
47                                 inputField.hide().attr("disabled", "disabled");
48                                 textarea.show();
49                         }
50                         $(inputField.get(0).form).submit(function() {
51                                 inputField.attr("disabled", "disabled");
52                                 if (!optional && (textarea.val() == "")) {
53                                         inputField.removeAttr("disabled").focus();
54                                         return false;
55                                 }
56                         });
57                 })($(this), textarea);
58         });
59 }
60
61 /**
62  * Adds a “comment” link to all status lines contained in the given element.
63  *
64  * @param postId
65  *            The ID of the post
66  * @param element
67  *            The element to add a “comment” link to
68  */
69 function addCommentLink(postId, element, insertAfterThisElement) {
70         if (($(element).find(".show-reply-form").length > 0) || (getPostElement(element).find(".create-reply").length == 0)) {
71                 return;
72         }
73         commentElement = (function(postId) {
74                 separator = $("<span> · </span>").addClass("separator");
75                 var commentElement = $("<div><span>Comment</span></div>").addClass("show-reply-form").click(function() {
76                         replyElement = $("#sone .post#" + postId + " .create-reply");
77                         replyElement.removeClass("hidden");
78                         replyElement.removeClass("light");
79                         (function(replyElement) {
80                                 replyElement.find("input.reply-input").blur(function() {
81                                         if ($(this).hasClass("default")) {
82                                                 replyElement.addClass("light");
83                                         }
84                                 }).focus(function() {
85                                         replyElement.removeClass("light");
86                                 });
87                         })(replyElement);
88                         replyElement.find("input.reply-input").focus();
89                 });
90                 return commentElement;
91         })(postId);
92         $(insertAfterThisElement).after(commentElement.clone(true));
93         $(insertAfterThisElement).after(separator);
94 }
95
96 var translations = {};
97
98 /**
99  * Retrieves the translation for the given key and calls the callback function.
100  * The callback function takes a single parameter, the translated string.
101  *
102  * @param key
103  *            The key of the translation string
104  * @param callback
105  *            The callback function
106  */
107 function getTranslation(key, callback) {
108         if (key in translations) {
109                 callback(translations[key]);
110                 return;
111         }
112         $.getJSON("getTranslation.ajax", {"key": key}, function(data, textStatus) {
113                 if ((data != null) && data.success) {
114                         translations[key] = data.value;
115                         callback(data.value);
116                 }
117         }, function(xmlHttpRequest, textStatus, error) {
118                 /* ignore error. */
119         });
120 }
121
122 /**
123  * Filters the given Sone ID, replacing all “~” characters by an underscore.
124  *
125  * @param soneId
126  *            The Sone ID to filter
127  * @returns The filtered Sone ID
128  */
129 function filterSoneId(soneId) {
130         return soneId.replace(/[^a-zA-Z0-9-]/g, "_");
131 }
132
133 /**
134  * Updates the status of the given Sone.
135  *
136  * @param soneId
137  *            The ID of the Sone to update
138  * @param status
139  *            The status of the Sone (“idle”, “unknown”, “inserting”,
140  *            “downloading”)
141  * @param modified
142  *            Whether the Sone is modified
143  * @param locked
144  *            Whether the Sone is locked
145  * @param lastUpdated
146  *            The date and time of the last update (formatted for display)
147  */
148 function updateSoneStatus(soneId, name, status, modified, locked, lastUpdated) {
149         $("#sone .sone." + filterSoneId(soneId)).
150                 toggleClass("unknown", status == "unknown").
151                 toggleClass("idle", status == "idle").
152                 toggleClass("inserting", status == "inserting").
153                 toggleClass("downloading", status == "downloading").
154                 toggleClass("modified", modified);
155         $("#sone .sone." + filterSoneId(soneId) + " .lock").toggleClass("hidden", locked);
156         $("#sone .sone." + filterSoneId(soneId) + " .unlock").toggleClass("hidden", !locked);
157         if (lastUpdated != null) {
158                 $("#sone .sone." + filterSoneId(soneId) + " .last-update span.time").text(lastUpdated);
159         } else {
160                 getTranslation("View.Sone.Text.UnknownDate", function(unknown) {
161                         $("#sone .sone." + filterSoneId(soneId) + " .last-update span.time").text(unknown);
162                 });
163         }
164         $("#sone .sone." + filterSoneId(soneId) + " .profile-link a").text(name);
165 }
166
167 /**
168  * Enhances a “delete” button so that the confirmation is done on the same page.
169  *
170  * @param button
171  *            The button element
172  * @param text
173  *            The text to show on the button
174  * @param deleteCallback
175  *            The callback that actually deletes something
176  */
177 function enhanceDeleteButton(button, text, deleteCallback) {
178         (function(button) {
179                 newButton = $("<button></button>").addClass("confirm").hide().text(text).click(function() {
180                         $(this).fadeOut("slow");
181                         deleteCallback();
182                         return false;
183                 }).insertAfter(button);
184                 (function(button, newButton) {
185                         button.click(function() {
186                                 button.fadeOut("slow", function() {
187                                         newButton.fadeIn("slow");
188                                         $(document).one("click", function() {
189                                                 if (this != newButton.get(0)) {
190                                                         newButton.fadeOut(function() {
191                                                                 button.fadeIn();
192                                                         });
193                                                 }
194                                         });
195                                 });
196                                 return false;
197                         });
198                 })(button, newButton);
199         })($(button));
200 }
201
202 /**
203  * Enhances a post’s “delete” button.
204  *
205  * @param button
206  *            The button element
207  * @param postId
208  *            The ID of the post to delete
209  * @param text
210  *            The text to replace the button with
211  */
212 function enhanceDeletePostButton(button, postId, text) {
213         enhanceDeleteButton(button, text, function() {
214                 $.getJSON("deletePost.ajax", { "post": postId, "formPassword": getFormPassword() }, function(data, textStatus) {
215                         if (data == null) {
216                                 return;
217                         }
218                         if (data.success) {
219                                 $("#sone .post#" + postId).slideUp();
220                         } else if (data.error == "invalid-post-id") {
221                                 alert("Invalid post ID given!");
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                 $.getJSON("deleteReply.ajax", { "reply": replyId, "formPassword": $("#sone #formPassword").text() }, function(data, textStatus) {
246                         if (data == null) {
247                                 return;
248                         }
249                         if (data.success) {
250                                 $("#sone .reply#" + replyId).slideUp();
251                         } else if (data.error == "invalid-reply-id") {
252                                 alert("Invalid reply ID given!");
253                         } else if (data.error == "auth-required") {
254                                 alert("You need to be logged in.");
255                         } else if (data.error == "not-authorized") {
256                                 alert("You are not allowed to delete this reply.");
257                         }
258                 }, function(xmlHttpRequest, textStatus, error) {
259                         /* ignore error. */
260                 });
261         });
262 }
263
264 function getFormPassword() {
265         return $("#sone #formPassword").text();
266 }
267
268 function getSoneElement(element) {
269         return $(element).closest(".sone");
270 }
271
272 /**
273  * Generates a list of Sones by concatening the names of the given sones with a
274  * new line character (“\n”).
275  *
276  * @param sones
277  *            The sones to format
278  * @returns {String} The created string
279  */
280 function generateSoneList(sones) {
281         var soneList = "";
282         $.each(sones, function() {
283                 if (soneList != "") {
284                         soneList += ", ";
285                 }
286                 soneList += this.name;
287         });
288         return soneList;
289 }
290
291 /**
292  * Returns the ID of the Sone that this element belongs to.
293  *
294  * @param element
295  *            The element to locate the matching Sone ID for
296  * @returns The ID of the Sone, or undefined
297  */
298 function getSoneId(element) {
299         return getSoneElement(element).find(".id").text();
300 }
301
302 /**
303  * Returns the element of the post with the given ID.
304  *
305  * @param postId
306  *            The ID of the post
307  * @returns The element of the post
308  */
309 function getPost(postId) {
310         return $("#sone .post#" + postId);
311 }
312
313 function getPostElement(element) {
314         return $(element).closest(".post");
315 }
316
317 function getPostId(element) {
318         return getPostElement(element).attr("id");
319 }
320
321 function getPostTime(element) {
322         return getPostElement(element).find(".post-time").text();
323 }
324
325 /**
326  * Returns the author of the post the given element belongs to.
327  *
328  * @param element
329  *            The element whose post to get the author for
330  * @returns The ID of the authoring Sone
331  */
332 function getPostAuthor(element) {
333         return getPostElement(element).find(".post-author").text();
334 }
335
336 /**
337  * Returns the element of the reply with the given ID.
338  *
339  * @param replyId
340  *            The ID of the reply
341  * @returns The element of the reply
342  */
343 function getReply(replyId) {
344         return $("#sone .reply#" + replyId);
345 }
346
347 function getReplyElement(element) {
348         return $(element).closest(".reply");
349 }
350
351 function getReplyId(element) {
352         return getReplyElement(element).attr("id");
353 }
354
355 function getReplyTime(element) {
356         return getReplyElement(element).find(".reply-time").text();
357 }
358
359 /**
360  * Returns the author of the reply the given element belongs to.
361  *
362  * @param element
363  *            The element whose reply to get the author for
364  * @returns The ID of the authoring Sone
365  */
366 function getReplyAuthor(element) {
367         return getReplyElement(element).find(".reply-author").text();
368 }
369
370 function likePost(postId) {
371         $.getJSON("like.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
372                 if ((data == null) || !data.success) {
373                         return;
374                 }
375                 $("#sone .post#" + postId + " > .inner-part > .status-line .like").addClass("hidden");
376                 $("#sone .post#" + postId + " > .inner-part > .status-line .unlike").removeClass("hidden");
377                 updatePostLikes(postId);
378         }, function(xmlHttpRequest, textStatus, error) {
379                 /* ignore error. */
380         });
381 }
382
383 function unlikePost(postId) {
384         $.getJSON("unlike.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
385                 if ((data == null) || !data.success) {
386                         return;
387                 }
388                 $("#sone .post#" + postId + " > .inner-part > .status-line .unlike").addClass("hidden");
389                 $("#sone .post#" + postId + " > .inner-part > .status-line .like").removeClass("hidden");
390                 updatePostLikes(postId);
391         }, function(xmlHttpRequest, textStatus, error) {
392                 /* ignore error. */
393         });
394 }
395
396 function updatePostLikes(postId) {
397         $.getJSON("getLikes.ajax", { "type": "post", "post": postId }, function(data, textStatus) {
398                 if ((data != null) && data.success) {
399                         $("#sone .post#" + postId + " > .inner-part > .status-line .likes").toggleClass("hidden", data.likes == 0)
400                         $("#sone .post#" + postId + " > .inner-part > .status-line .likes span.like-count").text(data.likes);
401                         $("#sone .post#" + postId + " > .inner-part > .status-line .likes > span").attr("title", generateSoneList(data.sones));
402                 }
403         }, function(xmlHttpRequest, textStatus, error) {
404                 /* ignore error. */
405         });
406 }
407
408 function likeReply(replyId) {
409         $.getJSON("like.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
410                 if ((data == null) || !data.success) {
411                         return;
412                 }
413                 $("#sone .reply#" + replyId + " .status-line .like").addClass("hidden");
414                 $("#sone .reply#" + replyId + " .status-line .unlike").removeClass("hidden");
415                 updateReplyLikes(replyId);
416         }, function(xmlHttpRequest, textStatus, error) {
417                 /* ignore error. */
418         });
419 }
420
421 function unlikeReply(replyId) {
422         $.getJSON("unlike.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
423                 if ((data == null) || !data.success) {
424                         return;
425                 }
426                 $("#sone .reply#" + replyId + " .status-line .unlike").addClass("hidden");
427                 $("#sone .reply#" + replyId + " .status-line .like").removeClass("hidden");
428                 updateReplyLikes(replyId);
429         }, function(xmlHttpRequest, textStatus, error) {
430                 /* ignore error. */
431         });
432 }
433
434 /**
435  * Trusts the Sone with the given ID.
436  *
437  * @param soneId
438  *            The ID of the Sone to trust
439  */
440 function trustSone(soneId) {
441         $.getJSON("trustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
442                 if ((data != null) && data.success) {
443                         updateTrustControls(soneId, data.trustValue);
444                 }
445         });
446 }
447
448 /**
449  * Distrusts the Sone with the given ID, i.e. assigns a negative trust value.
450  *
451  * @param soneId
452  *            The ID of the Sone to distrust
453  */
454 function distrustSone(soneId) {
455         $.getJSON("distrustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
456                 if ((data != null) && data.success) {
457                         updateTrustControls(soneId, data.trustValue);
458                 }
459         });
460 }
461
462 /**
463  * Untrusts the Sone with the given ID, i.e. removes any trust assignment.
464  *
465  * @param soneId
466  *            The ID of the Sone to untrust
467  */
468 function untrustSone(soneId) {
469         $.getJSON("untrustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
470                 if ((data != null) && data.success) {
471                         updateTrustControls(soneId, data.trustValue);
472                 }
473         });
474 }
475
476 /**
477  * Updates the trust controls for all posts and replies of the given Sone,
478  * according to the given trust value.
479  *
480  * @param soneId
481  *            The ID of the Sone to update all trust controls for
482  * @param trustValue
483  *            The trust value for the Sone
484  */
485 function updateTrustControls(soneId, trustValue) {
486         $("#sone .post").each(function() {
487                 if (getPostAuthor(this) == soneId) {
488                         getPostElement(this).find(".post-trust").toggleClass("hidden", trustValue != null);
489                         getPostElement(this).find(".post-distrust").toggleClass("hidden", trustValue != null);
490                         getPostElement(this).find(".post-untrust").toggleClass("hidden", trustValue == null);
491                 }
492         });
493         $("#sone .reply").each(function() {
494                 if (getReplyAuthor(this) == soneId) {
495                         getReplyElement(this).find(".reply-trust").toggleClass("hidden", trustValue != null);
496                         getReplyElement(this).find(".reply-distrust").toggleClass("hidden", trustValue != null);
497                         getReplyElement(this).find(".reply-untrust").toggleClass("hidden", trustValue == null);
498                 }
499         });
500 }
501
502 /**
503  * Bookmarks the post with the given ID.
504  *
505  * @param postId
506  *            The ID of the post to bookmark
507  */
508 function bookmarkPost(postId) {
509         (function(postId) {
510                 $.getJSON("bookmark.ajax", {"formPassword": getFormPassword(), "type": "post", "post": postId}, function(data, textStatus) {
511                         if ((data != null) && data.success) {
512                                 getPost(postId).find(".bookmark").toggleClass("hidden", true);
513                                 getPost(postId).find(".unbookmark").toggleClass("hidden", false);
514                         }
515                 });
516         })(postId);
517 }
518
519 /**
520  * Unbookmarks the post with the given ID.
521  *
522  * @param postId
523  *            The ID of the post to unbookmark
524  */
525 function unbookmarkPost(postId) {
526         $.getJSON("unbookmark.ajax", {"formPassword": getFormPassword(), "type": "post", "post": postId}, function(data, textStatus) {
527                 if ((data != null) && data.success) {
528                         getPost(postId).find(".bookmark").toggleClass("hidden", false);
529                         getPost(postId).find(".unbookmark").toggleClass("hidden", true);
530                 }
531         });
532 }
533
534 function updateReplyLikes(replyId) {
535         $.getJSON("getLikes.ajax", { "type": "reply", "reply": replyId }, function(data, textStatus) {
536                 if ((data != null) && data.success) {
537                         $("#sone .reply#" + replyId + " .status-line .likes").toggleClass("hidden", data.likes == 0)
538                         $("#sone .reply#" + replyId + " .status-line .likes span.like-count").text(data.likes);
539                         $("#sone .reply#" + replyId + " .status-line .likes > span").attr("title", generateSoneList(data.sones));
540                 }
541         }, function(xmlHttpRequest, textStatus, error) {
542                 /* ignore error. */
543         });
544 }
545
546 /**
547  * Posts a reply and calls the given callback when the request finishes.
548  *
549  * @param sender
550  *            The ID of the sender
551  * @param postId
552  *            The ID of the post the reply refers to
553  * @param text
554  *            The text to post
555  * @param callbackFunction
556  *            The callback function to call when the request finishes (takes 3
557  *            parameters: success, error, replyId)
558  */
559 function postReply(sender, postId, text, callbackFunction) {
560         $.getJSON("createReply.ajax", { "formPassword" : getFormPassword(), "sender": sender, "post" : postId, "text": text }, function(data, textStatus) {
561                 if (data == null) {
562                         /* TODO - show error */
563                         return;
564                 }
565                 if (data.success) {
566                         callbackFunction(true, null, data.reply, data.sone);
567                 } else {
568                         callbackFunction(false, data.error);
569                 }
570         }, function(xmlHttpRequest, textStatus, error) {
571                 /* ignore error. */
572         });
573 }
574
575 /**
576  * Ajaxifies the given Sone by enhancing all eligible elements with AJAX.
577  *
578  * @param soneElement
579  *            The Sone to ajaxify
580  */
581 function ajaxifySone(soneElement) {
582         /*
583          * convert all “follow”, “unfollow”, “lock”, and “unlock” links to something
584          * nicer.
585          */
586         $(".follow", soneElement).submit(function() {
587                 var followElement = this;
588                 $.getJSON("followSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
589                         $(followElement).addClass("hidden");
590                         $(followElement).parent().find(".unfollow").removeClass("hidden");
591                 });
592                 return false;
593         });
594         $(".unfollow", soneElement).submit(function() {
595                 var unfollowElement = this;
596                 $.getJSON("unfollowSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
597                         $(unfollowElement).addClass("hidden");
598                         $(unfollowElement).parent().find(".follow").removeClass("hidden");
599                 });
600                 return false;
601         });
602         $(".lock", soneElement).submit(function() {
603                 var lockElement = this;
604                 $.getJSON("lockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
605                         $(lockElement).addClass("hidden");
606                         $(lockElement).parent().find(".unlock").removeClass("hidden");
607                 });
608                 return false;
609         });
610         $(".unlock", soneElement).submit(function() {
611                 var unlockElement = this;
612                 $.getJSON("unlockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
613                         $(unlockElement).addClass("hidden");
614                         $(unlockElement).parent().find(".lock").removeClass("hidden");
615                 });
616                 return false;
617         });
618
619         /* mark Sone as known when clicking it. */
620         $(soneElement).click(function() {
621                 markSoneAsKnown(soneElement);
622         });
623 }
624
625 /**
626  * Ajaxifies the given post by enhancing all eligible elements with AJAX.
627  *
628  * @param postElement
629  *            The post element to ajaxify
630  */
631 function ajaxifyPost(postElement) {
632         $(postElement).find("form").submit(function() {
633                 return false;
634         });
635         $(postElement).find(".create-reply button:submit").click(function() {
636                 button = $(this);
637                 button.attr("disabled", "disabled");
638                 sender = $(this.form).find(":input[name=sender]").val();
639                 inputField = $(this.form).find(":input[name=text]:enabled").get(0);
640                 postId = getPostId(this);
641                 text = $(inputField).val();
642                 (function(sender, postId, text, inputField) {
643                         postReply(sender, postId, text, function(success, error, replyId, soneId) {
644                                 if (success) {
645                                         $(inputField).val("");
646                                         loadNewReply(replyId, soneId, postId);
647                                         $("#sone .post#" + postId + " .create-reply").addClass("hidden");
648                                         $("#sone .post#" + postId + " .create-reply .sender").hide();
649                                         $("#sone .post#" + postId + " .create-reply .select-sender").show();
650                                         $("#sone .post#" + postId + " .create-reply :input[name=sender]").val(getCurrentSoneId());
651                                 } else {
652                                         alert(error);
653                                 }
654                                 button.removeAttr("disabled");
655                         });
656                 })(sender, postId, text, inputField);
657                 return false;
658         });
659
660         /* replace all “delete” buttons with javascript. */
661         (function(postElement) {
662                 getTranslation("WebInterface.Confirmation.DeletePostButton", function(deletePostText) {
663                         postId = getPostId(postElement);
664                         enhanceDeletePostButton($(postElement).find(".delete-post button"), postId, deletePostText);
665                 });
666         })(postElement);
667
668         /* convert all “like” buttons to javascript functions. */
669         $(postElement).find(".like-post").submit(function() {
670                 likePost(getPostId(this));
671                 return false;
672         });
673         $(postElement).find(".unlike-post").submit(function() {
674                 unlikePost(getPostId(this));
675                 return false;
676         });
677
678         /* convert trust control buttons to javascript functions. */
679         $(postElement).find(".post-trust").submit(function() {
680                 trustSone(getPostAuthor(this));
681                 return false;
682         });
683         $(postElement).find(".post-distrust").submit(function() {
684                 distrustSone(getPostAuthor(this));
685                 return false;
686         });
687         $(postElement).find(".post-untrust").submit(function() {
688                 untrustSone(getPostAuthor(this));
689                 return false;
690         });
691
692         /* convert bookmark/unbookmark buttons to javascript functions. */
693         $(postElement).find(".bookmark").submit(function() {
694                 bookmarkPost(getPostId(this));
695                 return false;
696         });
697         $(postElement).find(".unbookmark").submit(function() {
698                 unbookmarkPost(getPostId(this));
699                 return false;
700         });
701
702         /* convert “show source” link into javascript function. */
703         $(postElement).find(".show-source").each(function() {
704                 $("a", this).click(function() {
705                         $(".post-text.text", getPostElement(this)).toggleClass("hidden");
706                         $(".post-text.raw-text", getPostElement(this)).toggleClass("hidden");
707                         return false;
708                 });
709         });
710
711         /* add “comment” link. */
712         addCommentLink(getPostId(postElement), postElement, $(postElement).find(".post-status-line .time"));
713
714         /* process all replies. */
715         $(postElement).find(".reply").each(function() {
716                 ajaxifyReply(this);
717         });
718
719         /* process reply input fields. */
720         getTranslation("WebInterface.DefaultText.Reply", function(text) {
721                 $(postElement).find("input.reply-input").each(function() {
722                         registerInputTextareaSwap(this, text, "text", false, false);
723                 });
724         });
725
726         /* process sender selection. */
727         $(".select-sender", postElement).css("display", "inline");
728         $(".sender", postElement).hide();
729         $(".select-sender button", postElement).click(function() {
730                 $(".sender", postElement).show();
731                 $(".select-sender", postElement).hide();
732                 return false;
733         });
734
735         /* mark everything as known on click. */
736         $(postElement).click(function(event) {
737                 if ($(event.target).hasClass("click-to-show")) {
738                         return false;
739                 }
740                 markPostAsKnown(this);
741         });
742
743         /* hide reply input field. */
744         $(postElement).find(".create-reply").addClass("hidden");
745 }
746
747 /**
748  * Ajaxifies the given reply element.
749  *
750  * @param replyElement
751  *            The reply element to ajaxify
752  */
753 function ajaxifyReply(replyElement) {
754         $(replyElement).find(".like-reply").submit(function() {
755                 likeReply(getReplyId(this));
756                 return false;
757         });
758         $(replyElement).find(".unlike-reply").submit(function() {
759                 unlikeReply(getReplyId(this));
760                 return false;
761         });
762         (function(replyElement) {
763                 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(deleteReplyText) {
764                         $(replyElement).find(".delete-reply button").each(function() {
765                                 enhanceDeleteReplyButton(this, getReplyId(replyElement), deleteReplyText);
766                         });
767                 });
768         })(replyElement);
769         addCommentLink(getPostId(replyElement), replyElement, $(replyElement).find(".reply-status-line .time"));
770
771         /* convert “show source” link into javascript function. */
772         $(replyElement).find(".show-reply-source").each(function() {
773                 $("a", this).click(function() {
774                         $(".reply-text.text", getReplyElement(this)).toggleClass("hidden");
775                         $(".reply-text.raw-text", getReplyElement(this)).toggleClass("hidden");
776                         return false;
777                 });
778         });
779
780         /* convert trust control buttons to javascript functions. */
781         $(replyElement).find(".reply-trust").submit(function() {
782                 trustSone(getReplyAuthor(this));
783                 return false;
784         });
785         $(replyElement).find(".reply-distrust").submit(function() {
786                 distrustSone(getReplyAuthor(this));
787                 return false;
788         });
789         $(replyElement).find(".reply-untrust").submit(function() {
790                 untrustSone(getReplyAuthor(this));
791                 return false;
792         });
793 }
794
795 /**
796  * Ajaxifies the given notification by replacing the form with AJAX.
797  *
798  * @param notification
799  *            jQuery object representing the notification.
800  */
801 function ajaxifyNotification(notification) {
802         notification.find("form").submit(function() {
803                 return false;
804         });
805         notification.find("input[name=returnPage]").val($.url.attr("relative"));
806         if (notification.find(".short-text").length > 0) {
807                 notification.find(".short-text").removeClass("hidden");
808                 notification.find(".text").addClass("hidden");
809         }
810         notification.find("form.mark-as-read button").click(function() {
811                 $.getJSON("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": $(":input[name=type]", this.form).val(), "id": $(":input[name=id]", this.form).val()});
812         });
813         notification.find("a[class^='link-']").each(function() {
814                 linkElement = $(this);
815                 if (linkElement.is("[href^='viewPost']")) {
816                         id = linkElement.attr("class").substr(5);
817                         if (hasPost(id)) {
818                                 linkElement.attr("href", "#post-" + id);
819                         }
820                 }
821         });
822         notification.find("form.dismiss button").click(function() {
823                 $.getJSON("dismissNotification.ajax", { "formPassword" : getFormPassword(), "notification" : notification.attr("id") }, function(data, textStatus) {
824                         /* dismiss in case of error, too. */
825                         notification.slideUp();
826                 }, function(xmlHttpRequest, textStatus, error) {
827                         /* ignore error. */
828                 });
829         });
830         return notification;
831 }
832
833 function getStatus() {
834         $.getJSON("getStatus.ajax", {"loadAllSones": isKnownSonesPage()}, function(data, textStatus) {
835                 if ((data != null) && data.success) {
836                         /* process Sone information. */
837                         $.each(data.sones, function(index, value) {
838                                 updateSoneStatus(value.id, value.name, value.status, value.modified, value.locked, value.lastUpdatedUnknown ? null : value.lastUpdated);
839                         });
840                         /* search for removed notifications. */
841                         $("#sone #notification-area .notification").each(function() {
842                                 notificationId = $(this).attr("id");
843                                 foundNotification = false;
844                                 $.each(data.notifications, function(index, value) {
845                                         if (value.id == notificationId) {
846                                                 foundNotification = true;
847                                                 return false;
848                                         }
849                                 });
850                                 if (!foundNotification) {
851                                         $(this).slideUp("normal", function() {
852                                                 $(this).remove();
853                                         });
854                                 }
855                         });
856                         /* process notifications. */
857                         $.each(data.notifications, function(index, value) {
858                                 oldNotification = $("#sone #notification-area .notification#" + value.id);
859                                 notification = ajaxifyNotification(createNotification(value.id, value.text, value.dismissable)).hide();
860                                 if (oldNotification.length != 0) {
861                                         if ((oldNotification.find(".short-text").length > 0) && (notification.find(".short-text").length > 0)) {
862                                                 opened = oldNotification.is(":visible") && oldNotification.find(".short-text").hasClass("hidden");
863                                                 notification.find(".short-text").toggleClass("hidden", opened);
864                                                 notification.find(".text").toggleClass("hidden", !opened);
865                                         }
866                                         oldNotification.replaceWith(notification.show());
867                                 } else {
868                                         $("#sone #notification-area").append(notification);
869                                         notification.slideDown();
870                                         setActivity();
871                                 }
872                         });
873                         /* process new posts. */
874                         $.each(data.newPosts, function(index, value) {
875                                 loadNewPost(value.id, value.sone, value.recipient, value.time);
876                         });
877                         /* process new replies. */
878                         $.each(data.newReplies, function(index, value) {
879                                 loadNewReply(value.id, value.sone, value.post, value.postSone);
880                         });
881                         /* do it again in 5 seconds. */
882                         setTimeout(getStatus, 5000);
883                 } else {
884                         /* data.success was false, wait 30 seconds. */
885                         setTimeout(getStatus, 30000);
886                 }
887         }, function(xmlHttpRequest, textStatus, error) {
888                 /* something really bad happend, wait a minute. */
889                 setTimeout(getStatus, 60000);
890         })
891 }
892
893 /**
894  * Returns the ID of the currently logged in Sone.
895  *
896  * @return The ID of the current Sone, or an empty string if no Sone is logged
897  *         in
898  */
899 function getCurrentSoneId() {
900         return $("#currentSoneId").text();
901 }
902
903 /**
904  * Returns the content of the page-id attribute.
905  *
906  * @returns The page ID
907  */
908 function getPageId() {
909         return $("#sone .page-id").text();
910 }
911
912 /**
913  * Returns whether the current page is the index page.
914  *
915  * @returns {Boolean} <code>true</code> if the current page is the index page,
916  *          <code>false</code> otherwise
917  */
918 function isIndexPage() {
919         return getPageId() == "index";
920 }
921
922 /**
923  * Returns whether the current page is a “view Sone” page.
924  *
925  * @returns {Boolean} <code>true</code> if the current page is a “view Sone”
926  *          page, <code>false</code> otherwise
927  */
928 function isViewSonePage() {
929         return getPageId() == "view-sone";
930 }
931
932 /**
933  * Returns the ID of the currently shown Sone. This will only return a sensible
934  * value if isViewSonePage() returns <code>true</code>.
935  *
936  * @returns The ID of the currently shown Sone
937  */
938 function getShownSoneId() {
939         return $("#sone .sone-id").text();
940 }
941
942 /**
943  * Returns whether the current page is a “view post” page.
944  *
945  * @returns {Boolean} <code>true</code> if the current page is a “view post”
946  *          page, <code>false</code> otherwise
947  */
948 function isViewPostPage() {
949         return getPageId() == "view-post";
950 }
951
952 /**
953  * Returns the ID of the currently shown post. This will only return a sensible
954  * value if isViewPostPage() returns <code>true</code>.
955  *
956  * @returns The ID of the currently shown post
957  */
958 function getShownPostId() {
959         return $("#sone .post-id").text();
960 }
961
962 /**
963  * Returns whether the current page is the “known Sones” page.
964  *
965  * @returns {Boolean} <code>true</code> if the current page is the “known
966  *          Sones” page, <code>false</code> otherwise
967  */
968 function isKnownSonesPage() {
969         return getPageId() == "known-sones";
970 }
971
972 /**
973  * Returns whether a post with the given ID exists on the current page.
974  *
975  * @param postId
976  *            The post ID to check for
977  * @returns {Boolean} <code>true</code> if a post with the given ID already
978  *          exists on the page, <code>false</code> otherwise
979  */
980 function hasPost(postId) {
981         return $(".post#" + postId).length > 0;
982 }
983
984 /**
985  * Returns whether a reply with the given ID exists on the current page.
986  *
987  * @param replyId
988  *            The reply ID to check for
989  * @returns {Boolean} <code>true</code> if a reply with the given ID already
990  *          exists on the page, <code>false</code> otherwise
991  */
992 function hasReply(replyId) {
993         return $("#sone .reply#" + replyId).length > 0;
994 }
995
996 function loadNewPost(postId, soneId, recipientId, time) {
997         if (hasPost(postId)) {
998                 return;
999         }
1000         if (!isIndexPage()) {
1001                 if (!isViewPostPage() || (getShownPostId() != postId)) {
1002                         if (!isViewSonePage() || ((getShownSoneId() != soneId) && (getShownSoneId() != recipientId))) {
1003                                 return;
1004                         }
1005                 }
1006         }
1007         if (getPostTime($("#sone .post").last()) > time) {
1008                 return;
1009         }
1010         $.getJSON("getPost.ajax", { "post" : postId }, function(data, textStatus) {
1011                 if ((data != null) && data.success) {
1012                         if (hasPost(data.post.id)) {
1013                                 return;
1014                         }
1015                         if (!isIndexPage() && !(isViewSonePage() && ((getShownSoneId() == data.post.sone) || (getShownSoneId() == data.post.recipient)))) {
1016                                 return;
1017                         }
1018                         var firstOlderPost = null;
1019                         $("#sone .post").each(function() {
1020                                 if (getPostTime(this) < data.post.time) {
1021                                         firstOlderPost = $(this);
1022                                         return false;
1023                                 }
1024                         });
1025                         newPost = $(data.post.html).addClass("hidden");
1026                         if (firstOlderPost != null) {
1027                                 newPost.insertBefore(firstOlderPost);
1028                         }
1029                         ajaxifyPost(newPost);
1030                         newPost.slideDown();
1031                         setActivity();
1032                 }
1033         });
1034 }
1035
1036 function loadNewReply(replyId, soneId, postId, postSoneId) {
1037         if (hasReply(replyId)) {
1038                 return;
1039         }
1040         if (!hasPost(postId)) {
1041                 return;
1042         }
1043         $.getJSON("getReply.ajax", { "reply": replyId }, function(data, textStatus) {
1044                 /* find post. */
1045                 if ((data != null) && data.success) {
1046                         if (hasReply(data.reply.id)) {
1047                                 return;
1048                         }
1049                         $("#sone .post#" + data.reply.postId).each(function() {
1050                                 var firstNewerReply = null;
1051                                 $(this).find(".replies .reply").each(function() {
1052                                         if (getReplyTime(this) > data.reply.time) {
1053                                                 firstNewerReply = $(this);
1054                                                 return false;
1055                                         }
1056                                 });
1057                                 newReply = $(data.reply.html).addClass("hidden");
1058                                 if (firstNewerReply != null) {
1059                                         newReply.insertBefore(firstNewerReply);
1060                                 } else {
1061                                         if ($(this).find(".replies .create-reply")) {
1062                                                 $(this).find(".replies .create-reply").before(newReply);
1063                                         } else {
1064                                                 $(this).find(".replies").append(newReply);
1065                                         }
1066                                 }
1067                                 ajaxifyReply(newReply);
1068                                 newReply.slideDown();
1069                                 setActivity();
1070                                 return false;
1071                         });
1072                 }
1073         });
1074 }
1075
1076 /**
1077  * Marks the given Sone as known if it is still new.
1078  *
1079  * @param soneElement
1080  *            The Sone to mark as known
1081  */
1082 function markSoneAsKnown(soneElement) {
1083         if ($(".new", soneElement).length > 0) {
1084                 $.getJSON("maskAsKnown.ajax", {"formPassword": getFormPassword(), "type": "sone", "id": getSoneId(soneElement)}, function(data, textStatus) {
1085                         $(soneElement).removeClass("new");
1086                 });
1087         }
1088 }
1089
1090 function markPostAsKnown(postElements) {
1091         $(postElements).each(function() {
1092                 postElement = this;
1093                 if ($(postElement).hasClass("new")) {
1094                         (function(postElement) {
1095                                 $(postElement).removeClass("new");
1096                                 $(".click-to-show", postElement).removeClass("new");
1097                                 $.getJSON("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "post", "id": getPostId(postElement)});
1098                         })(postElement);
1099                 }
1100         });
1101         markReplyAsKnown($(postElements).find(".reply"));
1102 }
1103
1104 function markReplyAsKnown(replyElements) {
1105         $(replyElements).each(function() {
1106                 replyElement = this;
1107                 if ($(replyElement).hasClass("new")) {
1108                         (function(replyElement) {
1109                                 $(replyElement).removeClass("new");
1110                                 $.getJSON("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "reply", "id": getReplyId(replyElement)});
1111                         })(replyElement);
1112                 }
1113         });
1114 }
1115
1116 function resetActivity() {
1117         title = document.title;
1118         if (title.indexOf('(') == 0) {
1119                 setTitle(title.substr(title.indexOf(' ') + 1));
1120         }
1121 }
1122
1123 function setActivity() {
1124         if (!focus) {
1125                 title = document.title;
1126                 if (title.indexOf('(') != 0) {
1127                         setTitle("(!) " + title);
1128                 }
1129                 if (!iconBlinking) {
1130                         setTimeout(toggleIcon, 1500);
1131                         iconBlinking = true;
1132                 }
1133         }
1134 }
1135
1136 /**
1137  * Sets the window title after a small delay to prevent race-condition issues.
1138  *
1139  * @param title
1140  *            The title to set
1141  */
1142 function setTitle(title) {
1143         setTimeout(function() {
1144                 document.title = title;
1145         }, 50);
1146 }
1147
1148 /** Whether the icon is currently showing activity. */
1149 var iconActive = false;
1150
1151 /** Whether the icon is currently supposed to blink. */
1152 var iconBlinking = false;
1153
1154 /**
1155  * Toggles the icon. If the window has gained focus and the icon is still
1156  * showing the activity state, it is returned to normal.
1157  */
1158 function toggleIcon() {
1159         if (focus) {
1160                 if (iconActive) {
1161                         changeIcon("images/icon.png");
1162                         iconActive = false;
1163                 }
1164                 iconBlinking = false;
1165         } else {
1166                 iconActive = !iconActive;
1167                 changeIcon(iconActive ? "images/icon-activity.png" : "images/icon.png");
1168                 setTimeout(toggleIcon, 1500);
1169         }
1170 }
1171
1172 /**
1173  * Changes the icon of the page.
1174  *
1175  * @param iconUrl
1176  *            The new URL of the icon
1177  */
1178 function changeIcon(iconUrl) {
1179         $("link[rel=icon]").remove();
1180         $("head").append($("<link>").attr("rel", "icon").attr("type", "image/png").attr("href", iconUrl));
1181         $("iframe[id=icon-update]")[0].src += "";
1182 }
1183
1184 /**
1185  * Creates a new notification.
1186  *
1187  * @param id
1188  *            The ID of the notificaiton
1189  * @param text
1190  *            The text of the notification
1191  * @param dismissable
1192  *            <code>true</code> if the notification can be dismissed by the
1193  *            user
1194  */
1195 function createNotification(id, text, dismissable) {
1196         notification = $("<div></div>").addClass("notification").attr("id", id);
1197         if (dismissable) {
1198                 dismissForm = $("#sone #notification-area #notification-dismiss-template").clone().removeClass("hidden").removeAttr("id")
1199                 dismissForm.find("input[name=notification]").val(id);
1200                 notification.append(dismissForm);
1201         }
1202         notification.append(text);
1203         return notification;
1204 }
1205
1206 /**
1207  * Shows the details of the notification with the given ID.
1208  *
1209  * @param notificationId
1210  *            The ID of the notification
1211  */
1212 function showNotificationDetails(notificationId) {
1213         $("#sone .notification#" + notificationId + " .text").removeClass("hidden");
1214         $("#sone .notification#" + notificationId + " .short-text").addClass("hidden");
1215 }
1216
1217 /**
1218  * Deletes the field with the given ID from the profile.
1219  *
1220  * @param fieldId
1221  *            The ID of the field to delete
1222  */
1223 function deleteProfileField(fieldId) {
1224         $.getJSON("deleteProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId}, function(data, textStatus) {
1225                 if (data && data.success) {
1226                         $("#sone .profile-field#" + data.field.id).slideUp();
1227                 }
1228         });
1229 }
1230
1231 /**
1232  * Renames a profile field.
1233  *
1234  * @param fieldId
1235  *            The ID of the field to rename
1236  * @param newName
1237  *            The new name of the field
1238  * @param successFunction
1239  *            Called when the renaming was successful
1240  */
1241 function editProfileField(fieldId, newName, successFunction) {
1242         $.getJSON("editProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId, "name": newName}, function(data, textStatus) {
1243                 if (data && data.success) {
1244                         successFunction();
1245                 }
1246         });
1247 }
1248
1249 /**
1250  * Moves the profile field with the given ID one slot in the given direction.
1251  *
1252  * @param fieldId
1253  *            The ID of the field to move
1254  * @param direction
1255  *            The direction to move in (“up” or “down”)
1256  * @param successFunction
1257  *            Function to call on success
1258  */
1259 function moveProfileField(fieldId, direction, successFunction) {
1260         $.getJSON("moveProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId, "direction": direction}, function(data, textStatus) {
1261                 if (data && data.success) {
1262                         successFunction();
1263                 }
1264         });
1265 }
1266
1267 /**
1268  * Moves the profile field with the given ID up one slot.
1269  *
1270  * @param fieldId
1271  *            The ID of the field to move
1272  * @param successFunction
1273  *            Function to call on success
1274  */
1275 function moveProfileFieldUp(fieldId, successFunction) {
1276         moveProfileField(fieldId, "up", successFunction);
1277 }
1278
1279 /**
1280  * Moves the profile field with the given ID down one slot.
1281  *
1282  * @param fieldId
1283  *            The ID of the field to move
1284  * @param successFunction
1285  *            Function to call on success
1286  */
1287 function moveProfileFieldDown(fieldId, successFunction) {
1288         moveProfileField(fieldId, "down", successFunction);
1289 }
1290
1291 //
1292 // EVERYTHING BELOW HERE IS EXECUTED AFTER LOADING THE PAGE
1293 //
1294
1295 var focus = true;
1296
1297 $(document).ready(function() {
1298
1299         /* this initializes the status update input field. */
1300         getTranslation("WebInterface.DefaultText.StatusUpdate", function(defaultText) {
1301                 registerInputTextareaSwap("#sone #update-status .status-input", defaultText, "text", false, false);
1302                 $("#sone #update-status .select-sender").css("display", "inline");
1303                 $("#sone #update-status .sender").hide();
1304                 $("#sone #update-status .select-sender button").click(function() {
1305                         $("#sone #update-status .sender").show();
1306                         $("#sone #update-status .select-sender").hide();
1307                         return false;
1308                 });
1309                 $("#sone #update-status").submit(function() {
1310                         button = $("button:submit", this);
1311                         button.attr("disabled", "disabled");
1312                         if ($(this).find(":input.default:enabled").length > 0) {
1313                                 return false;
1314                         }
1315                         sender = $(this).find(":input[name=sender]").val();
1316                         text = $(this).find(":input[name=text]:enabled").val();
1317                         $.getJSON("createPost.ajax", { "formPassword": getFormPassword(), "sender": sender, "text": text }, function(data, textStatus) {
1318                                 if ((data != null) && data.success) {
1319                                         loadNewPost(data.postId, data.sone, data.recipient);
1320                                 }
1321                                 button.removeAttr("disabled");
1322                         });
1323                         $(this).find(":input[name=sender]").val(getCurrentSoneId());
1324                         $(this).find(":input[name=text]:enabled").val("").blur();
1325                         $(this).find(".sender").hide();
1326                         $(this).find(".select-sender").show();
1327                         return false;
1328                 });
1329         });
1330
1331         /* ajaxify the search input field. */
1332         getTranslation("WebInterface.DefaultText.Search", function(defaultText) {
1333                 registerInputTextareaSwap("#sone #search input[name=query]", defaultText, "query", false, true);
1334         });
1335
1336         /* ajaxify input field on “view Sone” page. */
1337         getTranslation("WebInterface.DefaultText.Message", function(defaultText) {
1338                 registerInputTextareaSwap("#sone #post-message input[name=text]", defaultText, "text", false, false);
1339                 $("#sone #post-message .select-sender").css("display", "inline");
1340                 $("#sone #post-message .sender").hide();
1341                 $("#sone #post-message .select-sender button").click(function() {
1342                         $("#sone #post-message .sender").show();
1343                         $("#sone #post-message .select-sender").hide();
1344                         return false;
1345                 });
1346                 $("#sone #post-message").submit(function() {
1347                         sender = $(this).find(":input[name=sender]").val();
1348                         text = $(this).find(":input[name=text]:enabled").val();
1349                         $.getJSON("createPost.ajax", { "formPassword": getFormPassword(), "recipient": getShownSoneId(), "sender": sender, "text": text }, function(data, textStatus) {
1350                                 if ((data != null) && data.success) {
1351                                         loadNewPost(data.postId, getCurrentSoneId());
1352                                 }
1353                         });
1354                         $(this).find(":input[name=sender]").val(getCurrentSoneId());
1355                         $(this).find(":input[name=text]:enabled").val("").blur();
1356                         $(this).find(".sender").hide();
1357                         $(this).find(".select-sender").show();
1358                         return false;
1359                 });
1360         });
1361
1362         /* Ajaxifies all posts. */
1363         /* calling getTranslation here will cache the necessary values. */
1364         getTranslation("WebInterface.Confirmation.DeletePostButton", function(text) {
1365                 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(text) {
1366                         getTranslation("WebInterface.DefaultText.Reply", function(text) {
1367                                 $("#sone .post").each(function() {
1368                                         ajaxifyPost(this);
1369                                 });
1370                         });
1371                 });
1372         });
1373
1374         /* hides all replies but the latest two. */
1375         if (!isViewPostPage()) {
1376                 getTranslation("WebInterface.ClickToShow.Replies", function(text) {
1377                         $("#sone .post .replies").each(function() {
1378                                 allReplies = $(this).find(".reply");
1379                                 if (allReplies.length > 2) {
1380                                         newHidden = false;
1381                                         for (replyIndex = 0; replyIndex < (allReplies.length - 2); ++replyIndex) {
1382                                                 $(allReplies[replyIndex]).addClass("hidden");
1383                                                 newHidden |= $(allReplies[replyIndex]).hasClass("new");
1384                                         }
1385                                         clickToShowElement = $("<div></div>").addClass("click-to-show");
1386                                         if (newHidden) {
1387                                                 clickToShowElement.addClass("new");
1388                                         }
1389                                         (function(clickToShowElement, allReplies, text) {
1390                                                 clickToShowElement.text(text);
1391                                                 clickToShowElement.click(function() {
1392                                                         allReplies.removeClass("hidden");
1393                                                         clickToShowElement.addClass("hidden");
1394                                                 });
1395                                         })(clickToShowElement, allReplies, text);
1396                                         $(allReplies[0]).before(clickToShowElement);
1397                                 }
1398                         });
1399                 });
1400         }
1401
1402         $("#sone .sone").each(function() {
1403                 ajaxifySone($(this));
1404         });
1405
1406         /* process all existing notifications, ajaxify dismiss buttons. */
1407         $("#sone #notification-area .notification").each(function() {
1408                 ajaxifyNotification($(this));
1409         });
1410
1411         /* activate status polling. */
1412         setTimeout(getStatus, 5000);
1413
1414         /* reset activity counter when the page has focus. */
1415         $(window).focus(function() {
1416                 focus = true;
1417                 resetActivity();
1418         }).blur(function() {
1419                 focus = false;
1420         })
1421
1422 });