Remove unnecessary call to addCommentLink().
[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                                 if (!optional && (textarea.val() == "")) {
51                                         return false;
52                                 }
53                         });
54                 })($(this), textarea);
55         });
56 }
57
58 /**
59  * Adds a “comment” link to all status lines contained in the given element.
60  *
61  * @param postId
62  *            The ID of the post
63  * @param element
64  *            The element to add a “comment” link to
65  */
66 function addCommentLink(postId, element) {
67         if ($(element).find(".show-reply-form").length > 0) {
68                 return;
69         }
70         commentElement = (function(postId) {
71                 var commentElement = $("<div><span>Comment</span></div>").addClass("show-reply-form").click(function() {
72                         markPostAsKnown(getPostElement(this));
73                         replyElement = $("#sone .post#" + postId + " .create-reply");
74                         replyElement.removeClass("hidden");
75                         replyElement.removeClass("light");
76                         (function(replyElement) {
77                                 replyElement.find("input.reply-input").blur(function() {
78                                         if ($(this).hasClass("default")) {
79                                                 replyElement.addClass("light");
80                                         }
81                                 }).focus(function() {
82                                         replyElement.removeClass("light");
83                                 });
84                         })(replyElement);
85                         replyElement.find("input.reply-input").focus();
86                 });
87                 return commentElement;
88         })(postId);
89         $(element).find(".status-line .time").each(function() {
90                 $(this).after(commentElement.clone(true));
91         });
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("ajax/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         $("#sone .sone." + filterSoneId(soneId) + " .last-update span.time").text(lastUpdated);
156         $("#sone .sone." + filterSoneId(soneId) + " .profile-link a").text(name);
157 }
158
159 /**
160  * Enhances a “delete” button so that the confirmation is done on the same page.
161  *
162  * @param button
163  *            The button element
164  * @param text
165  *            The text to show on the button
166  * @param deleteCallback
167  *            The callback that actually deletes something
168  */
169 function enhanceDeleteButton(button, text, deleteCallback) {
170         (function(button) {
171                 newButton = $("<button></button>").addClass("confirm").hide().text(text).click(function() {
172                         $(this).fadeOut("slow");
173                         deleteCallback();
174                         return false;
175                 }).insertAfter(button);
176                 (function(button, newButton) {
177                         button.click(function() {
178                                 button.fadeOut("slow", function() {
179                                         newButton.fadeIn("slow");
180                                         $(document).one("click", function() {
181                                                 if (this != newButton.get(0)) {
182                                                         newButton.fadeOut(function() {
183                                                                 button.fadeIn();
184                                                         });
185                                                 }
186                                         });
187                                 });
188                                 return false;
189                         });
190                 })(button, newButton);
191         })($(button));
192 }
193
194 /**
195  * Enhances a post’s “delete” button.
196  *
197  * @param button
198  *            The button element
199  * @param postId
200  *            The ID of the post to delete
201  * @param text
202  *            The text to replace the button with
203  */
204 function enhanceDeletePostButton(button, postId, text) {
205         enhanceDeleteButton(button, text, function() {
206                 $.getJSON("ajax/deletePost.ajax", { "post": postId, "formPassword": getFormPassword() }, function(data, textStatus) {
207                         if (data == null) {
208                                 return;
209                         }
210                         if (data.success) {
211                                 $("#sone .post#" + postId).slideUp();
212                         } else if (data.error == "invalid-post-id") {
213                                 alert("Invalid post ID given!");
214                         } else if (data.error == "auth-required") {
215                                 alert("You need to be logged in.");
216                         } else if (data.error == "not-authorized") {
217                                 alert("You are not allowed to delete this post.");
218                         }
219                 }, function(xmlHttpRequest, textStatus, error) {
220                         /* ignore error. */
221                 });
222         });
223 }
224
225 /**
226  * Enhances a reply’s “delete” button.
227  *
228  * @param button
229  *            The button element
230  * @param replyId
231  *            The ID of the reply to delete
232  * @param text
233  *            The text to replace the button with
234  */
235 function enhanceDeleteReplyButton(button, replyId, text) {
236         enhanceDeleteButton(button, text, function() {
237                 $.getJSON("ajax/deleteReply.ajax", { "reply": replyId, "formPassword": $("#sone #formPassword").text() }, function(data, textStatus) {
238                         if (data == null) {
239                                 return;
240                         }
241                         if (data.success) {
242                                 $("#sone .reply#" + replyId).slideUp();
243                         } else if (data.error == "invalid-reply-id") {
244                                 alert("Invalid reply ID given!");
245                         } else if (data.error == "auth-required") {
246                                 alert("You need to be logged in.");
247                         } else if (data.error == "not-authorized") {
248                                 alert("You are not allowed to delete this reply.");
249                         }
250                 }, function(xmlHttpRequest, textStatus, error) {
251                         /* ignore error. */
252                 });
253         });
254 }
255
256 function getFormPassword() {
257         return $("#sone #formPassword").text();
258 }
259
260 function getSoneElement(element) {
261         return $(element).parents(".sone");
262 }
263
264 /**
265  * Generates a list of Sones by concatening the names of the given sones with a
266  * new line character (“\n”).
267  *
268  * @param sones
269  *            The sones to format
270  * @returns {String} The created string
271  */
272 function generateSoneList(sones) {
273         var soneList = "";
274         $.each(sones, function() {
275                 if (soneList != "") {
276                         soneList += ", ";
277                 }
278                 soneList += this.name;
279         });
280         return soneList;
281 }
282
283 /**
284  * Returns the ID of the Sone that this element belongs to.
285  *
286  * @param element
287  *            The element to locate the matching Sone ID for
288  * @returns The ID of the Sone, or undefined
289  */
290 function getSoneId(element) {
291         return getSoneElement(element).find(".id").text();
292 }
293
294 function getPostElement(element) {
295         return $(element).closest(".post");
296 }
297
298 function getPostId(element) {
299         return getPostElement(element).attr("id");
300 }
301
302 function getPostTime(element) {
303         return getPostElement(element).find(".post-time").text();
304 }
305
306 function getReplyElement(element) {
307         return $(element).closest(".reply");
308 }
309
310 function getReplyId(element) {
311         return getReplyElement(element).attr("id");
312 }
313
314 function getReplyTime(element) {
315         return getReplyElement(element).find(".reply-time").text();
316 }
317
318 function likePost(postId) {
319         $.getJSON("ajax/like.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
320                 if ((data == null) || !data.success) {
321                         return;
322                 }
323                 $("#sone .post#" + postId + " > .inner-part > .status-line .like").addClass("hidden");
324                 $("#sone .post#" + postId + " > .inner-part > .status-line .unlike").removeClass("hidden");
325                 updatePostLikes(postId);
326         }, function(xmlHttpRequest, textStatus, error) {
327                 /* ignore error. */
328         });
329 }
330
331 function unlikePost(postId) {
332         $.getJSON("ajax/unlike.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
333                 if ((data == null) || !data.success) {
334                         return;
335                 }
336                 $("#sone .post#" + postId + " > .inner-part > .status-line .unlike").addClass("hidden");
337                 $("#sone .post#" + postId + " > .inner-part > .status-line .like").removeClass("hidden");
338                 updatePostLikes(postId);
339         }, function(xmlHttpRequest, textStatus, error) {
340                 /* ignore error. */
341         });
342 }
343
344 function updatePostLikes(postId) {
345         $.getJSON("ajax/getLikes.ajax", { "type": "post", "post": postId }, function(data, textStatus) {
346                 if ((data != null) && data.success) {
347                         $("#sone .post#" + postId + " > .inner-part > .status-line .likes").toggleClass("hidden", data.likes == 0)
348                         $("#sone .post#" + postId + " > .inner-part > .status-line .likes span.like-count").text(data.likes);
349                         $("#sone .post#" + postId + " > .inner-part > .status-line .likes > span").attr("title", generateSoneList(data.sones));
350                 }
351         }, function(xmlHttpRequest, textStatus, error) {
352                 /* ignore error. */
353         });
354 }
355
356 function likeReply(replyId) {
357         $.getJSON("ajax/like.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
358                 if ((data == null) || !data.success) {
359                         return;
360                 }
361                 $("#sone .reply#" + replyId + " .status-line .like").addClass("hidden");
362                 $("#sone .reply#" + replyId + " .status-line .unlike").removeClass("hidden");
363                 updateReplyLikes(replyId);
364         }, function(xmlHttpRequest, textStatus, error) {
365                 /* ignore error. */
366         });
367 }
368
369 function unlikeReply(replyId) {
370         $.getJSON("ajax/unlike.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
371                 if ((data == null) || !data.success) {
372                         return;
373                 }
374                 $("#sone .reply#" + replyId + " .status-line .unlike").addClass("hidden");
375                 $("#sone .reply#" + replyId + " .status-line .like").removeClass("hidden");
376                 updateReplyLikes(replyId);
377         }, function(xmlHttpRequest, textStatus, error) {
378                 /* ignore error. */
379         });
380 }
381
382 function updateReplyLikes(replyId) {
383         $.getJSON("ajax/getLikes.ajax", { "type": "reply", "reply": replyId }, function(data, textStatus) {
384                 if ((data != null) && data.success) {
385                         $("#sone .reply#" + replyId + " .status-line .likes").toggleClass("hidden", data.likes == 0)
386                         $("#sone .reply#" + replyId + " .status-line .likes span.like-count").text(data.likes);
387                         $("#sone .reply#" + replyId + " .status-line .likes > span").attr("title", generateSoneList(data.sones));
388                 }
389         }, function(xmlHttpRequest, textStatus, error) {
390                 /* ignore error. */
391         });
392 }
393
394 /**
395  * Posts a reply and calls the given callback when the request finishes.
396  *
397  * @param postId
398  *            The ID of the post the reply refers to
399  * @param text
400  *            The text to post
401  * @param callbackFunction
402  *            The callback function to call when the request finishes (takes 3
403  *            parameters: success, error, replyId)
404  */
405 function postReply(postId, text, callbackFunction) {
406         $.getJSON("ajax/createReply.ajax", { "formPassword" : getFormPassword(), "post" : postId, "text": text }, function(data, textStatus) {
407                 if (data == null) {
408                         /* TODO - show error */
409                         return;
410                 }
411                 if (data.success) {
412                         callbackFunction(true, null, data.reply);
413                 } else {
414                         callbackFunction(false, data.error);
415                 }
416         }, function(xmlHttpRequest, textStatus, error) {
417                 /* ignore error. */
418         });
419 }
420
421 /**
422  * Requests information about the reply with the given ID.
423  *
424  * @param replyId
425  *            The ID of the reply
426  * @param callbackFunction
427  *            A callback function (parameters soneId, soneName, replyTime,
428  *            replyDisplayTime, text, html)
429  */
430 function getReply(replyId, callbackFunction) {
431         $.getJSON("ajax/getReply.ajax", { "reply" : replyId }, function(data, textStatus) {
432                 if ((data != null) && data.success) {
433                         callbackFunction(data.soneId, data.soneName, data.time, data.displayTime, data.text, data.html);
434                 }
435         }, function(xmlHttpRequest, textStatus, error) {
436                 /* ignore error. */
437         });
438 }
439
440 /**
441  * Ajaxifies the given post by enhancing all eligible elements with AJAX.
442  *
443  * @param postElement
444  *            The post element to ajaxify
445  */
446 function ajaxifyPost(postElement) {
447         $(postElement).find("form").submit(function() {
448                 return false;
449         });
450         $(postElement).find(".create-reply button:submit").click(function() {
451                 inputField = $(this.form).find(":input:enabled").get(0);
452                 postId = getPostId(this);
453                 text = $(inputField).val();
454                 $(inputField).val("");
455                 postReply(postId, text, function(success, error, replyId) {
456                         if (success) {
457                                 loadNewReply(replyId);
458                                 markPostAsKnown(getPostElement(inputField));
459                                 $("#sone .post#" + postId + " .create-reply").addClass("hidden");
460                         } else {
461                                 alert(error);
462                         }
463                 });
464                 return false;
465         });
466
467         /* replace all “delete” buttons with javascript. */
468         (function(postElement) {
469                 getTranslation("WebInterface.Confirmation.DeletePostButton", function(deletePostText) {
470                         postId = getPostId(postElement);
471                         enhanceDeletePostButton($(postElement).find(".delete-post button"), postId, deletePostText);
472                 });
473         })(postElement);
474
475         /* convert all “like” buttons to javascript functions. */
476         $(postElement).find(".like-post").submit(function() {
477                 likePost(getPostId(this));
478                 markPostAsKnown(getPostElement(this));
479                 return false;
480         });
481         $(postElement).find(".unlike-post").submit(function() {
482                 unlikePost(getPostId(this));
483                 markPostAsKnown(getPostElement(this));
484                 return false;
485         });
486
487         /* process all replies. */
488         $(postElement).find(".reply").each(function() {
489                 ajaxifyReply(this);
490         });
491
492         /* process reply input fields. */
493         getTranslation("WebInterface.DefaultText.Reply", function(text) {
494                 $(postElement).find("input.reply-input").each(function() {
495                         registerInputTextareaSwap(this, text, "text", false, false);
496                 });
497         });
498
499         /* add “comment” link. */
500         addCommentLink(getPostId(postElement), postElement);
501
502         /* hide reply input field. */
503         $(postElement).find(".create-reply").addClass("hidden");
504 }
505
506 /**
507  * Ajaxifies the given reply element.
508  *
509  * @param replyElement
510  *            The reply element to ajaxify
511  */
512 function ajaxifyReply(replyElement) {
513         $(replyElement).find(".like-reply").submit(function() {
514                 likeReply(getReplyId(this));
515                 markPostAsKnown(getPostElement(this));
516                 return false;
517         });
518         $(replyElement).find(".unlike-reply").submit(function() {
519                 unlikeReply(getReplyId(this));
520                 markPostAsKnown(getPostElement(this));
521                 return false;
522         });
523         (function(replyElement) {
524                 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(deleteReplyText) {
525                         $(replyElement).find(".delete-reply button").each(function() {
526                                 enhanceDeleteReplyButton(this, getReplyId(replyElement), deleteReplyText);
527                         });
528                 });
529         })(replyElement);
530         addCommentLink(getPostId(replyElement), replyElement);
531 }
532
533 /**
534  * Ajaxifies the given notification by replacing the form with AJAX.
535  *
536  * @param notification
537  *            jQuery object representing the notification.
538  */
539 function ajaxifyNotification(notification) {
540         notification.find("form.dismiss").submit(function() {
541                 return false;
542         });
543         notification.find("form.dismiss button").click(function() {
544                 $.getJSON("ajax/dismissNotification.ajax", { "formPassword" : getFormPassword(), "notification" : notification.attr("id") }, function(data, textStatus) {
545                         /* dismiss in case of error, too. */
546                         notification.slideUp();
547                 }, function(xmlHttpRequest, textStatus, error) {
548                         /* ignore error. */
549                 });
550         });
551         return notification;
552 }
553
554 function getStatus() {
555         $.getJSON("ajax/getStatus.ajax", {}, function(data, textStatus) {
556                 if ((data != null) && data.success) {
557                         /* process Sone information. */
558                         $.each(data.sones, function(index, value) {
559                                 updateSoneStatus(value.id, value.name, value.status, value.modified, value.locked, value.lastUpdated);
560                         });
561                         /* process notifications. */
562                         $.each(data.notifications, function(index, value) {
563                                 oldNotification = $("#sone #notification-area .notification#" + value.id);
564                                 notification = ajaxifyNotification(createNotification(value.id, value.text, value.dismissable)).hide();
565                                 if (oldNotification.length != 0) {
566                                         oldNotification.replaceWith(notification.show());
567                                 } else {
568                                         $("#sone #notification-area").append(notification);
569                                         notification.slideDown();
570                                 }
571                                 setActivity();
572                         });
573                         $.each(data.removedNotifications, function(index, value) {
574                                 $("#sone #notification-area .notification#" + value.id).slideUp();
575                         });
576                         /* process new posts. */
577                         $.each(data.newPosts, function(index, value) {
578                                 loadNewPost(value);
579                         });
580                         /* process new replies. */
581                         $.each(data.newReplies, function(index, value) {
582                                 loadNewReply(value);
583                         });
584                         /* do it again in 5 seconds. */
585                         setTimeout(getStatus, 5000);
586                 } else {
587                         /* data.success was false, wait 30 seconds. */
588                         setTimeout(getStatus, 30000);
589                 }
590         }, function(xmlHttpRequest, textStatus, error) {
591                 /* something really bad happend, wait a minute. */
592                 setTimeout(getStatus, 60000);
593         })
594 }
595
596 var loadedPosts = {};
597 var loadedReplies = {};
598
599 function loadNewPost(postId) {
600         if (postId in loadedPosts) {
601                 return;
602         }
603         loadedPosts[postId] = true;
604         $.getJSON("ajax/getPost.ajax", { "post" : postId }, function(data, textStatus) {
605                 if ((data != null) && data.success) {
606                         var firstOlderPost = null;
607                         $("#sone .post").each(function() {
608                                 if (getPostTime(this) < data.post.time) {
609                                         firstOlderPost = $(this);
610                                         return false;
611                                 }
612                         });
613                         newPost = $(data.post.html).addClass("hidden");
614                         if (firstOlderPost != null) {
615                                 newPost.insertBefore(firstOlderPost);
616                         } else {
617                                 $("#sone #posts").append(newPost);
618                         }
619                         ajaxifyPost(newPost);
620                         newPost.slideDown();
621                         setActivity();
622                 }
623         });
624 }
625
626 function loadNewReply(replyId) {
627         if (replyId in loadedReplies) {
628                 return;
629         }
630         loadedReplies[replyId] = true;
631         $.getJSON("ajax/getReply.ajax", { "reply": replyId }, function(data, textStatus) {
632                 /* find post. */
633                 if ((data != null) && data.success) {
634                         $("#sone .post#" + data.reply.postId).each(function() {
635                                 var firstNewerReply = null;
636                                 $(this).find(".replies .reply").each(function() {
637                                         if (getReplyTime(this) > data.reply.time) {
638                                                 firstNewerReply = $(this);
639                                                 return false;
640                                         }
641                                 });
642                                 newReply = $(data.reply.html).addClass("hidden");
643                                 if (firstNewerReply != null) {
644                                         newReply.insertBefore(firstNewerReply);
645                                 } else {
646                                         if ($(this).find(".replies .create-reply")) {
647                                                 $(this).find(".replies .create-reply").before(newReply);
648                                         } else {
649                                                 $(this).find(".replies").append(newReply);
650                                         }
651                                 }
652                                 ajaxifyReply(newReply);
653                                 newReply.slideDown();
654                                 setActivity();
655                         });
656                 }
657         });
658 }
659
660 function markPostAsKnown(postElements) {
661         $(postElements).each(function() {
662                 postElement = this;
663                 if ($(postElement).hasClass("new")) {
664                         (function(postElement) {
665                                 $.getJSON("ajax/markPostAsKnown.ajax", {"formPassword": getFormPassword(), "post": getPostId(postElement)}, function(data, textStatus) {
666                                         $(postElement).removeClass("new");
667                                 });
668                         })(postElement);
669                 }
670         });
671         markReplyAsKnown($(postElements).find(".reply"));
672 }
673
674 function markReplyAsKnown(replyElements) {
675         $(replyElements).each(function() {
676                 replyElement = this;
677                 if ($(replyElement).hasClass("new")) {
678                         (function(replyElement) {
679                                 $.getJSON("ajax/markReplyAsKnown.ajax", {"formPassword": getFormPassword(), "reply": getReplyId(replyElement)}, function(data, textStatus) {
680                                         $(replyElement).removeClass("new");
681                                 });
682                         })(replyElement);
683                 }
684         });
685 }
686
687 function resetActivity() {
688         title = document.title;
689         if (title.indexOf('(') == 0) {
690                 document.title = title.substr(title.indexOf(' ') + 1);
691         }
692 }
693
694 function setActivity() {
695         title = document.title;
696         if (title.indexOf('(') != 0) {
697                 document.title = "(!) " + title;
698         }
699 }
700
701 /**
702  * Creates a new notification.
703  *
704  * @param id
705  *            The ID of the notificaiton
706  * @param text
707  *            The text of the notification
708  * @param dismissable
709  *            <code>true</code> if the notification can be dismissed by the
710  *            user
711  */
712 function createNotification(id, text, dismissable) {
713         notification = $("<div></div>").addClass("notification").attr("id", id);
714         if (dismissable) {
715                 dismissForm = $("#sone #notification-area #notification-dismiss-template").clone().removeClass("hidden").removeAttr("id")
716                 dismissForm.find("input[name=notification]").val(id);
717                 notification.append(dismissForm);
718         }
719         notification.append(text);
720         return notification;
721 }
722
723 //
724 // EVERYTHING BELOW HERE IS EXECUTED AFTER LOADING THE PAGE
725 //
726
727 $(document).ready(function() {
728
729         /* this initializes the status update input field. */
730         getTranslation("WebInterface.DefaultText.StatusUpdate", function(defaultText) {
731                 registerInputTextareaSwap("#sone #update-status .status-input", defaultText, "text", false, false);
732                 $("#sone #update-status").submit(function() {
733                         text = $(this).find(":input:enabled").val();
734                         $.getJSON("ajax/createPost.ajax", { "formPassword": getFormPassword(), "text": text }, function(data, textStatus) {
735                                 if ((data != null) && data.success) {
736                                         loadNewPost(data.postId);
737                                 }
738                         });
739                         $(this).find(":input:enabled").val("").blur();
740                         return false;
741                 });
742         });
743
744         /* Ajaxifies all posts. */
745         /* calling getTranslation here will cache the necessary values. */
746         getTranslation("WebInterface.Confirmation.DeletePostButton", function(text) {
747                 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(text) {
748                         getTranslation("WebInterface.DefaultText.Reply", function(text) {
749                                 $("#sone .post").each(function() {
750                                         ajaxifyPost(this);
751                                 });
752                         });
753                 });
754         });
755
756         /* hides all replies but the latest two. */
757         getTranslation("WebInterface.ClickToShow.Replies", function(text) {
758                 $("#sone .post .replies").each(function() {
759                         allReplies = $(this).find(".reply");
760                         if (allReplies.length > 2) {
761                                 newHidden = false;
762                                 for (replyIndex = 0; replyIndex < (allReplies.length - 2); ++replyIndex) {
763                                         $(allReplies[replyIndex]).addClass("hidden");
764                                         newHidden |= $(allReplies[replyIndex]).hasClass("new");
765                                 }
766                                 clickToShowElement = $("<div></div>").addClass("click-to-show");
767                                 if (newHidden) {
768                                         clickToShowElement.addClass("new");
769                                 }
770                                 (function(clickToShowElement, allReplies, text) {
771                                         clickToShowElement.text(text);
772                                         clickToShowElement.click(function() {
773                                                 allReplies.removeClass("hidden");
774                                                 clickToShowElement.addClass("hidden");
775                                         });
776                                 })(clickToShowElement, allReplies, text);
777                                 $(allReplies[0]).before(clickToShowElement);
778                         }
779                 });
780         });
781
782         /*
783          * convert all “follow”, “unfollow”, “lock”, and “unlock” links to something
784          * nicer.
785          */
786         $("#sone .follow").submit(function() {
787                 var followElement = this;
788                 $.getJSON("ajax/followSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
789                         $(followElement).addClass("hidden");
790                         $(followElement).parent().find(".unfollow").removeClass("hidden");
791                 });
792                 return false;
793         });
794         $("#sone .unfollow").submit(function() {
795                 var unfollowElement = this;
796                 $.getJSON("ajax/unfollowSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
797                         $(unfollowElement).addClass("hidden");
798                         $(unfollowElement).parent().find(".follow").removeClass("hidden");
799                 });
800                 return false;
801         });
802         $("#sone .lock").submit(function() {
803                 var lockElement = this;
804                 $.getJSON("ajax/lockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
805                         $(lockElement).addClass("hidden");
806                         $(lockElement).parent().find(".unlock").removeClass("hidden");
807                 });
808                 return false;
809         });
810         $("#sone .unlock").submit(function() {
811                 var unlockElement = this;
812                 $.getJSON("ajax/unlockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
813                         $(unlockElement).addClass("hidden");
814                         $(unlockElement).parent().find(".lock").removeClass("hidden");
815                 });
816                 return false;
817         });
818
819         /* process all existing notifications, ajaxify dismiss buttons. */
820         $("#sone #notification-area .notification").each(function() {
821                 ajaxifyNotification($(this));
822         });
823
824         /* activate status polling. */
825         setTimeout(getStatus, 5000);
826
827         /* reset activity counter when the page has focus. */
828         $(window).focus(function() {
829                 resetActivity();
830         });
831
832 });