Fix search term input field jumping around on Firefox.
[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 function getReplyElement(element) {
337         return $(element).closest(".reply");
338 }
339
340 function getReplyId(element) {
341         return getReplyElement(element).attr("id");
342 }
343
344 function getReplyTime(element) {
345         return getReplyElement(element).find(".reply-time").text();
346 }
347
348 /**
349  * Returns the author of the reply the given element belongs to.
350  *
351  * @param element
352  *            The element whose reply to get the author for
353  * @returns The ID of the authoring Sone
354  */
355 function getReplyAuthor(element) {
356         return getReplyElement(element).find(".reply-author").text();
357 }
358
359 function likePost(postId) {
360         $.getJSON("like.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
361                 if ((data == null) || !data.success) {
362                         return;
363                 }
364                 $("#sone .post#" + postId + " > .inner-part > .status-line .like").addClass("hidden");
365                 $("#sone .post#" + postId + " > .inner-part > .status-line .unlike").removeClass("hidden");
366                 updatePostLikes(postId);
367         }, function(xmlHttpRequest, textStatus, error) {
368                 /* ignore error. */
369         });
370 }
371
372 function unlikePost(postId) {
373         $.getJSON("unlike.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
374                 if ((data == null) || !data.success) {
375                         return;
376                 }
377                 $("#sone .post#" + postId + " > .inner-part > .status-line .unlike").addClass("hidden");
378                 $("#sone .post#" + postId + " > .inner-part > .status-line .like").removeClass("hidden");
379                 updatePostLikes(postId);
380         }, function(xmlHttpRequest, textStatus, error) {
381                 /* ignore error. */
382         });
383 }
384
385 function updatePostLikes(postId) {
386         $.getJSON("getLikes.ajax", { "type": "post", "post": postId }, function(data, textStatus) {
387                 if ((data != null) && data.success) {
388                         $("#sone .post#" + postId + " > .inner-part > .status-line .likes").toggleClass("hidden", data.likes == 0)
389                         $("#sone .post#" + postId + " > .inner-part > .status-line .likes span.like-count").text(data.likes);
390                         $("#sone .post#" + postId + " > .inner-part > .status-line .likes > span").attr("title", generateSoneList(data.sones));
391                 }
392         }, function(xmlHttpRequest, textStatus, error) {
393                 /* ignore error. */
394         });
395 }
396
397 function likeReply(replyId) {
398         $.getJSON("like.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
399                 if ((data == null) || !data.success) {
400                         return;
401                 }
402                 $("#sone .reply#" + replyId + " .status-line .like").addClass("hidden");
403                 $("#sone .reply#" + replyId + " .status-line .unlike").removeClass("hidden");
404                 updateReplyLikes(replyId);
405         }, function(xmlHttpRequest, textStatus, error) {
406                 /* ignore error. */
407         });
408 }
409
410 function unlikeReply(replyId) {
411         $.getJSON("unlike.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
412                 if ((data == null) || !data.success) {
413                         return;
414                 }
415                 $("#sone .reply#" + replyId + " .status-line .unlike").addClass("hidden");
416                 $("#sone .reply#" + replyId + " .status-line .like").removeClass("hidden");
417                 updateReplyLikes(replyId);
418         }, function(xmlHttpRequest, textStatus, error) {
419                 /* ignore error. */
420         });
421 }
422
423 /**
424  * Trusts the Sone with the given ID.
425  *
426  * @param soneId
427  *            The ID of the Sone to trust
428  */
429 function trustSone(soneId) {
430         $.getJSON("trustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
431                 if ((data != null) && data.success) {
432                         updateTrustControls(soneId, data.trustValue);
433                 }
434         });
435 }
436
437 /**
438  * Distrusts the Sone with the given ID, i.e. assigns a negative trust value.
439  *
440  * @param soneId
441  *            The ID of the Sone to distrust
442  */
443 function distrustSone(soneId) {
444         $.getJSON("distrustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
445                 if ((data != null) && data.success) {
446                         updateTrustControls(soneId, data.trustValue);
447                 }
448         });
449 }
450
451 /**
452  * Untrusts the Sone with the given ID, i.e. removes any trust assignment.
453  *
454  * @param soneId
455  *            The ID of the Sone to untrust
456  */
457 function untrustSone(soneId) {
458         $.getJSON("untrustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
459                 if ((data != null) && data.success) {
460                         updateTrustControls(soneId, data.trustValue);
461                 }
462         });
463 }
464
465 /**
466  * Updates the trust controls for all posts and replies of the given Sone,
467  * according to the given trust value.
468  *
469  * @param soneId
470  *            The ID of the Sone to update all trust controls for
471  * @param trustValue
472  *            The trust value for the Sone
473  */
474 function updateTrustControls(soneId, trustValue) {
475         $("#sone .post").each(function() {
476                 if (getPostAuthor(this) == soneId) {
477                         getPostElement(this).find(".post-trust").toggleClass("hidden", trustValue != null);
478                         getPostElement(this).find(".post-distrust").toggleClass("hidden", trustValue != null);
479                         getPostElement(this).find(".post-untrust").toggleClass("hidden", trustValue == null);
480                 }
481         });
482         $("#sone .reply").each(function() {
483                 if (getReplyAuthor(this) == soneId) {
484                         getReplyElement(this).find(".reply-trust").toggleClass("hidden", trustValue != null);
485                         getReplyElement(this).find(".reply-distrust").toggleClass("hidden", trustValue != null);
486                         getReplyElement(this).find(".reply-untrust").toggleClass("hidden", trustValue == null);
487                 }
488         });
489 }
490
491 /**
492  * Bookmarks the post with the given ID.
493  *
494  * @param postId
495  *            The ID of the post to bookmark
496  */
497 function bookmarkPost(postId) {
498         (function(postId) {
499                 $.getJSON("bookmark.ajax", {"formPassword": getFormPassword(), "type": "post", "post": postId}, function(data, textStatus) {
500                         if ((data != null) && data.success) {
501                                 getPost(postId).find(".bookmark").toggleClass("hidden", true);
502                                 getPost(postId).find(".unbookmark").toggleClass("hidden", false);
503                         }
504                 });
505         })(postId);
506 }
507
508 /**
509  * Unbookmarks the post with the given ID.
510  *
511  * @param postId
512  *            The ID of the post to unbookmark
513  */
514 function unbookmarkPost(postId) {
515         $.getJSON("unbookmark.ajax", {"formPassword": getFormPassword(), "type": "post", "post": postId}, function(data, textStatus) {
516                 if ((data != null) && data.success) {
517                         getPost(postId).find(".bookmark").toggleClass("hidden", false);
518                         getPost(postId).find(".unbookmark").toggleClass("hidden", true);
519                 }
520         });
521 }
522
523 function updateReplyLikes(replyId) {
524         $.getJSON("getLikes.ajax", { "type": "reply", "reply": replyId }, function(data, textStatus) {
525                 if ((data != null) && data.success) {
526                         $("#sone .reply#" + replyId + " .status-line .likes").toggleClass("hidden", data.likes == 0)
527                         $("#sone .reply#" + replyId + " .status-line .likes span.like-count").text(data.likes);
528                         $("#sone .reply#" + replyId + " .status-line .likes > span").attr("title", generateSoneList(data.sones));
529                 }
530         }, function(xmlHttpRequest, textStatus, error) {
531                 /* ignore error. */
532         });
533 }
534
535 /**
536  * Posts a reply and calls the given callback when the request finishes.
537  *
538  * @param sender
539  *            The ID of the sender
540  * @param postId
541  *            The ID of the post the reply refers to
542  * @param text
543  *            The text to post
544  * @param callbackFunction
545  *            The callback function to call when the request finishes (takes 3
546  *            parameters: success, error, replyId)
547  */
548 function postReply(sender, postId, text, callbackFunction) {
549         $.getJSON("createReply.ajax", { "formPassword" : getFormPassword(), "sender": sender, "post" : postId, "text": text }, function(data, textStatus) {
550                 if (data == null) {
551                         /* TODO - show error */
552                         return;
553                 }
554                 if (data.success) {
555                         callbackFunction(true, null, data.reply, data.sone);
556                 } else {
557                         callbackFunction(false, data.error);
558                 }
559         }, function(xmlHttpRequest, textStatus, error) {
560                 /* ignore error. */
561         });
562 }
563
564 /**
565  * Requests information about the reply with the given ID.
566  *
567  * @param replyId
568  *            The ID of the reply
569  * @param callbackFunction
570  *            A callback function (parameters soneId, soneName, replyTime,
571  *            replyDisplayTime, text, html)
572  */
573 function getReply(replyId, callbackFunction) {
574         $.getJSON("getReply.ajax", { "reply" : replyId }, function(data, textStatus) {
575                 if ((data != null) && data.success) {
576                         callbackFunction(data.soneId, data.soneName, data.time, data.displayTime, data.text, data.html);
577                 }
578         }, function(xmlHttpRequest, textStatus, error) {
579                 /* ignore error. */
580         });
581 }
582
583 /**
584  * Ajaxifies the given Sone by enhancing all eligible elements with AJAX.
585  *
586  * @param soneElement
587  *            The Sone to ajaxify
588  */
589 function ajaxifySone(soneElement) {
590         /*
591          * convert all “follow”, “unfollow”, “lock”, and “unlock” links to something
592          * nicer.
593          */
594         $(".follow", soneElement).submit(function() {
595                 var followElement = this;
596                 $.getJSON("followSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
597                         $(followElement).addClass("hidden");
598                         $(followElement).parent().find(".unfollow").removeClass("hidden");
599                 });
600                 return false;
601         });
602         $(".unfollow", soneElement).submit(function() {
603                 var unfollowElement = this;
604                 $.getJSON("unfollowSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
605                         $(unfollowElement).addClass("hidden");
606                         $(unfollowElement).parent().find(".follow").removeClass("hidden");
607                 });
608                 return false;
609         });
610         $(".lock", soneElement).submit(function() {
611                 var lockElement = this;
612                 $.getJSON("lockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
613                         $(lockElement).addClass("hidden");
614                         $(lockElement).parent().find(".unlock").removeClass("hidden");
615                 });
616                 return false;
617         });
618         $(".unlock", soneElement).submit(function() {
619                 var unlockElement = this;
620                 $.getJSON("unlockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
621                         $(unlockElement).addClass("hidden");
622                         $(unlockElement).parent().find(".lock").removeClass("hidden");
623                 });
624                 return false;
625         });
626
627         /* mark Sone as known when clicking it. */
628         $(soneElement).click(function() {
629                 markSoneAsKnown(soneElement);
630         });
631 }
632
633 /**
634  * Ajaxifies the given post by enhancing all eligible elements with AJAX.
635  *
636  * @param postElement
637  *            The post element to ajaxify
638  */
639 function ajaxifyPost(postElement) {
640         $(postElement).find("form").submit(function() {
641                 return false;
642         });
643         $(postElement).find(".create-reply button:submit").click(function() {
644                 button = $(this);
645                 button.attr("disabled", "disabled");
646                 sender = $(this.form).find(":input[name=sender]").val();
647                 inputField = $(this.form).find(":input[name=text]:enabled").get(0);
648                 postId = getPostId(this);
649                 text = $(inputField).val();
650                 (function(sender, postId, text, inputField) {
651                         postReply(sender, postId, text, function(success, error, replyId, soneId) {
652                                 if (success) {
653                                         $(inputField).val("");
654                                         loadNewReply(replyId, soneId, postId);
655                                         $("#sone .post#" + postId + " .create-reply").addClass("hidden");
656                                         $("#sone .post#" + postId + " .create-reply .sender").hide();
657                                         $("#sone .post#" + postId + " .create-reply .select-sender").show();
658                                         $("#sone .post#" + postId + " .create-reply :input[name=sender]").val(getCurrentSoneId());
659                                 } else {
660                                         alert(error);
661                                 }
662                                 button.removeAttr("disabled");
663                         });
664                 })(sender, postId, text, inputField);
665                 return false;
666         });
667
668         /* replace all “delete” buttons with javascript. */
669         (function(postElement) {
670                 getTranslation("WebInterface.Confirmation.DeletePostButton", function(deletePostText) {
671                         postId = getPostId(postElement);
672                         enhanceDeletePostButton($(postElement).find(".delete-post button"), postId, deletePostText);
673                 });
674         })(postElement);
675
676         /* convert all “like” buttons to javascript functions. */
677         $(postElement).find(".like-post").submit(function() {
678                 likePost(getPostId(this));
679                 return false;
680         });
681         $(postElement).find(".unlike-post").submit(function() {
682                 unlikePost(getPostId(this));
683                 return false;
684         });
685
686         /* convert trust control buttons to javascript functions. */
687         $(postElement).find(".post-trust").submit(function() {
688                 trustSone(getPostAuthor(this));
689                 return false;
690         });
691         $(postElement).find(".post-distrust").submit(function() {
692                 distrustSone(getPostAuthor(this));
693                 return false;
694         });
695         $(postElement).find(".post-untrust").submit(function() {
696                 untrustSone(getPostAuthor(this));
697                 return false;
698         });
699
700         /* convert bookmark/unbookmark buttons to javascript functions. */
701         $(postElement).find(".bookmark").submit(function() {
702                 bookmarkPost(getPostId(this));
703                 return false;
704         });
705         $(postElement).find(".unbookmark").submit(function() {
706                 unbookmarkPost(getPostId(this));
707                 return false;
708         });
709
710         /* convert “show source” link into javascript function. */
711         $(postElement).find(".show-source").each(function() {
712                 $("a", this).click(function() {
713                         $(".post-text.text", getPostElement(this)).toggleClass("hidden");
714                         $(".post-text.raw-text", getPostElement(this)).toggleClass("hidden");
715                         return false;
716                 });
717         });
718
719         /* add “comment” link. */
720         addCommentLink(getPostId(postElement), postElement, $(postElement).find(".post-status-line .time"));
721
722         /* process all replies. */
723         $(postElement).find(".reply").each(function() {
724                 ajaxifyReply(this);
725         });
726
727         /* process reply input fields. */
728         getTranslation("WebInterface.DefaultText.Reply", function(text) {
729                 $(postElement).find("input.reply-input").each(function() {
730                         registerInputTextareaSwap(this, text, "text", false, false);
731                 });
732         });
733
734         /* process sender selection. */
735         $(".select-sender", postElement).css("display", "inline");
736         $(".sender", postElement).hide();
737         $(".select-sender button", postElement).click(function() {
738                 $(".sender", postElement).show();
739                 $(".select-sender", postElement).hide();
740                 return false;
741         });
742
743         /* mark everything as known on click. */
744         $(postElement).click(function(event) {
745                 if ($(event.target).hasClass("click-to-show")) {
746                         return false;
747                 }
748                 markPostAsKnown(this);
749         });
750
751         /* hide reply input field. */
752         $(postElement).find(".create-reply").addClass("hidden");
753 }
754
755 /**
756  * Ajaxifies the given reply element.
757  *
758  * @param replyElement
759  *            The reply element to ajaxify
760  */
761 function ajaxifyReply(replyElement) {
762         $(replyElement).find(".like-reply").submit(function() {
763                 likeReply(getReplyId(this));
764                 return false;
765         });
766         $(replyElement).find(".unlike-reply").submit(function() {
767                 unlikeReply(getReplyId(this));
768                 return false;
769         });
770         (function(replyElement) {
771                 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(deleteReplyText) {
772                         $(replyElement).find(".delete-reply button").each(function() {
773                                 enhanceDeleteReplyButton(this, getReplyId(replyElement), deleteReplyText);
774                         });
775                 });
776         })(replyElement);
777         addCommentLink(getPostId(replyElement), replyElement, $(replyElement).find(".reply-status-line .time"));
778
779         /* convert “show source” link into javascript function. */
780         $(replyElement).find(".show-reply-source").each(function() {
781                 $("a", this).click(function() {
782                         $(".reply-text.text", getReplyElement(this)).toggleClass("hidden");
783                         $(".reply-text.raw-text", getReplyElement(this)).toggleClass("hidden");
784                         return false;
785                 });
786         });
787
788         /* convert trust control buttons to javascript functions. */
789         $(replyElement).find(".reply-trust").submit(function() {
790                 trustSone(getReplyAuthor(this));
791                 return false;
792         });
793         $(replyElement).find(".reply-distrust").submit(function() {
794                 distrustSone(getReplyAuthor(this));
795                 return false;
796         });
797         $(replyElement).find(".reply-untrust").submit(function() {
798                 untrustSone(getReplyAuthor(this));
799                 return false;
800         });
801 }
802
803 /**
804  * Ajaxifies the given notification by replacing the form with AJAX.
805  *
806  * @param notification
807  *            jQuery object representing the notification.
808  */
809 function ajaxifyNotification(notification) {
810         notification.find("form").submit(function() {
811                 return false;
812         });
813         notification.find("input[name=returnPage]").val($.url.attr("relative"));
814         if (notification.find(".short-text").length > 0) {
815                 notification.find(".short-text").removeClass("hidden");
816                 notification.find(".text").addClass("hidden");
817         }
818         notification.find("form.mark-as-read button").click(function() {
819                 $.getJSON("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": $(":input[name=type]", this.form).val(), "id": $(":input[name=id]", this.form).val()});
820         });
821         notification.find("a[class^='link-']").each(function() {
822                 linkElement = $(this);
823                 if (linkElement.is("[href^='viewPost']")) {
824                         id = linkElement.attr("class").substr(5);
825                         if (hasPost(id)) {
826                                 linkElement.attr("href", "#post-" + id);
827                         }
828                 }
829         });
830         notification.find("form.dismiss button").click(function() {
831                 $.getJSON("dismissNotification.ajax", { "formPassword" : getFormPassword(), "notification" : notification.attr("id") }, function(data, textStatus) {
832                         /* dismiss in case of error, too. */
833                         notification.slideUp();
834                 }, function(xmlHttpRequest, textStatus, error) {
835                         /* ignore error. */
836                 });
837         });
838         return notification;
839 }
840
841 function getStatus() {
842         $.getJSON("getStatus.ajax", {"loadAllSones": isKnownSonesPage()}, function(data, textStatus) {
843                 if ((data != null) && data.success) {
844                         /* process Sone information. */
845                         $.each(data.sones, function(index, value) {
846                                 updateSoneStatus(value.id, value.name, value.status, value.modified, value.locked, value.lastUpdatedUnknown ? null : value.lastUpdated);
847                         });
848                         /* process notifications. */
849                         $.each(data.notifications, function(index, value) {
850                                 oldNotification = $("#sone #notification-area .notification#" + value.id);
851                                 notification = ajaxifyNotification(createNotification(value.id, value.text, value.dismissable)).hide();
852                                 if (oldNotification.length != 0) {
853                                         if ((oldNotification.find(".short-text").length > 0) && (notification.find(".short-text").length > 0)) {
854                                                 opened = oldNotification.is(":visible") && oldNotification.find(".short-text").hasClass("hidden");
855                                                 notification.find(".short-text").toggleClass("hidden", opened);
856                                                 notification.find(".text").toggleClass("hidden", !opened);
857                                         }
858                                         oldNotification.replaceWith(notification.show());
859                                 } else {
860                                         $("#sone #notification-area").append(notification);
861                                         notification.slideDown();
862                                 }
863                                 setActivity();
864                         });
865                         $.each(data.removedNotifications, function(index, value) {
866                                 $("#sone #notification-area .notification#" + value.id).slideUp();
867                         });
868                         /* process new posts. */
869                         $.each(data.newPosts, function(index, value) {
870                                 loadNewPost(value.id, value.sone, value.recipient, value.time);
871                         });
872                         /* process new replies. */
873                         $.each(data.newReplies, function(index, value) {
874                                 loadNewReply(value.id, value.sone, value.post, value.postSone);
875                         });
876                         /* do it again in 5 seconds. */
877                         setTimeout(getStatus, 5000);
878                 } else {
879                         /* data.success was false, wait 30 seconds. */
880                         setTimeout(getStatus, 30000);
881                 }
882         }, function(xmlHttpRequest, textStatus, error) {
883                 /* something really bad happend, wait a minute. */
884                 setTimeout(getStatus, 60000);
885         })
886 }
887
888 /**
889  * Returns the ID of the currently logged in Sone.
890  *
891  * @return The ID of the current Sone, or an empty string if no Sone is logged
892  *         in
893  */
894 function getCurrentSoneId() {
895         return $("#currentSoneId").text();
896 }
897
898 /**
899  * Returns the content of the page-id attribute.
900  *
901  * @returns The page ID
902  */
903 function getPageId() {
904         return $("#sone .page-id").text();
905 }
906
907 /**
908  * Returns whether the current page is the index page.
909  *
910  * @returns {Boolean} <code>true</code> if the current page is the index page,
911  *          <code>false</code> otherwise
912  */
913 function isIndexPage() {
914         return getPageId() == "index";
915 }
916
917 /**
918  * Returns whether the current page is a “view Sone” page.
919  *
920  * @returns {Boolean} <code>true</code> if the current page is a “view Sone”
921  *          page, <code>false</code> otherwise
922  */
923 function isViewSonePage() {
924         return getPageId() == "view-sone";
925 }
926
927 /**
928  * Returns the ID of the currently shown Sone. This will only return a sensible
929  * value if isViewSonePage() returns <code>true</code>.
930  *
931  * @returns The ID of the currently shown Sone
932  */
933 function getShownSoneId() {
934         return $("#sone .sone-id").text();
935 }
936
937 /**
938  * Returns whether the current page is a “view post” page.
939  *
940  * @returns {Boolean} <code>true</code> if the current page is a “view post”
941  *          page, <code>false</code> otherwise
942  */
943 function isViewPostPage() {
944         return getPageId() == "view-post";
945 }
946
947 /**
948  * Returns the ID of the currently shown post. This will only return a sensible
949  * value if isViewPostPage() returns <code>true</code>.
950  *
951  * @returns The ID of the currently shown post
952  */
953 function getShownPostId() {
954         return $("#sone .post-id").text();
955 }
956
957 /**
958  * Returns whether the current page is the “known Sones” page.
959  *
960  * @returns {Boolean} <code>true</code> if the current page is the “known
961  *          Sones” page, <code>false</code> otherwise
962  */
963 function isKnownSonesPage() {
964         return getPageId() == "known-sones";
965 }
966
967 /**
968  * Returns whether a post with the given ID exists on the current page.
969  *
970  * @param postId
971  *            The post ID to check for
972  * @returns {Boolean} <code>true</code> if a post with the given ID already
973  *          exists on the page, <code>false</code> otherwise
974  */
975 function hasPost(postId) {
976         return $(".post#" + postId).length > 0;
977 }
978
979 /**
980  * Returns whether a reply with the given ID exists on the current page.
981  *
982  * @param replyId
983  *            The reply ID to check for
984  * @returns {Boolean} <code>true</code> if a reply with the given ID already
985  *          exists on the page, <code>false</code> otherwise
986  */
987 function hasReply(replyId) {
988         return $("#sone .reply#" + replyId).length > 0;
989 }
990
991 function loadNewPost(postId, soneId, recipientId, time) {
992         if (hasPost(postId)) {
993                 return;
994         }
995         if (!isIndexPage()) {
996                 if (!isViewPostPage() || (getShownPostId() != postId)) {
997                         if (!isViewSonePage() || ((getShownSoneId() != soneId) && (getShownSoneId() != recipientId))) {
998                                 return;
999                         }
1000                 }
1001         }
1002         if (getPostTime($("#sone .post").last()) > time) {
1003                 return;
1004         }
1005         $.getJSON("getPost.ajax", { "post" : postId }, function(data, textStatus) {
1006                 if ((data != null) && data.success) {
1007                         if (hasPost(data.post.id)) {
1008                                 return;
1009                         }
1010                         if (!isIndexPage() && !(isViewSonePage() && ((getShownSoneId() == data.post.sone) || (getShownSoneId() == data.post.recipient)))) {
1011                                 return;
1012                         }
1013                         var firstOlderPost = null;
1014                         $("#sone .post").each(function() {
1015                                 if (getPostTime(this) < data.post.time) {
1016                                         firstOlderPost = $(this);
1017                                         return false;
1018                                 }
1019                         });
1020                         newPost = $(data.post.html).addClass("hidden");
1021                         if (firstOlderPost != null) {
1022                                 newPost.insertBefore(firstOlderPost);
1023                         }
1024                         ajaxifyPost(newPost);
1025                         newPost.slideDown();
1026                         setActivity();
1027                 }
1028         });
1029 }
1030
1031 function loadNewReply(replyId, soneId, postId, postSoneId) {
1032         if (hasReply(replyId)) {
1033                 return;
1034         }
1035         if (!hasPost(postId)) {
1036                 return;
1037         }
1038         $.getJSON("getReply.ajax", { "reply": replyId }, function(data, textStatus) {
1039                 /* find post. */
1040                 if ((data != null) && data.success) {
1041                         if (hasReply(data.reply.id)) {
1042                                 return;
1043                         }
1044                         $("#sone .post#" + data.reply.postId).each(function() {
1045                                 var firstNewerReply = null;
1046                                 $(this).find(".replies .reply").each(function() {
1047                                         if (getReplyTime(this) > data.reply.time) {
1048                                                 firstNewerReply = $(this);
1049                                                 return false;
1050                                         }
1051                                 });
1052                                 newReply = $(data.reply.html).addClass("hidden");
1053                                 if (firstNewerReply != null) {
1054                                         newReply.insertBefore(firstNewerReply);
1055                                 } else {
1056                                         if ($(this).find(".replies .create-reply")) {
1057                                                 $(this).find(".replies .create-reply").before(newReply);
1058                                         } else {
1059                                                 $(this).find(".replies").append(newReply);
1060                                         }
1061                                 }
1062                                 ajaxifyReply(newReply);
1063                                 newReply.slideDown();
1064                                 setActivity();
1065                                 return false;
1066                         });
1067                 }
1068         });
1069 }
1070
1071 /**
1072  * Marks the given Sone as known if it is still new.
1073  *
1074  * @param soneElement
1075  *            The Sone to mark as known
1076  */
1077 function markSoneAsKnown(soneElement) {
1078         if ($(".new", soneElement).length > 0) {
1079                 $.getJSON("maskAsKnown.ajax", {"formPassword": getFormPassword(), "type": "sone", "id": getSoneId(soneElement)}, function(data, textStatus) {
1080                         $(soneElement).removeClass("new");
1081                 });
1082         }
1083 }
1084
1085 function markPostAsKnown(postElements) {
1086         $(postElements).each(function() {
1087                 postElement = this;
1088                 if ($(postElement).hasClass("new")) {
1089                         (function(postElement) {
1090                                 $(postElement).removeClass("new");
1091                                 $(".click-to-show", postElement).removeClass("new");
1092                                 $.getJSON("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "post", "id": getPostId(postElement)});
1093                         })(postElement);
1094                 }
1095         });
1096         markReplyAsKnown($(postElements).find(".reply"));
1097 }
1098
1099 function markReplyAsKnown(replyElements) {
1100         $(replyElements).each(function() {
1101                 replyElement = this;
1102                 if ($(replyElement).hasClass("new")) {
1103                         (function(replyElement) {
1104                                 $(replyElement).removeClass("new");
1105                                 $.getJSON("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "reply", "id": getReplyId(replyElement)});
1106                         })(replyElement);
1107                 }
1108         });
1109 }
1110
1111 function resetActivity() {
1112         title = document.title;
1113         if (title.indexOf('(') == 0) {
1114                 setTitle(title.substr(title.indexOf(' ') + 1));
1115         }
1116 }
1117
1118 function setActivity() {
1119         if (!focus) {
1120                 title = document.title;
1121                 if (title.indexOf('(') != 0) {
1122                         setTitle("(!) " + title);
1123                 }
1124                 if (!iconBlinking) {
1125                         setTimeout(toggleIcon, 1500);
1126                         iconBlinking = true;
1127                 }
1128         }
1129 }
1130
1131 /**
1132  * Sets the window title after a small delay to prevent race-condition issues.
1133  *
1134  * @param title
1135  *            The title to set
1136  */
1137 function setTitle(title) {
1138         setTimeout(function() {
1139                 document.title = title;
1140         }, 50);
1141 }
1142
1143 /** Whether the icon is currently showing activity. */
1144 var iconActive = false;
1145
1146 /** Whether the icon is currently supposed to blink. */
1147 var iconBlinking = false;
1148
1149 /**
1150  * Toggles the icon. If the window has gained focus and the icon is still
1151  * showing the activity state, it is returned to normal.
1152  */
1153 function toggleIcon() {
1154         if (focus) {
1155                 if (iconActive) {
1156                         changeIcon("images/icon.png");
1157                         iconActive = false;
1158                 }
1159                 iconBlinking = false;
1160         } else {
1161                 iconActive = !iconActive;
1162                 changeIcon(iconActive ? "images/icon-activity.png" : "images/icon.png");
1163                 setTimeout(toggleIcon, 1500);
1164         }
1165 }
1166
1167 /**
1168  * Changes the icon of the page.
1169  *
1170  * @param iconUrl
1171  *            The new URL of the icon
1172  */
1173 function changeIcon(iconUrl) {
1174         $("link[rel=icon]").remove();
1175         $("head").append($("<link>").attr("rel", "icon").attr("type", "image/png").attr("href", iconUrl));
1176         $("iframe[id=icon-update]")[0].src += "";
1177 }
1178
1179 /**
1180  * Creates a new notification.
1181  *
1182  * @param id
1183  *            The ID of the notificaiton
1184  * @param text
1185  *            The text of the notification
1186  * @param dismissable
1187  *            <code>true</code> if the notification can be dismissed by the
1188  *            user
1189  */
1190 function createNotification(id, text, dismissable) {
1191         notification = $("<div></div>").addClass("notification").attr("id", id);
1192         if (dismissable) {
1193                 dismissForm = $("#sone #notification-area #notification-dismiss-template").clone().removeClass("hidden").removeAttr("id")
1194                 dismissForm.find("input[name=notification]").val(id);
1195                 notification.append(dismissForm);
1196         }
1197         notification.append(text);
1198         return notification;
1199 }
1200
1201 /**
1202  * Shows the details of the notification with the given ID.
1203  *
1204  * @param notificationId
1205  *            The ID of the notification
1206  */
1207 function showNotificationDetails(notificationId) {
1208         $("#sone .notification#" + notificationId + " .text").removeClass("hidden");
1209         $("#sone .notification#" + notificationId + " .short-text").addClass("hidden");
1210 }
1211
1212 /**
1213  * Deletes the field with the given ID from the profile.
1214  *
1215  * @param fieldId
1216  *            The ID of the field to delete
1217  */
1218 function deleteProfileField(fieldId) {
1219         $.getJSON("deleteProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId}, function(data, textStatus) {
1220                 if (data && data.success) {
1221                         $("#sone .profile-field#" + data.field.id).slideUp();
1222                 }
1223         });
1224 }
1225
1226 /**
1227  * Renames a profile field.
1228  *
1229  * @param fieldId
1230  *            The ID of the field to rename
1231  * @param newName
1232  *            The new name of the field
1233  * @param successFunction
1234  *            Called when the renaming was successful
1235  */
1236 function editProfileField(fieldId, newName, successFunction) {
1237         $.getJSON("editProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId, "name": newName}, function(data, textStatus) {
1238                 if (data && data.success) {
1239                         successFunction();
1240                 }
1241         });
1242 }
1243
1244 /**
1245  * Moves the profile field with the given ID one slot in the given direction.
1246  *
1247  * @param fieldId
1248  *            The ID of the field to move
1249  * @param direction
1250  *            The direction to move in (“up” or “down”)
1251  * @param successFunction
1252  *            Function to call on success
1253  */
1254 function moveProfileField(fieldId, direction, successFunction) {
1255         $.getJSON("moveProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId, "direction": direction}, function(data, textStatus) {
1256                 if (data && data.success) {
1257                         successFunction();
1258                 }
1259         });
1260 }
1261
1262 /**
1263  * Moves the profile field with the given ID up one slot.
1264  *
1265  * @param fieldId
1266  *            The ID of the field to move
1267  * @param successFunction
1268  *            Function to call on success
1269  */
1270 function moveProfileFieldUp(fieldId, successFunction) {
1271         moveProfileField(fieldId, "up", successFunction);
1272 }
1273
1274 /**
1275  * Moves the profile field with the given ID down one slot.
1276  *
1277  * @param fieldId
1278  *            The ID of the field to move
1279  * @param successFunction
1280  *            Function to call on success
1281  */
1282 function moveProfileFieldDown(fieldId, successFunction) {
1283         moveProfileField(fieldId, "down", successFunction);
1284 }
1285
1286 //
1287 // EVERYTHING BELOW HERE IS EXECUTED AFTER LOADING THE PAGE
1288 //
1289
1290 var focus = true;
1291
1292 $(document).ready(function() {
1293
1294         /* this initializes the status update input field. */
1295         getTranslation("WebInterface.DefaultText.StatusUpdate", function(defaultText) {
1296                 registerInputTextareaSwap("#sone #update-status .status-input", defaultText, "text", false, false);
1297                 $("#sone #update-status .select-sender").css("display", "inline");
1298                 $("#sone #update-status .sender").hide();
1299                 $("#sone #update-status .select-sender button").click(function() {
1300                         $("#sone #update-status .sender").show();
1301                         $("#sone #update-status .select-sender").hide();
1302                         return false;
1303                 });
1304                 $("#sone #update-status").submit(function() {
1305                         button = $("button:submit", this);
1306                         button.attr("disabled", "disabled");
1307                         if ($(this).find(":input.default:enabled").length > 0) {
1308                                 return false;
1309                         }
1310                         sender = $(this).find(":input[name=sender]").val();
1311                         text = $(this).find(":input[name=text]:enabled").val();
1312                         $.getJSON("createPost.ajax", { "formPassword": getFormPassword(), "sender": sender, "text": text }, function(data, textStatus) {
1313                                 if ((data != null) && data.success) {
1314                                         loadNewPost(data.postId, data.sone, data.recipient);
1315                                 }
1316                                 button.removeAttr("disabled");
1317                         });
1318                         $(this).find(":input[name=sender]").val(getCurrentSoneId());
1319                         $(this).find(":input[name=text]:enabled").val("").blur();
1320                         $(this).find(".sender").hide();
1321                         $(this).find(".select-sender").show();
1322                         return false;
1323                 });
1324         });
1325
1326         /* ajaxify the search input field. */
1327         getTranslation("WebInterface.DefaultText.Search", function(defaultText) {
1328                 registerInputTextareaSwap("#sone #search input[name=query]", defaultText, "query", false, true);
1329         });
1330
1331         /* ajaxify input field on “view Sone” page. */
1332         getTranslation("WebInterface.DefaultText.Message", function(defaultText) {
1333                 registerInputTextareaSwap("#sone #post-message input[name=text]", defaultText, "text", false, false);
1334                 $("#sone #post-message .select-sender").css("display", "inline");
1335                 $("#sone #post-message .sender").hide();
1336                 $("#sone #post-message .select-sender button").click(function() {
1337                         $("#sone #post-message .sender").show();
1338                         $("#sone #post-message .select-sender").hide();
1339                         return false;
1340                 });
1341                 $("#sone #post-message").submit(function() {
1342                         sender = $(this).find(":input[name=sender]").val();
1343                         text = $(this).find(":input[name=text]:enabled").val();
1344                         $.getJSON("createPost.ajax", { "formPassword": getFormPassword(), "recipient": getShownSoneId(), "sender": sender, "text": text }, function(data, textStatus) {
1345                                 if ((data != null) && data.success) {
1346                                         loadNewPost(data.postId, getCurrentSoneId());
1347                                 }
1348                         });
1349                         $(this).find(":input[name=sender]").val(getCurrentSoneId());
1350                         $(this).find(":input[name=text]:enabled").val("").blur();
1351                         $(this).find(".sender").hide();
1352                         $(this).find(".select-sender").show();
1353                         return false;
1354                 });
1355         });
1356
1357         /* Ajaxifies all posts. */
1358         /* calling getTranslation here will cache the necessary values. */
1359         getTranslation("WebInterface.Confirmation.DeletePostButton", function(text) {
1360                 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(text) {
1361                         getTranslation("WebInterface.DefaultText.Reply", function(text) {
1362                                 $("#sone .post").each(function() {
1363                                         ajaxifyPost(this);
1364                                 });
1365                         });
1366                 });
1367         });
1368
1369         /* hides all replies but the latest two. */
1370         if (!isViewPostPage()) {
1371                 getTranslation("WebInterface.ClickToShow.Replies", function(text) {
1372                         $("#sone .post .replies").each(function() {
1373                                 allReplies = $(this).find(".reply");
1374                                 if (allReplies.length > 2) {
1375                                         newHidden = false;
1376                                         for (replyIndex = 0; replyIndex < (allReplies.length - 2); ++replyIndex) {
1377                                                 $(allReplies[replyIndex]).addClass("hidden");
1378                                                 newHidden |= $(allReplies[replyIndex]).hasClass("new");
1379                                         }
1380                                         clickToShowElement = $("<div></div>").addClass("click-to-show");
1381                                         if (newHidden) {
1382                                                 clickToShowElement.addClass("new");
1383                                         }
1384                                         (function(clickToShowElement, allReplies, text) {
1385                                                 clickToShowElement.text(text);
1386                                                 clickToShowElement.click(function() {
1387                                                         allReplies.removeClass("hidden");
1388                                                         clickToShowElement.addClass("hidden");
1389                                                 });
1390                                         })(clickToShowElement, allReplies, text);
1391                                         $(allReplies[0]).before(clickToShowElement);
1392                                 }
1393                         });
1394                 });
1395         }
1396
1397         $("#sone .sone").each(function() {
1398                 ajaxifySone($(this));
1399         });
1400
1401         /* process all existing notifications, ajaxify dismiss buttons. */
1402         $("#sone #notification-area .notification").each(function() {
1403                 ajaxifyNotification($(this));
1404         });
1405
1406         /* activate status polling. */
1407         setTimeout(getStatus, 5000);
1408
1409         /* reset activity counter when the page has focus. */
1410         $(window).focus(function() {
1411                 focus = true;
1412                 resetActivity();
1413         }).blur(function() {
1414                 focus = false;
1415         })
1416
1417 });