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