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