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