Change the way notifications are handled.
[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                         /* search for removed notifications. */
849                         $("#sone #notification-area .notification").each(function() {
850                                 notificationId = $(this).attr("id");
851                                 foundNotification = false;
852                                 $.each(data.notifications, function(index, value) {
853                                         if (value.id == notificationId) {
854                                                 foundNotification = true;
855                                                 return false;
856                                         }
857                                 });
858                                 if (!foundNotification) {
859                                         $(this).slideUp("normal", function() {
860                                                 $(this).remove();
861                                         });
862                                 }
863                         });
864                         /* process notifications. */
865                         $.each(data.notifications, function(index, value) {
866                                 oldNotification = $("#sone #notification-area .notification#" + value.id);
867                                 notification = ajaxifyNotification(createNotification(value.id, value.text, value.dismissable)).hide();
868                                 if (oldNotification.length != 0) {
869                                         if ((oldNotification.find(".short-text").length > 0) && (notification.find(".short-text").length > 0)) {
870                                                 opened = oldNotification.is(":visible") && oldNotification.find(".short-text").hasClass("hidden");
871                                                 notification.find(".short-text").toggleClass("hidden", opened);
872                                                 notification.find(".text").toggleClass("hidden", !opened);
873                                         }
874                                         oldNotification.replaceWith(notification.show());
875                                 } else {
876                                         $("#sone #notification-area").append(notification);
877                                         notification.slideDown();
878                                         setActivity();
879                                 }
880                         });
881                         /* process new posts. */
882                         $.each(data.newPosts, function(index, value) {
883                                 loadNewPost(value.id, value.sone, value.recipient, value.time);
884                         });
885                         /* process new replies. */
886                         $.each(data.newReplies, function(index, value) {
887                                 loadNewReply(value.id, value.sone, value.post, value.postSone);
888                         });
889                         /* do it again in 5 seconds. */
890                         setTimeout(getStatus, 5000);
891                 } else {
892                         /* data.success was false, wait 30 seconds. */
893                         setTimeout(getStatus, 30000);
894                 }
895         }, function(xmlHttpRequest, textStatus, error) {
896                 /* something really bad happend, wait a minute. */
897                 setTimeout(getStatus, 60000);
898         })
899 }
900
901 /**
902  * Returns the ID of the currently logged in Sone.
903  *
904  * @return The ID of the current Sone, or an empty string if no Sone is logged
905  *         in
906  */
907 function getCurrentSoneId() {
908         return $("#currentSoneId").text();
909 }
910
911 /**
912  * Returns the content of the page-id attribute.
913  *
914  * @returns The page ID
915  */
916 function getPageId() {
917         return $("#sone .page-id").text();
918 }
919
920 /**
921  * Returns whether the current page is the index page.
922  *
923  * @returns {Boolean} <code>true</code> if the current page is the index page,
924  *          <code>false</code> otherwise
925  */
926 function isIndexPage() {
927         return getPageId() == "index";
928 }
929
930 /**
931  * Returns whether the current page is a “view Sone” page.
932  *
933  * @returns {Boolean} <code>true</code> if the current page is a “view Sone”
934  *          page, <code>false</code> otherwise
935  */
936 function isViewSonePage() {
937         return getPageId() == "view-sone";
938 }
939
940 /**
941  * Returns the ID of the currently shown Sone. This will only return a sensible
942  * value if isViewSonePage() returns <code>true</code>.
943  *
944  * @returns The ID of the currently shown Sone
945  */
946 function getShownSoneId() {
947         return $("#sone .sone-id").text();
948 }
949
950 /**
951  * Returns whether the current page is a “view post” page.
952  *
953  * @returns {Boolean} <code>true</code> if the current page is a “view post”
954  *          page, <code>false</code> otherwise
955  */
956 function isViewPostPage() {
957         return getPageId() == "view-post";
958 }
959
960 /**
961  * Returns the ID of the currently shown post. This will only return a sensible
962  * value if isViewPostPage() returns <code>true</code>.
963  *
964  * @returns The ID of the currently shown post
965  */
966 function getShownPostId() {
967         return $("#sone .post-id").text();
968 }
969
970 /**
971  * Returns whether the current page is the “known Sones” page.
972  *
973  * @returns {Boolean} <code>true</code> if the current page is the “known
974  *          Sones” page, <code>false</code> otherwise
975  */
976 function isKnownSonesPage() {
977         return getPageId() == "known-sones";
978 }
979
980 /**
981  * Returns whether a post with the given ID exists on the current page.
982  *
983  * @param postId
984  *            The post ID to check for
985  * @returns {Boolean} <code>true</code> if a post with the given ID already
986  *          exists on the page, <code>false</code> otherwise
987  */
988 function hasPost(postId) {
989         return $(".post#" + postId).length > 0;
990 }
991
992 /**
993  * Returns whether a reply with the given ID exists on the current page.
994  *
995  * @param replyId
996  *            The reply ID to check for
997  * @returns {Boolean} <code>true</code> if a reply with the given ID already
998  *          exists on the page, <code>false</code> otherwise
999  */
1000 function hasReply(replyId) {
1001         return $("#sone .reply#" + replyId).length > 0;
1002 }
1003
1004 function loadNewPost(postId, soneId, recipientId, time) {
1005         if (hasPost(postId)) {
1006                 return;
1007         }
1008         if (!isIndexPage()) {
1009                 if (!isViewPostPage() || (getShownPostId() != postId)) {
1010                         if (!isViewSonePage() || ((getShownSoneId() != soneId) && (getShownSoneId() != recipientId))) {
1011                                 return;
1012                         }
1013                 }
1014         }
1015         if (getPostTime($("#sone .post").last()) > time) {
1016                 return;
1017         }
1018         $.getJSON("getPost.ajax", { "post" : postId }, function(data, textStatus) {
1019                 if ((data != null) && data.success) {
1020                         if (hasPost(data.post.id)) {
1021                                 return;
1022                         }
1023                         if (!isIndexPage() && !(isViewSonePage() && ((getShownSoneId() == data.post.sone) || (getShownSoneId() == data.post.recipient)))) {
1024                                 return;
1025                         }
1026                         var firstOlderPost = null;
1027                         $("#sone .post").each(function() {
1028                                 if (getPostTime(this) < data.post.time) {
1029                                         firstOlderPost = $(this);
1030                                         return false;
1031                                 }
1032                         });
1033                         newPost = $(data.post.html).addClass("hidden");
1034                         if (firstOlderPost != null) {
1035                                 newPost.insertBefore(firstOlderPost);
1036                         }
1037                         ajaxifyPost(newPost);
1038                         newPost.slideDown();
1039                         setActivity();
1040                 }
1041         });
1042 }
1043
1044 function loadNewReply(replyId, soneId, postId, postSoneId) {
1045         if (hasReply(replyId)) {
1046                 return;
1047         }
1048         if (!hasPost(postId)) {
1049                 return;
1050         }
1051         $.getJSON("getReply.ajax", { "reply": replyId }, function(data, textStatus) {
1052                 /* find post. */
1053                 if ((data != null) && data.success) {
1054                         if (hasReply(data.reply.id)) {
1055                                 return;
1056                         }
1057                         $("#sone .post#" + data.reply.postId).each(function() {
1058                                 var firstNewerReply = null;
1059                                 $(this).find(".replies .reply").each(function() {
1060                                         if (getReplyTime(this) > data.reply.time) {
1061                                                 firstNewerReply = $(this);
1062                                                 return false;
1063                                         }
1064                                 });
1065                                 newReply = $(data.reply.html).addClass("hidden");
1066                                 if (firstNewerReply != null) {
1067                                         newReply.insertBefore(firstNewerReply);
1068                                 } else {
1069                                         if ($(this).find(".replies .create-reply")) {
1070                                                 $(this).find(".replies .create-reply").before(newReply);
1071                                         } else {
1072                                                 $(this).find(".replies").append(newReply);
1073                                         }
1074                                 }
1075                                 ajaxifyReply(newReply);
1076                                 newReply.slideDown();
1077                                 setActivity();
1078                                 return false;
1079                         });
1080                 }
1081         });
1082 }
1083
1084 /**
1085  * Marks the given Sone as known if it is still new.
1086  *
1087  * @param soneElement
1088  *            The Sone to mark as known
1089  */
1090 function markSoneAsKnown(soneElement) {
1091         if ($(".new", soneElement).length > 0) {
1092                 $.getJSON("maskAsKnown.ajax", {"formPassword": getFormPassword(), "type": "sone", "id": getSoneId(soneElement)}, function(data, textStatus) {
1093                         $(soneElement).removeClass("new");
1094                 });
1095         }
1096 }
1097
1098 function markPostAsKnown(postElements) {
1099         $(postElements).each(function() {
1100                 postElement = this;
1101                 if ($(postElement).hasClass("new")) {
1102                         (function(postElement) {
1103                                 $(postElement).removeClass("new");
1104                                 $(".click-to-show", postElement).removeClass("new");
1105                                 $.getJSON("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "post", "id": getPostId(postElement)});
1106                         })(postElement);
1107                 }
1108         });
1109         markReplyAsKnown($(postElements).find(".reply"));
1110 }
1111
1112 function markReplyAsKnown(replyElements) {
1113         $(replyElements).each(function() {
1114                 replyElement = this;
1115                 if ($(replyElement).hasClass("new")) {
1116                         (function(replyElement) {
1117                                 $(replyElement).removeClass("new");
1118                                 $.getJSON("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "reply", "id": getReplyId(replyElement)});
1119                         })(replyElement);
1120                 }
1121         });
1122 }
1123
1124 function resetActivity() {
1125         title = document.title;
1126         if (title.indexOf('(') == 0) {
1127                 setTitle(title.substr(title.indexOf(' ') + 1));
1128         }
1129 }
1130
1131 function setActivity() {
1132         if (!focus) {
1133                 title = document.title;
1134                 if (title.indexOf('(') != 0) {
1135                         setTitle("(!) " + title);
1136                 }
1137                 if (!iconBlinking) {
1138                         setTimeout(toggleIcon, 1500);
1139                         iconBlinking = true;
1140                 }
1141         }
1142 }
1143
1144 /**
1145  * Sets the window title after a small delay to prevent race-condition issues.
1146  *
1147  * @param title
1148  *            The title to set
1149  */
1150 function setTitle(title) {
1151         setTimeout(function() {
1152                 document.title = title;
1153         }, 50);
1154 }
1155
1156 /** Whether the icon is currently showing activity. */
1157 var iconActive = false;
1158
1159 /** Whether the icon is currently supposed to blink. */
1160 var iconBlinking = false;
1161
1162 /**
1163  * Toggles the icon. If the window has gained focus and the icon is still
1164  * showing the activity state, it is returned to normal.
1165  */
1166 function toggleIcon() {
1167         if (focus) {
1168                 if (iconActive) {
1169                         changeIcon("images/icon.png");
1170                         iconActive = false;
1171                 }
1172                 iconBlinking = false;
1173         } else {
1174                 iconActive = !iconActive;
1175                 changeIcon(iconActive ? "images/icon-activity.png" : "images/icon.png");
1176                 setTimeout(toggleIcon, 1500);
1177         }
1178 }
1179
1180 /**
1181  * Changes the icon of the page.
1182  *
1183  * @param iconUrl
1184  *            The new URL of the icon
1185  */
1186 function changeIcon(iconUrl) {
1187         $("link[rel=icon]").remove();
1188         $("head").append($("<link>").attr("rel", "icon").attr("type", "image/png").attr("href", iconUrl));
1189         $("iframe[id=icon-update]")[0].src += "";
1190 }
1191
1192 /**
1193  * Creates a new notification.
1194  *
1195  * @param id
1196  *            The ID of the notificaiton
1197  * @param text
1198  *            The text of the notification
1199  * @param dismissable
1200  *            <code>true</code> if the notification can be dismissed by the
1201  *            user
1202  */
1203 function createNotification(id, text, dismissable) {
1204         notification = $("<div></div>").addClass("notification").attr("id", id);
1205         if (dismissable) {
1206                 dismissForm = $("#sone #notification-area #notification-dismiss-template").clone().removeClass("hidden").removeAttr("id")
1207                 dismissForm.find("input[name=notification]").val(id);
1208                 notification.append(dismissForm);
1209         }
1210         notification.append(text);
1211         return notification;
1212 }
1213
1214 /**
1215  * Shows the details of the notification with the given ID.
1216  *
1217  * @param notificationId
1218  *            The ID of the notification
1219  */
1220 function showNotificationDetails(notificationId) {
1221         $("#sone .notification#" + notificationId + " .text").removeClass("hidden");
1222         $("#sone .notification#" + notificationId + " .short-text").addClass("hidden");
1223 }
1224
1225 /**
1226  * Deletes the field with the given ID from the profile.
1227  *
1228  * @param fieldId
1229  *            The ID of the field to delete
1230  */
1231 function deleteProfileField(fieldId) {
1232         $.getJSON("deleteProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId}, function(data, textStatus) {
1233                 if (data && data.success) {
1234                         $("#sone .profile-field#" + data.field.id).slideUp();
1235                 }
1236         });
1237 }
1238
1239 /**
1240  * Renames a profile field.
1241  *
1242  * @param fieldId
1243  *            The ID of the field to rename
1244  * @param newName
1245  *            The new name of the field
1246  * @param successFunction
1247  *            Called when the renaming was successful
1248  */
1249 function editProfileField(fieldId, newName, successFunction) {
1250         $.getJSON("editProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId, "name": newName}, function(data, textStatus) {
1251                 if (data && data.success) {
1252                         successFunction();
1253                 }
1254         });
1255 }
1256
1257 /**
1258  * Moves the profile field with the given ID one slot in the given direction.
1259  *
1260  * @param fieldId
1261  *            The ID of the field to move
1262  * @param direction
1263  *            The direction to move in (“up” or “down”)
1264  * @param successFunction
1265  *            Function to call on success
1266  */
1267 function moveProfileField(fieldId, direction, successFunction) {
1268         $.getJSON("moveProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId, "direction": direction}, function(data, textStatus) {
1269                 if (data && data.success) {
1270                         successFunction();
1271                 }
1272         });
1273 }
1274
1275 /**
1276  * Moves the profile field with the given ID up one slot.
1277  *
1278  * @param fieldId
1279  *            The ID of the field to move
1280  * @param successFunction
1281  *            Function to call on success
1282  */
1283 function moveProfileFieldUp(fieldId, successFunction) {
1284         moveProfileField(fieldId, "up", successFunction);
1285 }
1286
1287 /**
1288  * Moves the profile field with the given ID down one slot.
1289  *
1290  * @param fieldId
1291  *            The ID of the field to move
1292  * @param successFunction
1293  *            Function to call on success
1294  */
1295 function moveProfileFieldDown(fieldId, successFunction) {
1296         moveProfileField(fieldId, "down", successFunction);
1297 }
1298
1299 //
1300 // EVERYTHING BELOW HERE IS EXECUTED AFTER LOADING THE PAGE
1301 //
1302
1303 var focus = true;
1304
1305 $(document).ready(function() {
1306
1307         /* this initializes the status update input field. */
1308         getTranslation("WebInterface.DefaultText.StatusUpdate", function(defaultText) {
1309                 registerInputTextareaSwap("#sone #update-status .status-input", defaultText, "text", false, false);
1310                 $("#sone #update-status .select-sender").css("display", "inline");
1311                 $("#sone #update-status .sender").hide();
1312                 $("#sone #update-status .select-sender button").click(function() {
1313                         $("#sone #update-status .sender").show();
1314                         $("#sone #update-status .select-sender").hide();
1315                         return false;
1316                 });
1317                 $("#sone #update-status").submit(function() {
1318                         button = $("button:submit", this);
1319                         button.attr("disabled", "disabled");
1320                         if ($(this).find(":input.default:enabled").length > 0) {
1321                                 return false;
1322                         }
1323                         sender = $(this).find(":input[name=sender]").val();
1324                         text = $(this).find(":input[name=text]:enabled").val();
1325                         $.getJSON("createPost.ajax", { "formPassword": getFormPassword(), "sender": sender, "text": text }, function(data, textStatus) {
1326                                 if ((data != null) && data.success) {
1327                                         loadNewPost(data.postId, data.sone, data.recipient);
1328                                 }
1329                                 button.removeAttr("disabled");
1330                         });
1331                         $(this).find(":input[name=sender]").val(getCurrentSoneId());
1332                         $(this).find(":input[name=text]:enabled").val("").blur();
1333                         $(this).find(".sender").hide();
1334                         $(this).find(".select-sender").show();
1335                         return false;
1336                 });
1337         });
1338
1339         /* ajaxify the search input field. */
1340         getTranslation("WebInterface.DefaultText.Search", function(defaultText) {
1341                 registerInputTextareaSwap("#sone #search input[name=query]", defaultText, "query", false, true);
1342         });
1343
1344         /* ajaxify input field on “view Sone” page. */
1345         getTranslation("WebInterface.DefaultText.Message", function(defaultText) {
1346                 registerInputTextareaSwap("#sone #post-message input[name=text]", defaultText, "text", false, false);
1347                 $("#sone #post-message .select-sender").css("display", "inline");
1348                 $("#sone #post-message .sender").hide();
1349                 $("#sone #post-message .select-sender button").click(function() {
1350                         $("#sone #post-message .sender").show();
1351                         $("#sone #post-message .select-sender").hide();
1352                         return false;
1353                 });
1354                 $("#sone #post-message").submit(function() {
1355                         sender = $(this).find(":input[name=sender]").val();
1356                         text = $(this).find(":input[name=text]:enabled").val();
1357                         $.getJSON("createPost.ajax", { "formPassword": getFormPassword(), "recipient": getShownSoneId(), "sender": sender, "text": text }, function(data, textStatus) {
1358                                 if ((data != null) && data.success) {
1359                                         loadNewPost(data.postId, getCurrentSoneId());
1360                                 }
1361                         });
1362                         $(this).find(":input[name=sender]").val(getCurrentSoneId());
1363                         $(this).find(":input[name=text]:enabled").val("").blur();
1364                         $(this).find(".sender").hide();
1365                         $(this).find(".select-sender").show();
1366                         return false;
1367                 });
1368         });
1369
1370         /* Ajaxifies all posts. */
1371         /* calling getTranslation here will cache the necessary values. */
1372         getTranslation("WebInterface.Confirmation.DeletePostButton", function(text) {
1373                 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(text) {
1374                         getTranslation("WebInterface.DefaultText.Reply", function(text) {
1375                                 $("#sone .post").each(function() {
1376                                         ajaxifyPost(this);
1377                                 });
1378                         });
1379                 });
1380         });
1381
1382         /* hides all replies but the latest two. */
1383         if (!isViewPostPage()) {
1384                 getTranslation("WebInterface.ClickToShow.Replies", function(text) {
1385                         $("#sone .post .replies").each(function() {
1386                                 allReplies = $(this).find(".reply");
1387                                 if (allReplies.length > 2) {
1388                                         newHidden = false;
1389                                         for (replyIndex = 0; replyIndex < (allReplies.length - 2); ++replyIndex) {
1390                                                 $(allReplies[replyIndex]).addClass("hidden");
1391                                                 newHidden |= $(allReplies[replyIndex]).hasClass("new");
1392                                         }
1393                                         clickToShowElement = $("<div></div>").addClass("click-to-show");
1394                                         if (newHidden) {
1395                                                 clickToShowElement.addClass("new");
1396                                         }
1397                                         (function(clickToShowElement, allReplies, text) {
1398                                                 clickToShowElement.text(text);
1399                                                 clickToShowElement.click(function() {
1400                                                         allReplies.removeClass("hidden");
1401                                                         clickToShowElement.addClass("hidden");
1402                                                 });
1403                                         })(clickToShowElement, allReplies, text);
1404                                         $(allReplies[0]).before(clickToShowElement);
1405                                 }
1406                         });
1407                 });
1408         }
1409
1410         $("#sone .sone").each(function() {
1411                 ajaxifySone($(this));
1412         });
1413
1414         /* process all existing notifications, ajaxify dismiss buttons. */
1415         $("#sone #notification-area .notification").each(function() {
1416                 ajaxifyNotification($(this));
1417         });
1418
1419         /* activate status polling. */
1420         setTimeout(getStatus, 5000);
1421
1422         /* reset activity counter when the page has focus. */
1423         $(window).focus(function() {
1424                 focus = true;
1425                 resetActivity();
1426         }).blur(function() {
1427                 focus = false;
1428         })
1429
1430 });