3339d18eab3be08d2bbe4a2a14a2121049406f7b
[Sone.git] / src / main / resources / static / javascript / sone.js
1 /* Sone JavaScript functions. */
2
3 function ajaxGet(url, data, successCallback, errorCallback) {
4         (function(url, data, successCallback, errorCallback) {
5                 $.ajax({"type": "GET", "url": url, "data": data, "dataType": "json", "success": function(data, textStatus, xmlHttpRequest) {
6                         ajaxSuccess();
7                         if (typeof successCallback != "undefined") {
8                                 successCallback(data, textStatus);
9                         }
10                 }, "error": function(xmlHttpRequest, textStatus, errorThrown) {
11                         if (xmlHttpRequest.status == 403) {
12                                 notLoggedIn = true;
13                         }
14                         if (typeof errorCallback != "undefined") {
15                                 errorCallback();
16                         } else {
17                                 ajaxError();
18                         }
19                 }});
20         })(url, data, successCallback, errorCallback);
21 }
22
23 function registerInputTextareaSwap(inputElement, defaultText, inputFieldName, optional, dontUseTextarea) {
24         $(inputElement).each(function() {
25                 textarea = $(dontUseTextarea ? "<input type=\"text\" name=\"" + inputFieldName + "\">" : "<textarea name=\"" + inputFieldName + "\"></textarea>").blur(function() {
26                         if ($(this).val() == "") {
27                                 $(this).hide();
28                                 inputField = $(this).data("inputField");
29                                 inputField.show().removeAttr("disabled").addClass("default");
30                                 inputField.val(defaultText);
31                         }
32                 }).hide().data("inputField", $(this)).val($(this).val());
33                 $(this).data("textarea", textarea).after(textarea);
34                 (function(inputField, textarea) {
35                         inputField.focus(function() {
36                                 $(this).hide().attr("disabled", "disabled");
37                                 /* no, show(), “display: block” is not what I need. */
38                                 textarea.attr("style", "display: inline").focus();
39                         });
40                         if (inputField.val() == "") {
41                                 inputField.addClass("default");
42                                 inputField.val(defaultText);
43                         } else {
44                                 inputField.hide().attr("disabled", "disabled");
45                                 textarea.show();
46                         }
47                         $(inputField.get(0).form).submit(function() {
48                                 inputField.attr("disabled", "disabled");
49                                 if (!optional && (textarea.val() == "")) {
50                                         inputField.removeAttr("disabled").focus();
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, author, element, insertAfterThisElement) {
67         if (($(element).find(".show-reply-form").length > 0) || (getPostElement(element).find(".create-reply").length == 0)) {
68                 return;
69         }
70         (function(postId, author, insertAfterThisElement) {
71                 separator = $("<span> · </span>").addClass("separator");
72                 getTranslation("WebInterface.Button.Comment", function(text) {
73                         commentElement = $("<div><span>" + text + "</span></div>").addClass("show-reply-form").click(function() {
74                                 replyElement = $("#sone .post#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                                 textArea = replyElement.find("input.reply-input").focus().data("textarea");
87                                 if (author != getCurrentSoneId()) {
88                                         textArea.val(textArea.val() + "@sone://" + author + " ");
89                                 }
90                         });
91                         $(insertAfterThisElement).after(commentElement.clone(true));
92                         $(insertAfterThisElement).after(separator);
93                 });
94         })(postId, author, insertAfterThisElement);
95 }
96
97 var translations = {};
98
99 /**
100  * Retrieves the translation for the given key and calls the callback function.
101  * The callback function takes a single parameter, the translated string.
102  *
103  * @param key
104  *            The key of the translation string
105  * @param callback
106  *            The callback function
107  */
108 function getTranslation(key, callback) {
109         if (key in translations) {
110                 callback(translations[key]);
111                 return;
112         }
113         ajaxGet("getTranslation.ajax", {"key": key}, function(data, textStatus) {
114                 if ((data != null) && data.success) {
115                         translations[key] = data.value;
116                         callback(data.value);
117                 }
118         });
119 }
120
121 /**
122  * Filters the given Sone ID, replacing all “~” characters by an underscore.
123  *
124  * @param soneId
125  *            The Sone ID to filter
126  * @returns The filtered Sone ID
127  */
128 function filterSoneId(soneId) {
129         return soneId.replace(/[^a-zA-Z0-9-]/g, "_");
130 }
131
132 /**
133  * Updates the status of the given Sone.
134  *
135  * @param soneId
136  *            The ID of the Sone to update
137  * @param status
138  *            The status of the Sone (“idle”, “unknown”, “inserting”,
139  *            “downloading”)
140  * @param modified
141  *            Whether the Sone is modified
142  * @param locked
143  *            Whether the Sone is locked
144  * @param lastUpdated
145  *            The date and time of the last update (formatted for display)
146  */
147 function updateSoneStatus(soneId, name, status, modified, locked, lastUpdated, lastUpdatedText) {
148         $("#sone .sone." + filterSoneId(soneId)).
149                 toggleClass("unknown", status == "unknown").
150                 toggleClass("idle", status == "idle").
151                 toggleClass("inserting", status == "inserting").
152                 toggleClass("downloading", status == "downloading").
153                 toggleClass("modified", modified);
154         $("#sone .sone." + filterSoneId(soneId) + " .lock").toggleClass("hidden", locked);
155         $("#sone .sone." + filterSoneId(soneId) + " .unlock").toggleClass("hidden", !locked);
156         if (lastUpdated != null) {
157                 $("#sone .sone." + filterSoneId(soneId) + " .last-update span.time").attr("title", lastUpdated).text(lastUpdatedText);
158         } else {
159                 getTranslation("View.Sone.Text.UnknownDate", function(unknown) {
160                         $("#sone .sone." + filterSoneId(soneId) + " .last-update span.time").text(unknown);
161                 });
162         }
163         $("#sone .sone." + filterSoneId(soneId) + " .profile-link a").text(name);
164 }
165
166 /**
167  * Enhances a “delete” button so that the confirmation is done on the same page.
168  *
169  * @param button
170  *            The button element
171  * @param text
172  *            The text to show on the button
173  * @param deleteCallback
174  *            The callback that actually deletes something
175  */
176 function enhanceDeleteButton(button, text, deleteCallback) {
177         (function(button) {
178                 newButton = $("<button></button>").addClass("confirm").hide().text(text).click(function() {
179                         $(this).fadeOut("slow");
180                         deleteCallback();
181                         return false;
182                 }).insertAfter(button);
183                 (function(button, newButton) {
184                         button.click(function() {
185                                 button.fadeOut("slow", function() {
186                                         newButton.fadeIn("slow");
187                                         $(document).one("click", function() {
188                                                 if (this != newButton.get(0)) {
189                                                         newButton.fadeOut(function() {
190                                                                 button.fadeIn();
191                                                         });
192                                                 }
193                                         });
194                                 });
195                                 return false;
196                         });
197                 })(button, newButton);
198         })($(button));
199 }
200
201 /**
202  * Enhances a post’s “delete” button.
203  *
204  * @param button
205  *            The button element
206  * @param postId
207  *            The ID of the post to delete
208  * @param text
209  *            The text to replace the button with
210  */
211 function enhanceDeletePostButton(button, postId, text) {
212         enhanceDeleteButton(button, text, function() {
213                 ajaxGet("deletePost.ajax", { "post": postId, "formPassword": getFormPassword() }, function(data, textStatus) {
214                         if (data == null) {
215                                 return;
216                         }
217                         if (data.success) {
218                                 $("#sone .post#post-" + postId).slideUp();
219                         } else if (data.error == "invalid-post-id") {
220                                 /* pretend the post is already gone. */
221                                 getPost(postId).slideUp();
222                         } else if (data.error == "auth-required") {
223                                 alert("You need to be logged in.");
224                         } else if (data.error == "not-authorized") {
225                                 alert("You are not allowed to delete this post.");
226                         }
227                 }, function(xmlHttpRequest, textStatus, error) {
228                         /* ignore error. */
229                 });
230         });
231 }
232
233 /**
234  * Enhances a reply’s “delete” button.
235  *
236  * @param button
237  *            The button element
238  * @param replyId
239  *            The ID of the reply to delete
240  * @param text
241  *            The text to replace the button with
242  */
243 function enhanceDeleteReplyButton(button, replyId, text) {
244         enhanceDeleteButton(button, text, function() {
245                 ajaxGet("deleteReply.ajax", { "reply": replyId, "formPassword": $("#sone #formPassword").text() }, function(data, textStatus) {
246                         if (data == null) {
247                                 return;
248                         }
249                         if (data.success) {
250                                 $("#sone .reply#reply-" + replyId).slideUp();
251                         } else if (data.error == "invalid-reply-id") {
252                                 /* pretend the reply is already gone. */
253                                 getReply(replyId).slideUp();
254                         } else if (data.error == "auth-required") {
255                                 alert("You need to be logged in.");
256                         } else if (data.error == "not-authorized") {
257                                 alert("You are not allowed to delete this reply.");
258                         }
259                 }, function(xmlHttpRequest, textStatus, error) {
260                         /* ignore error. */
261                 });
262         });
263 }
264
265 function getFormPassword() {
266         return $("#sone #formPassword").text();
267 }
268
269 /**
270  * Returns the element of the Sone with the given ID.
271  *
272  * @param soneId
273  *            The ID of the Sone
274  * @returns All Sone elements with the given ID
275  */
276 function getSone(soneId) {
277         return $("#sone .sone").filter(function(index) {
278                 return $(".id", this).text() == soneId;
279         });
280 }
281
282 function getSoneElement(element) {
283         return $(element).closest(".sone");
284 }
285
286 /**
287  * Returns the ID of the sone of the context menu that contains the given
288  * element.
289  *
290  * @param element
291  *            The element within a context menu to get the Sone ID for
292  * @return The Sone ID
293  */
294 function getMenuSone(element) {
295         return $(element).closest(".sone-menu").find(".sone-menu-id").text();
296 }
297
298 /**
299  * Generates a list of Sones by concatening the names of the given sones with a
300  * new line character (“\n”).
301  *
302  * @param sones
303  *            The sones to format
304  * @returns {String} The created string
305  */
306 function generateSoneList(sones) {
307         var soneList = "";
308         $.each(sones, function() {
309                 if (soneList != "") {
310                         soneList += ", ";
311                 }
312                 soneList += this.name;
313         });
314         return soneList;
315 }
316
317 /**
318  * Returns the ID of the Sone that this element belongs to.
319  *
320  * @param element
321  *            The element to locate the matching Sone ID for
322  * @returns The ID of the Sone, or undefined
323  */
324 function getSoneId(element) {
325         return getSoneElement(element).find(".id").text();
326 }
327
328 /**
329  * Returns the element of the post with the given ID.
330  *
331  * @param postId
332  *            The ID of the post
333  * @returns The element of the post
334  */
335 function getPost(postId) {
336         return $("#sone .post#post-" + postId);
337 }
338
339 function getPostElement(element) {
340         return $(element).closest(".post");
341 }
342
343 function getPostId(element) {
344         return getPostElement(element).attr("id").substr(5);
345 }
346
347 function getPostTime(element) {
348         return getPostElement(element).find(".post-time").text();
349 }
350
351 /**
352  * Returns the author of the post the given element belongs to.
353  *
354  * @param element
355  *            The element whose post to get the author for
356  * @returns The ID of the authoring Sone
357  */
358 function getPostAuthor(element) {
359         return getPostElement(element).find(".post-author").text();
360 }
361
362 /**
363  * Returns the element of the reply with the given ID.
364  *
365  * @param replyId
366  *            The ID of the reply
367  * @returns The element of the reply
368  */
369 function getReply(replyId) {
370         return $("#sone .reply#reply-" + replyId);
371 }
372
373 function getReplyElement(element) {
374         return $(element).closest(".reply");
375 }
376
377 function getReplyId(element) {
378         return getReplyElement(element).attr("id").substr(6);
379 }
380
381 function getReplyTime(element) {
382         return getReplyElement(element).find(".reply-time").text();
383 }
384
385 /**
386  * Returns the author of the reply the given element belongs to.
387  *
388  * @param element
389  *            The element whose reply to get the author for
390  * @returns The ID of the authoring Sone
391  */
392 function getReplyAuthor(element) {
393         return getReplyElement(element).find(".reply-author").text();
394 }
395
396 /**
397  * Returns the notification with the given ID.
398  *
399  * @param notificationId
400  *            The ID of the notification
401  * @returns The notification element
402  */
403 function getNotification(notificationId) {
404         return $("#sone #notification-area .notification#" + notificationId);
405 }
406
407 /**
408  * Returns the notification element closest to the given element.
409  *
410  * @param element
411  *            The element to get the closest notification of
412  * @return The closest notification element
413  */
414 function getNotificationElement(element) {
415         return $(element).closest(".notification");
416 }
417
418 /**
419  * Returns the ID of the notification element.
420  *
421  * @param notificationElement
422  *            The notification element
423  * @returns The ID of the notification
424  */
425 function getNotificationId(notificationElement) {
426         return $(notificationElement).attr("id");
427 }
428
429 /**
430  * Returns the time the notification was last updated.
431  *
432  * @param notificationElement
433  *            The notification element
434  * @returns The last update time of the notification
435  */
436 function getNotificationLastUpdatedTime(notificationElement) {
437         return $(notificationElement).attr("lastUpdatedTime");
438 }
439
440 function likePost(postId) {
441         ajaxGet("like.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
442                 if ((data == null) || !data.success) {
443                         return;
444                 }
445                 $("#sone .post#post-" + postId + " > .inner-part > .status-line .like").addClass("hidden");
446                 $("#sone .post#post-" + postId + " > .inner-part > .status-line .unlike").removeClass("hidden");
447                 updatePostLikes(postId);
448         }, function(xmlHttpRequest, textStatus, error) {
449                 /* ignore error. */
450         });
451 }
452
453 function unlikePost(postId) {
454         ajaxGet("unlike.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
455                 if ((data == null) || !data.success) {
456                         return;
457                 }
458                 $("#sone .post#post-" + postId + " > .inner-part > .status-line .unlike").addClass("hidden");
459                 $("#sone .post#post-" + postId + " > .inner-part > .status-line .like").removeClass("hidden");
460                 updatePostLikes(postId);
461         }, function(xmlHttpRequest, textStatus, error) {
462                 /* ignore error. */
463         });
464 }
465
466 function updatePostLikes(postId) {
467         ajaxGet("getLikes.ajax", { "type": "post", "post": postId }, function(data, textStatus) {
468                 if ((data != null) && data.success) {
469                         $("#sone .post#post-" + postId + " > .inner-part > .status-line .likes").toggleClass("hidden", data.likes == 0);
470                         $("#sone .post#post-" + postId + " > .inner-part > .status-line .likes span.like-count").text(data.likes);
471                         $("#sone .post#post-" + postId + " > .inner-part > .status-line .likes > span").attr("title", generateSoneList(data.sones));
472                 }
473         }, function(xmlHttpRequest, textStatus, error) {
474                 /* ignore error. */
475         });
476 }
477
478 function likeReply(replyId) {
479         ajaxGet("like.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
480                 if ((data == null) || !data.success) {
481                         return;
482                 }
483                 $("#sone .reply#reply-" + replyId + " .status-line .like").addClass("hidden");
484                 $("#sone .reply#reply-" + replyId + " .status-line .unlike").removeClass("hidden");
485                 updateReplyLikes(replyId);
486         }, function(xmlHttpRequest, textStatus, error) {
487                 /* ignore error. */
488         });
489 }
490
491 function unlikeReply(replyId) {
492         ajaxGet("unlike.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
493                 if ((data == null) || !data.success) {
494                         return;
495                 }
496                 $("#sone .reply#reply-" + replyId + " .status-line .unlike").addClass("hidden");
497                 $("#sone .reply#reply-" + replyId + " .status-line .like").removeClass("hidden");
498                 updateReplyLikes(replyId);
499         }, function(xmlHttpRequest, textStatus, error) {
500                 /* ignore error. */
501         });
502 }
503
504 /**
505  * Trusts the Sone with the given ID.
506  *
507  * @param soneId
508  *            The ID of the Sone to trust
509  */
510 function trustSone(soneId) {
511         ajaxGet("trustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
512                 if ((data != null) && data.success) {
513                         updateTrustControls(soneId, data.trustValue);
514                 }
515         });
516 }
517
518 /**
519  * Distrusts the Sone with the given ID, i.e. assigns a negative trust value.
520  *
521  * @param soneId
522  *            The ID of the Sone to distrust
523  */
524 function distrustSone(soneId) {
525         ajaxGet("distrustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
526                 if ((data != null) && data.success) {
527                         updateTrustControls(soneId, data.trustValue);
528                 }
529         });
530 }
531
532 /**
533  * Untrusts the Sone with the given ID, i.e. removes any trust assignment.
534  *
535  * @param soneId
536  *            The ID of the Sone to untrust
537  */
538 function untrustSone(soneId) {
539         ajaxGet("untrustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
540                 if ((data != null) && data.success) {
541                         updateTrustControls(soneId, data.trustValue);
542                 }
543         });
544 }
545
546 /**
547  * Updates the trust controls for all posts and replies of the given Sone,
548  * according to the given trust value.
549  *
550  * @param soneId
551  *            The ID of the Sone to update all trust controls for
552  * @param trustValue
553  *            The trust value for the Sone
554  */
555 function updateTrustControls(soneId, trustValue) {
556         $("#sone .post").each(function() {
557                 if (getPostAuthor(this) == soneId) {
558                         getPostElement(this).find(".post-trust").toggleClass("hidden", trustValue != null);
559                         getPostElement(this).find(".post-distrust").toggleClass("hidden", trustValue != null);
560                         getPostElement(this).find(".post-untrust").toggleClass("hidden", trustValue == null);
561                 }
562         });
563         $("#sone .reply").each(function() {
564                 if (getReplyAuthor(this) == soneId) {
565                         getReplyElement(this).find(".reply-trust").toggleClass("hidden", trustValue != null);
566                         getReplyElement(this).find(".reply-distrust").toggleClass("hidden", trustValue != null);
567                         getReplyElement(this).find(".reply-untrust").toggleClass("hidden", trustValue == null);
568                 }
569         });
570 }
571
572 /**
573  * Bookmarks the post with the given ID.
574  *
575  * @param postId
576  *            The ID of the post to bookmark
577  */
578 function bookmarkPost(postId) {
579         (function(postId) {
580                 ajaxGet("bookmark.ajax", {"formPassword": getFormPassword(), "type": "post", "post": postId}, function(data, textStatus) {
581                         if ((data != null) && data.success) {
582                                 getPost(postId).find(".bookmark").toggleClass("hidden", true);
583                                 getPost(postId).find(".unbookmark").toggleClass("hidden", false);
584                         }
585                 });
586         })(postId);
587 }
588
589 /**
590  * Unbookmarks the post with the given ID.
591  *
592  * @param postId
593  *            The ID of the post to unbookmark
594  */
595 function unbookmarkPost(postId) {
596         ajaxGet("unbookmark.ajax", {"formPassword": getFormPassword(), "type": "post", "post": postId}, function(data, textStatus) {
597                 if ((data != null) && data.success) {
598                         getPost(postId).find(".bookmark").toggleClass("hidden", false);
599                         getPost(postId).find(".unbookmark").toggleClass("hidden", true);
600                 }
601         });
602 }
603
604 function updateReplyLikes(replyId) {
605         ajaxGet("getLikes.ajax", { "type": "reply", "reply": replyId }, function(data, textStatus) {
606                 if ((data != null) && data.success) {
607                         $("#sone .reply#reply-" + replyId + " .status-line .likes").toggleClass("hidden", data.likes == 0);
608                         $("#sone .reply#reply-" + replyId + " .status-line .likes span.like-count").text(data.likes);
609                         $("#sone .reply#reply-" + replyId + " .status-line .likes > span").attr("title", generateSoneList(data.sones));
610                 }
611         }, function(xmlHttpRequest, textStatus, error) {
612                 /* ignore error. */
613         });
614 }
615
616 /**
617  * Posts a reply and calls the given callback when the request finishes.
618  *
619  * @param sender
620  *            The ID of the sender
621  * @param postId
622  *            The ID of the post the reply refers to
623  * @param text
624  *            The text to post
625  * @param callbackFunction
626  *            The callback function to call when the request finishes (takes 3
627  *            parameters: success, error, replyId)
628  */
629 function postReply(sender, postId, text, callbackFunction) {
630         ajaxGet("createReply.ajax", { "formPassword" : getFormPassword(), "sender": sender, "post" : postId, "text": text }, function(data, textStatus) {
631                 if (data == null) {
632                         /* TODO - show error */
633                         return;
634                 }
635                 if (data.success) {
636                         callbackFunction(true, null, data.reply, data.sone);
637                 } else {
638                         callbackFunction(false, data.error);
639                 }
640         }, function(xmlHttpRequest, textStatus, error) {
641                 /* ignore error. */
642         });
643 }
644
645 /**
646  * Ajaxifies the given Sone by enhancing all eligible elements with AJAX.
647  *
648  * @param soneElement
649  *            The Sone to ajaxify
650  */
651 function ajaxifySone(soneElement) {
652         /*
653          * convert all “follow”, “unfollow”, “lock”, and “unlock” links to something
654          * nicer.
655          */
656         $(".follow", soneElement).submit(function() {
657                 var followElement = this;
658                 ajaxGet("followSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
659                         $(followElement).addClass("hidden");
660                         $(followElement).parent().find(".unfollow").removeClass("hidden");
661                 });
662                 return false;
663         });
664         $(".unfollow", soneElement).submit(function() {
665                 var unfollowElement = this;
666                 ajaxGet("unfollowSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
667                         $(unfollowElement).addClass("hidden");
668                         $(unfollowElement).parent().find(".follow").removeClass("hidden");
669                 });
670                 return false;
671         });
672         $(".lock", soneElement).submit(function() {
673                 var lockElement = this;
674                 ajaxGet("lockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
675                         $(lockElement).addClass("hidden");
676                         $(lockElement).parent().find(".unlock").removeClass("hidden");
677                 });
678                 return false;
679         });
680         $(".unlock", soneElement).submit(function() {
681                 var unlockElement = this;
682                 ajaxGet("unlockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
683                         $(unlockElement).addClass("hidden");
684                         $(unlockElement).parent().find(".lock").removeClass("hidden");
685                 });
686                 return false;
687         });
688
689         /* mark Sone as known when clicking it. */
690         $(soneElement).click(function() {
691                 markSoneAsKnown(this);
692         });
693 }
694
695 /**
696  * Ajaxifies the given post by enhancing all eligible elements with AJAX.
697  *
698  * @param postElement
699  *            The post element to ajaxify
700  */
701 function ajaxifyPost(postElement) {
702         $(postElement).find("form").submit(function() {
703                 return false;
704         });
705         $(postElement).find(".create-reply button:submit").click(function() {
706                 button = $(this);
707                 button.attr("disabled", "disabled");
708                 sender = $(this.form).find(":input[name=sender]").val();
709                 inputField = $(this.form).find(":input[name=text]:enabled").get(0);
710                 postId = getPostId(this);
711                 text = $(inputField).val();
712                 (function(sender, postId, text, inputField) {
713                         postReply(sender, postId, text, function(success, error, replyId, soneId) {
714                                 if (success) {
715                                         $(inputField).val("");
716                                         loadNewReply(replyId, soneId, postId);
717                                         $("#sone .post#post-" + postId + " .create-reply").addClass("hidden");
718                                         $("#sone .post#post-" + postId + " .create-reply .sender").hide();
719                                         $("#sone .post#post-" + postId + " .create-reply .select-sender").show();
720                                         $("#sone .post#post-" + postId + " .create-reply :input[name=sender]").val(getCurrentSoneId());
721                                 } else {
722                                         alert(error);
723                                 }
724                                 button.removeAttr("disabled");
725                         });
726                 })(sender, postId, text, inputField);
727                 return false;
728         });
729
730         /* replace all “delete” buttons with javascript. */
731         (function(postElement) {
732                 getTranslation("WebInterface.Confirmation.DeletePostButton", function(deletePostText) {
733                         postId = getPostId(postElement);
734                         enhanceDeletePostButton($(postElement).find(".delete-post button"), postId, deletePostText);
735                 });
736         })(postElement);
737
738         /* convert all “like” buttons to javascript functions. */
739         $(postElement).find(".like-post").submit(function() {
740                 likePost(getPostId(this));
741                 return false;
742         });
743         $(postElement).find(".unlike-post").submit(function() {
744                 unlikePost(getPostId(this));
745                 return false;
746         });
747
748         /* convert trust control buttons to javascript functions. */
749         $(postElement).find(".post-trust").submit(function() {
750                 trustSone(getPostAuthor(this));
751                 return false;
752         });
753         $(postElement).find(".post-distrust").submit(function() {
754                 distrustSone(getPostAuthor(this));
755                 return false;
756         });
757         $(postElement).find(".post-untrust").submit(function() {
758                 untrustSone(getPostAuthor(this));
759                 return false;
760         });
761
762         /* convert bookmark/unbookmark buttons to javascript functions. */
763         $(postElement).find(".bookmark").submit(function() {
764                 bookmarkPost(getPostId(this));
765                 return false;
766         });
767         $(postElement).find(".unbookmark").submit(function() {
768                 unbookmarkPost(getPostId(this));
769                 return false;
770         });
771
772         /* convert “show source” link into javascript function. */
773         $(postElement).find(".show-source").each(function() {
774                 $("a", this).click(function() {
775                         post = getPostElement(this);
776                         rawPostText = $(".post-text.raw-text", post);
777                         rawPostText.toggleClass("hidden");
778                         if (rawPostText.hasClass("hidden")) {
779                                 $(".post-text.short-text", post).removeClass("hidden");
780                                 $(".post-text.text", post).addClass("hidden");
781                                 $(".expand-post-text", post).removeClass("hidden");
782                                 $(".shrink-post-text", post).addClass("hidden");
783                         } else {
784                                 $(".post-text.short-text", post).addClass("hidden");
785                                 $(".post-text.text", post).addClass("hidden");
786                                 $(".expand-post-text", post).addClass("hidden");
787                                 $(".shrink-post-text", post).addClass("hidden");
788                         }
789                         return false;
790                 });
791         });
792
793         /* convert “show more” link into javascript function. */
794         $(postElement).find(".expand-post-text").each(function() {
795                 $(this).click(function() {
796                         $(".post-text.text", getPostElement(this)).toggleClass("hidden");
797                         $(".post-text.short-text", getPostElement(this)).toggleClass("hidden");
798                         $(".expand-post-text", getPostElement(this)).toggleClass("hidden");
799                         $(".shrink-post-text", getPostElement(this)).toggleClass("hidden");
800                         return false;
801                 });
802         });
803         $(postElement).find(".shrink-post-text").each(function() {
804                 $(this).click(function() {
805                         $(".post-text.text", getPostElement(this)).toggleClass("hidden");
806                         $(".post-text.short-text", getPostElement(this)).toggleClass("hidden");
807                         $(".expand-post-text", getPostElement(this)).toggleClass("hidden");
808                         $(".shrink-post-text", getPostElement(this)).toggleClass("hidden");
809                         return false;
810                 });
811         });
812
813         /* ajaxify author/post links */
814         $(".post-status-line .permalink a", postElement).click(function() {
815                 if (!$(".create-reply", postElement).hasClass("hidden")) {
816                         textArea = $("input.reply-input", postElement).focus().data("textarea");
817                         $(textArea).replaceSelection($(this).attr("href"));
818                 }
819                 return false;
820         });
821
822         /* add “comment” link. */
823         addCommentLink(getPostId(postElement), getPostAuthor(postElement), postElement, $(postElement).find(".post-status-line .permalink-author"));
824
825         /* process all replies. */
826         replyIds = [];
827         $(postElement).find(".reply").each(function() {
828                 replyIds.push(getReplyId(this));
829                 ajaxifyReply(this);
830         });
831         updateReplyTimes(replyIds.join(","));
832
833         /* process reply input fields. */
834         getTranslation("WebInterface.DefaultText.Reply", function(text) {
835                 $(postElement).find("input.reply-input").each(function() {
836                         registerInputTextareaSwap(this, text, "text", false, false);
837                 });
838         });
839
840         /* process sender selection. */
841         $(".select-sender", postElement).css("display", "inline");
842         $(".sender", postElement).hide();
843         $(".select-sender button", postElement).click(function() {
844                 $(".sender", postElement).show();
845                 $(".select-sender", postElement).hide();
846                 return false;
847         });
848
849         /* mark everything as known on click. */
850         (function(postElement) {
851                 $(postElement).click(function(event) {
852                         if ($(event.target).hasClass("click-to-show")) {
853                                 return false;
854                         }
855                         markPostAsKnown(postElement, false);
856                 });
857         })(postElement);
858
859         /* hide reply input field. */
860         $(postElement).find(".create-reply").addClass("hidden");
861
862         /* show Sone menu when hovering over the avatar. */
863         $(postElement).find(".post-avatar").mouseover(function() {
864                 if (typeof currentSoneMenuTimeoutHandler != undefined) {
865                         clearTimeout(currentSoneMenuTimeoutHandler);
866                 }
867                 currentSoneMenuId = getPostId(this);
868                 currentSoneMenuTimeoutHandler = setTimeout(function() {
869                         $(".sone-menu:visible").fadeOut();
870                         $(".sone-post-menu", postElement).mouseleave(function() {
871                                 $(this).fadeOut();
872                         }).fadeIn();
873                 }, 1000);
874         }).mouseleave(function() {
875                 if (currentSoneMenuId = getPostId(this)) {
876                         clearTimeout(currentSoneMenuTimeoutHandler);
877                 }
878         });
879         (function(postElement) {
880                 var soneId = $(".sone-menu-id:first", postElement).text();
881                 $(".sone-post-menu .follow", postElement).click(function() {
882                         var followElement = this;
883                         ajaxGet("followSone.ajax", { "sone": soneId, "formPassword": getFormPassword() }, function() {
884                                 $(followElement).addClass("hidden");
885                                 $(followElement).parent().find(".unfollow").removeClass("hidden");
886                                 $("#sone .sone-menu").each(function() {
887                                         if (getMenuSone(this) == soneId) {
888                                                 $(".follow", this).toggleClass("hidden", true);
889                                                 $(".unfollow", this).toggleClass("hidden", false);
890                                         }
891                                 });
892                         });
893                         return false;
894                 });
895                 $(".sone-post-menu .unfollow", postElement).click(function() {
896                         var unfollowElement = this;
897                         ajaxGet("unfollowSone.ajax", { "sone": soneId, "formPassword": getFormPassword() }, function() {
898                                 $(unfollowElement).addClass("hidden");
899                                 $(unfollowElement).parent().find(".follow").removeClass("hidden");
900                                 $("#sone .sone-menu").each(function() {
901                                         if (getMenuSone(this) == soneId) {
902                                                 $(".follow", this).toggleClass("hidden", false);
903                                                 $(".unfollow", this).toggleClass("hidden", true);
904                                         }
905                                 });
906                         });
907                         return false;
908                 });
909         })(postElement);
910 }
911
912 /**
913  * Ajaxifies the given reply element.
914  *
915  * @param replyElement
916  *            The reply element to ajaxify
917  */
918 function ajaxifyReply(replyElement) {
919         $(replyElement).find(".like-reply").submit(function() {
920                 likeReply(getReplyId(this));
921                 return false;
922         });
923         $(replyElement).find(".unlike-reply").submit(function() {
924                 unlikeReply(getReplyId(this));
925                 return false;
926         });
927         (function(replyElement) {
928                 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(deleteReplyText) {
929                         $(replyElement).find(".delete-reply button").each(function() {
930                                 enhanceDeleteReplyButton(this, getReplyId(replyElement), deleteReplyText);
931                         });
932                 });
933         })(replyElement);
934
935         /* ajaxify author links */
936         $(".reply-status-line .permalink a", replyElement).click(function() {
937                 if (!$(".create-reply", getPostElement(replyElement)).hasClass("hidden")) {
938                         textArea = $("input.reply-input", getPostElement(replyElement)).focus().data("textarea");
939                         $(textArea).replaceSelection($(this).attr("href"));
940                 }
941                 return false;
942         });
943
944         addCommentLink(getPostId(replyElement), getReplyAuthor(replyElement), replyElement, $(replyElement).find(".reply-status-line .permalink-author"));
945
946         /* convert “show source” link into javascript function. */
947         $(replyElement).find(".show-reply-source").each(function() {
948                 $("a", this).click(function() {
949                         reply = getReplyElement(this);
950                         rawReplyText = $(".reply-text.raw-text", reply);
951                         rawReplyText.toggleClass("hidden");
952                         if (rawReplyText.hasClass("hidden")) {
953                                 $(".reply-text.short-text", reply).removeClass("hidden");
954                                 $(".reply-text.text", reply).addClass("hidden");
955                                 $(".expand-reply-text", reply).removeClass("hidden");
956                                 $(".shrink-reply-text", reply).addClass("hidden");
957                         } else {
958                                 $(".reply-text.short-text", reply).addClass("hidden");
959                                 $(".reply-text.text", reply).addClass("hidden");
960                                 $(".expand-reply-text", reply).addClass("hidden");
961                                 $(".shrink-reply-text", reply).addClass("hidden");
962                         }
963                         return false;
964                 });
965         });
966
967         /* convert “show more” link into javascript function. */
968         $(replyElement).find(".expand-reply-text").each(function() {
969                 $(this).click(function() {
970                         $(".reply-text.text", getReplyElement(this)).toggleClass("hidden");
971                         $(".reply-text.short-text", getReplyElement(this)).toggleClass("hidden");
972                         $(".expand-reply-text", getReplyElement(this)).toggleClass("hidden");
973                         $(".shrink-reply-text", getReplyElement(this)).toggleClass("hidden");
974                         return false;
975                 });
976         });
977         $(replyElement).find(".shrink-reply-text").each(function() {
978                 $(this).click(function() {
979                         $(".reply-text.text", getReplyElement(this)).toggleClass("hidden");
980                         $(".reply-text.short-text", getReplyElement(this)).toggleClass("hidden");
981                         $(".expand-reply-text", getReplyElement(this)).toggleClass("hidden");
982                         $(".shrink-reply-text", getReplyElement(this)).toggleClass("hidden");
983                         return false;
984                 });
985         });
986
987         /* convert trust control buttons to javascript functions. */
988         $(replyElement).find(".reply-trust").submit(function() {
989                 trustSone(getReplyAuthor(this));
990                 return false;
991         });
992         $(replyElement).find(".reply-distrust").submit(function() {
993                 distrustSone(getReplyAuthor(this));
994                 return false;
995         });
996         $(replyElement).find(".reply-untrust").submit(function() {
997                 untrustSone(getReplyAuthor(this));
998                 return false;
999         });
1000
1001         /* show Sone menu when hovering over the avatar. */
1002         $(replyElement).find(".reply-avatar").mouseover(function() {
1003                 if (typeof currentSoneMenuTimeoutHandler != undefined) {
1004                         clearTimeout(currentSoneMenuTimeoutHandler);
1005                 }
1006                 currentSoneMenuId = getPostId(this) + "-" + getReplyId(this);
1007                 currentSoneMenuTimeoutHandler = setTimeout(function() {
1008                         $(".sone-menu:visible").fadeOut();
1009                         $(".sone-reply-menu", replyElement).mouseleave(function() {
1010                                 $(this).fadeOut();
1011                         }).fadeIn();
1012                 }, 1000);
1013         }).mouseleave(function() {
1014                 if (currentSoneMenuId = getPostId(this) + "-" + getReplyId(this)) {
1015                         clearTimeout(currentSoneMenuTimeoutHandler);
1016                 }
1017         });
1018         (function(replyElement) {
1019                 var soneId = $(".sone-menu-id", replyElement).text();
1020                 $(".sone-menu .follow", replyElement).click(function() {
1021                         var followElement = this;
1022                         ajaxGet("followSone.ajax", { "sone": soneId, "formPassword": getFormPassword() }, function() {
1023                                 $(followElement).addClass("hidden");
1024                                 $(followElement).parent().find(".unfollow").removeClass("hidden");
1025                                 $("#sone .sone-menu").each(function() {
1026                                         if (getMenuSone(this) == soneId) {
1027                                                 $(".follow", this).toggleClass("hidden", true);
1028                                                 $(".unfollow", this).toggleClass("hidden", false);
1029                                         }
1030                                 });
1031                         });
1032                         return false;
1033                 });
1034                 $(".sone-menu .unfollow", replyElement).click(function() {
1035                         var unfollowElement = this;
1036                         ajaxGet("unfollowSone.ajax", { "sone": soneId, "formPassword": getFormPassword() }, function() {
1037                                 $(unfollowElement).addClass("hidden");
1038                                 $(unfollowElement).parent().find(".follow").removeClass("hidden");
1039                                 $("#sone .sone-menu").each(function() {
1040                                         if (getMenuSone(this) == soneId) {
1041                                                 $(".follow", this).toggleClass("hidden", false);
1042                                                 $(".unfollow", this).toggleClass("hidden", true);
1043                                         }
1044                                 });
1045                         });
1046                         return false;
1047                 });
1048         })(replyElement);
1049 }
1050
1051 /**
1052  * Ajaxifies the given notification by replacing the form with AJAX.
1053  *
1054  * @param notification
1055  *            jQuery object representing the notification.
1056  */
1057 function ajaxifyNotification(notification) {
1058         notification.find("form").submit(function() {
1059                 return false;
1060         });
1061         notification.find("input[name=returnPage]").val($.url.attr("relative"));
1062         if (notification.find(".short-text").length > 0) {
1063                 notification.find(".short-text").removeClass("hidden");
1064                 notification.find(".text").addClass("hidden");
1065         }
1066         notification.find("form.mark-as-read button").click(function() {
1067                 allIds = $(":input[name=id]", this.form).val().split(" ");
1068                 for (var index = 0; index < allIds.length; index += 16) {
1069                         ids = allIds.slice(index, index + 16).join(" ");
1070                         ajaxGet("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": $(":input[name=type]", this.form).val(), "id": ids});
1071                 }
1072         });
1073         notification.find("a[class^='link-']").each(function() {
1074                 linkElement = $(this);
1075                 if (linkElement.is("[href^='viewPost']")) {
1076                         id = linkElement.attr("class").substr(5);
1077                         if (hasPost(id)) {
1078                                 linkElement.attr("href", "#post-" + id).addClass("in-page-link");
1079                         }
1080                 }
1081         });
1082         notification.find("form.dismiss button").click(function() {
1083                 ajaxGet("dismissNotification.ajax", { "formPassword" : getFormPassword(), "notification" : notification.attr("id") }, function(data, textStatus) {
1084                         /* dismiss in case of error, too. */
1085                         notification.slideUp();
1086                 }, function(xmlHttpRequest, textStatus, error) {
1087                         /* ignore error. */
1088                 });
1089         });
1090         return notification;
1091 }
1092
1093 /**
1094  * Returns the notification hash. This hash is used in {@link #getStatus()} to
1095  * determine whether the notifications changed and need to be reloaded.
1096  */
1097 function getNotificationHash() {
1098         return $("#sone #notification-area #notification-hash").text();
1099 }
1100
1101 /**
1102  * Sets the notification hash.
1103  *
1104  * @param notificationHash
1105  *            The new notification hash
1106  */
1107 function setNotificationHash(notificationHash) {
1108         $("#sone #notification-area #notification-hash").text(notificationHash);
1109 }
1110
1111 /**
1112  * Retrieves element IDs from notification elements.
1113  *
1114  * @param notification
1115  *            The notification element
1116  * @param selector
1117  *            The selector of the element containing the ID as text
1118  * @returns All extracted IDs
1119  */
1120 function getElementIds(notification, selector) {
1121         elementIds = [];
1122         $(selector, notification).each(function() {
1123                 elementIds.push($(this).text());
1124         });
1125         return elementIds;
1126 }
1127
1128 /**
1129  * Compares the given notification elements and calls {@link #markSoneAsKnown()}
1130  * for every ID that is contained in the old notification but not in the new.
1131  *
1132  * @param oldNotification
1133  *            The old notification element
1134  * @param newNotification
1135  *            The new notification element
1136  */
1137 function checkForRemovedSones(oldNotification, newNotification) {
1138         if (getNotificationId(oldNotification) != "new-sone-notification") {
1139                 return;
1140         }
1141         oldIds = getElementIds(oldNotification, ".new-sone-id");
1142         newIds = getElementIds(newNotification, ".new-sone-id");
1143         $.each(oldIds, function(index, value) {
1144                 if ($.inArray(value, newIds) == -1) {
1145                         markSoneAsKnown(getSone(value), true);
1146                 }
1147         });
1148 }
1149
1150 /**
1151  * Compares the given notification elements and calls {@link #markPostAsKnown()}
1152  * for every ID that is contained in the old notification but not in the new.
1153  *
1154  * @param oldNotification
1155  *            The old notification element
1156  * @param newNotification
1157  *            The new notification element
1158  */
1159 function checkForRemovedPosts(oldNotification, newNotification) {
1160         if (getNotificationId(oldNotification) != "new-post-notification") {
1161                 return;
1162         }
1163         oldIds = getElementIds(oldNotification, ".post-id");
1164         newIds = getElementIds(newNotification, ".post-id");
1165         $.each(oldIds, function(index, value) {
1166                 if ($.inArray(value, newIds) == -1) {
1167                         markPostAsKnown(getPost(value), true);
1168                 }
1169         });
1170 }
1171
1172 /**
1173  * Compares the given notification elements and calls
1174  * {@link #markReplyAsKnown()} for every ID that is contained in the old
1175  * notification but not in the new.
1176  *
1177  * @param oldNotification
1178  *            The old notification element
1179  * @param newNotification
1180  *            The new notification element
1181  */
1182 function checkForRemovedReplies(oldNotification, newNotification) {
1183         if (getNotificationId(oldNotification) != "new-reply-notification") {
1184                 return;
1185         }
1186         oldIds = getElementIds(oldNotification, ".reply-id");
1187         newIds = getElementIds(newNotification, ".reply-id");
1188         $.each(oldIds, function(index, value) {
1189                 if ($.inArray(value, newIds) == -1) {
1190                         markReplyAsKnown(getReply(value), true);
1191                 }
1192         });
1193 }
1194
1195 function getStatus() {
1196         ajaxGet("getStatus.ajax", isViewSonePage() ? {"soneIds": getShownSoneId() } : isKnownSonesPage() ? {"soneIds": getShownSoneIds() } : {}, function(data, textStatus) {
1197                 if ((data != null) && data.success) {
1198                         /* process Sone information. */
1199                         $.each(data.sones, function(index, value) {
1200                                 updateSoneStatus(value.id, value.name, value.status, value.modified, value.locked, value.lastUpdatedUnknown ? null : value.lastUpdated, value.lastUpdatedText);
1201                         });
1202                         notLoggedIn = !data.loggedIn;
1203                         if (!notLoggedIn) {
1204                                 showOfflineMarker(!online);
1205                         }
1206                         if (data.notificationHash != getNotificationHash()) {
1207                                 console.log("Old hash: ", getNotificationHash(), ", new hash: ", data.notificationHash);
1208                                 requestNotifications();
1209                                 /* process new posts. */
1210                                 $.each(data.newPosts, function(index, value) {
1211                                         loadNewPost(value.id, value.sone, value.recipient, value.time);
1212                                 });
1213                                 /* process new replies. */
1214                                 $.each(data.newReplies, function(index, value) {
1215                                         loadNewReply(value.id, value.sone, value.post, value.postSone);
1216                                 });
1217                         }
1218                         /* do it again in 5 seconds. */
1219                         setTimeout(getStatus, 5000);
1220                 } else {
1221                         /* data.success was false, wait 30 seconds. */
1222                         setTimeout(getStatus, 30000);
1223                 }
1224         }, function() {
1225                 statusRequestQueued = false;
1226                 ajaxError();
1227         });
1228 }
1229
1230 function requestNotifications() {
1231         ajaxGet("getNotifications.ajax", {}, function(data, textStatus) {
1232                 if (data && data.success) {
1233                         /* search for removed notifications. */
1234                         $("#sone #notification-area .notification").each(function() {
1235                                 notificationId = $(this).attr("id");
1236                                 foundNotification = false;
1237                                 $.each(data.notifications, function(index, value) {
1238                                         if (value.id == notificationId) {
1239                                                 foundNotification = true;
1240                                                 return false;
1241                                         }
1242                                 });
1243                                 if (!foundNotification) {
1244                                         if (notificationId == "new-sone-notification" && (data.options["ShowNotification/NewSones"] == true)) {
1245                                                 $(".new-sone-id", this).each(function(index, element) {
1246                                                         soneId = $(this).text();
1247                                                         markSoneAsKnown(getSone(soneId), true);
1248                                                 });
1249                                         } else if (notificationId == "new-post-notification" && (data.options["ShowNotification/NewPosts"] == true)) {
1250                                                 $(".post-id", this).each(function(index, element) {
1251                                                         postId = $(this).text();
1252                                                         markPostAsKnown(getPost(postId), true);
1253                                                 });
1254                                         } else if (notificationId == "new-reply-notification" && (data.options["ShowNotification/NewReplies"] == true)) {
1255                                                 $(".reply-id", this).each(function(index, element) {
1256                                                         replyId = $(this).text();
1257                                                         markReplyAsKnown(getReply(replyId), true);
1258                                                 });
1259                                         }
1260                                         $(this).slideUp("normal", function() {
1261                                                 $(this).remove();
1262                                                 /* remove activity when no notifications are visible. */
1263                                                 if ($("#sone #notification-area .notification").length == 0) {
1264                                                         resetActivity();
1265                                                 }
1266                                         });
1267                                 }
1268                         });
1269                         /* process notifications. */
1270                         $.each(data.notifications, function(index, value) {
1271                                 oldNotification = getNotification(value.id);
1272                                 notification = ajaxifyNotification(createNotification(value.id, value.lastUpdatedTime, value.text, value.dismissable)).hide();
1273                                 if (oldNotification.length != 0) {
1274                                         if ((oldNotification.find(".short-text").length > 0) && (notification.find(".short-text").length > 0)) {
1275                                                 opened = oldNotification.is(":visible") && oldNotification.find(".short-text").hasClass("hidden");
1276                                                 notification.find(".short-text").toggleClass("hidden", opened);
1277                                                 notification.find(".text").toggleClass("hidden", !opened);
1278                                         }
1279                                         checkForRemovedSones(oldNotification, notification);
1280                                         checkForRemovedPosts(oldNotification, notification);
1281                                         checkForRemovedReplies(oldNotification, notification);
1282                                         oldNotification.replaceWith(notification.show());
1283                                 } else {
1284                                         $("#sone #notification-area").append(notification);
1285                                         if (value.id.substring(0, 5) != "local") {
1286                                                 notification.slideDown();
1287                                                 setActivity();
1288                                         }
1289                                 }
1290                         });
1291                         setNotificationHash(data.notificationHash);
1292                 }
1293         });
1294 }
1295
1296 /**
1297  * Returns the ID of the currently logged in Sone.
1298  *
1299  * @return The ID of the current Sone, or an empty string if no Sone is logged
1300  *         in
1301  */
1302 function getCurrentSoneId() {
1303         return $("#currentSoneId").text();
1304 }
1305
1306 /**
1307  * Returns the content of the page-id attribute.
1308  *
1309  * @returns The page ID
1310  */
1311 function getPageId() {
1312         return $("#sone .page-id").text();
1313 }
1314
1315 /**
1316  * Returns whether the current page is the index page.
1317  *
1318  * @returns {Boolean} <code>true</code> if the current page is the index page,
1319  *          <code>false</code> otherwise
1320  */
1321 function isIndexPage() {
1322         return getPageId() == "index";
1323 }
1324
1325 /**
1326  * Returns the current page of the selected pagination. If no pagination can be
1327  * found with the given selector, {@code 1} is returned.
1328  *
1329  * @param paginationSelector
1330  *            The pagination selector
1331  * @returns The current page of the pagination
1332  */
1333 function getPage(paginationSelector) {
1334         pagination = $(paginationSelector);
1335         if (pagination.length > 0) {
1336                 return $(".current-page", paginationSelector).text();
1337         }
1338         return 1;
1339 }
1340
1341 /**
1342  * Returns whether the current page is a “view Sone” page.
1343  *
1344  * @returns {Boolean} <code>true</code> if the current page is a “view Sone”
1345  *          page, <code>false</code> otherwise
1346  */
1347 function isViewSonePage() {
1348         return getPageId() == "view-sone";
1349 }
1350
1351 /**
1352  * Returns the ID of the currently shown Sone. This will only return a sensible
1353  * value if isViewSonePage() returns <code>true</code>.
1354  *
1355  * @returns The ID of the currently shown Sone
1356  */
1357 function getShownSoneId() {
1358         return $("#sone .sone-id").first().text();
1359 }
1360
1361 /**
1362  * Returns the ID of all currently visible Sones. This is mainly used on the
1363  * “Known Sones” page.
1364  *
1365  * @returns The ID of the currently shown Sones
1366  */
1367 function getShownSoneIds() {
1368         var soneIds = new Array();
1369         $("#sone #known-sones .sone .id").each(function() {
1370                 soneIds.push($(this).text());
1371         });
1372         return soneIds.join(",");
1373 }
1374
1375 /**
1376  * Returns whether the current page is a “view post” page.
1377  *
1378  * @returns {Boolean} <code>true</code> if the current page is a “view post”
1379  *          page, <code>false</code> otherwise
1380  */
1381 function isViewPostPage() {
1382         return getPageId() == "view-post";
1383 }
1384
1385 /**
1386  * Returns the ID of the currently shown post. This will only return a sensible
1387  * value if isViewPostPage() returns <code>true</code>.
1388  *
1389  * @returns The ID of the currently shown post
1390  */
1391 function getShownPostId() {
1392         return $("#sone .post-id").text();
1393 }
1394
1395 /**
1396  * Returns whether the current page is the “known Sones” page.
1397  *
1398  * @returns {Boolean} <code>true</code> if the current page is the “known
1399  *          Sones” page, <code>false</code> otherwise
1400  */
1401 function isKnownSonesPage() {
1402         return getPageId() == "known-sones";
1403 }
1404
1405 /**
1406  * Returns whether a post with the given ID exists on the current page.
1407  *
1408  * @param postId
1409  *            The post ID to check for
1410  * @returns {Boolean} <code>true</code> if a post with the given ID already
1411  *          exists on the page, <code>false</code> otherwise
1412  */
1413 function hasPost(postId) {
1414         return $(".post#post-" + postId).length > 0;
1415 }
1416
1417 /**
1418  * Returns whether a reply with the given ID exists on the current page.
1419  *
1420  * @param replyId
1421  *            The reply ID to check for
1422  * @returns {Boolean} <code>true</code> if a reply with the given ID already
1423  *          exists on the page, <code>false</code> otherwise
1424  */
1425 function hasReply(replyId) {
1426         return $("#sone .reply#reply-" + replyId).length > 0;
1427 }
1428
1429 function loadNewPost(postId, soneId, recipientId, time) {
1430         if (hasPost(postId)) {
1431                 return;
1432         }
1433         if (!isIndexPage() || (getPage(".pagination-index") > 1)) {
1434                 if (!isViewPostPage() || (getShownPostId() != postId)) {
1435                         if (!isViewSonePage() || ((getShownSoneId() != soneId) && (getShownSoneId() != recipientId)) || (getPage(".post-navigation") > 1)) {
1436                                 return;
1437                         }
1438                 }
1439         }
1440         if (getPostTime($("#sone .post").last()) > time) {
1441                 return;
1442         }
1443         ajaxGet("getPost.ajax", { "post" : postId }, function(data, textStatus) {
1444                 if ((data != null) && data.success) {
1445                         if (hasPost(data.post.id)) {
1446                                 return;
1447                         }
1448                         if ((!isIndexPage() || (getPage(".pagination-index") > 1)) && !(isViewSonePage() && ((getShownSoneId() == data.post.sone) || (getShownSoneId() == data.post.recipient) || (getPage(".post-navigation") > 1)))) {
1449                                 return;
1450                         }
1451                         var firstOlderPost = null;
1452                         $("#sone .post").each(function() {
1453                                 if (getPostTime(this) < data.post.time) {
1454                                         firstOlderPost = $(this);
1455                                         return false;
1456                                 }
1457                         });
1458                         newPost = $(data.post.html).addClass("hidden");
1459                         if ($(".post-author-local", newPost).text() == "true") {
1460                                 newPost.removeClass("new");
1461                         }
1462                         if (firstOlderPost != null) {
1463                                 newPost.insertBefore(firstOlderPost);
1464                         }
1465                         ajaxifyPost(newPost);
1466                         updatePostTimes(data.post.id);
1467                         newPost.slideDown();
1468                         setActivity();
1469                 }
1470         });
1471 }
1472
1473 function loadNewReply(replyId, soneId, postId, postSoneId) {
1474         if (hasReply(replyId)) {
1475                 return;
1476         }
1477         if (!hasPost(postId)) {
1478                 return;
1479         }
1480         ajaxGet("getReply.ajax", { "reply": replyId }, function(data, textStatus) {
1481                 /* find post. */
1482                 if ((data != null) && data.success) {
1483                         if (hasReply(data.reply.id)) {
1484                                 return;
1485                         }
1486                         $("#sone .post#post-" + data.reply.postId).each(function() {
1487                                 var firstNewerReply = null;
1488                                 $(this).find(".replies .reply").each(function() {
1489                                         if (getReplyTime(this) > data.reply.time) {
1490                                                 firstNewerReply = $(this);
1491                                                 return false;
1492                                         }
1493                                 });
1494                                 newReply = $(data.reply.html).addClass("hidden");
1495                                 if ($(".reply-author-local", newReply).text() == "true") {
1496                                         newReply.removeClass("new");
1497                                         (function(newReply) {
1498                                                 setTimeout(function() {
1499                                                         markReplyAsKnown(newReply, false);
1500                                                 }, 5000);
1501                                         })(newReply);
1502                                 }
1503                                 if (firstNewerReply != null) {
1504                                         newReply.insertBefore(firstNewerReply);
1505                                 } else {
1506                                         if ($(this).find(".replies .create-reply")) {
1507                                                 $(this).find(".replies .create-reply").before(newReply);
1508                                         } else {
1509                                                 $(this).find(".replies").append(newReply);
1510                                         }
1511                                 }
1512                                 ajaxifyReply(newReply);
1513                                 updateReplyTimes(data.reply.id);
1514                                 newReply.slideDown();
1515                                 setActivity();
1516                                 return false;
1517                         });
1518                 }
1519         });
1520 }
1521
1522 /**
1523  * Marks the given Sone as known if it is still new.
1524  *
1525  * @param soneElement
1526  *            The Sone to mark as known
1527  * @param skipRequest
1528  *            true to skip the JSON request, false or omit to perform the JSON
1529  *            request
1530  */
1531 function markSoneAsKnown(soneElement, skipRequest) {
1532         if ($(soneElement).hasClass("new")) {
1533                 $(soneElement).removeClass("new");
1534                 if ((typeof skipRequest == "undefined") || !skipRequest) {
1535                         ajaxGet("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "sone", "id": getSoneId(soneElement)});
1536                         requestNotifications();
1537                 }
1538         }
1539 }
1540
1541 function markPostAsKnown(postElements, skipRequest) {
1542         $(postElements).each(function() {
1543                 postElement = this;
1544                 if ($(postElement).hasClass("new") || ((typeof skipRequest != "undefined"))) {
1545                         (function(postElement) {
1546                                 $(postElement).removeClass("new");
1547                                 if ((typeof skipRequest == "undefined") || !skipRequest) {
1548                                         ajaxGet("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "post", "id": getPostId(postElement)});
1549                                         requestNotifications();
1550                                 }
1551                         })(postElement);
1552                 }
1553                 $(".click-to-show", postElement).removeClass("new");
1554         });
1555         markReplyAsKnown($(postElements).find(".reply"), true);
1556 }
1557
1558 function markReplyAsKnown(replyElements, skipRequest) {
1559         $(replyElements).each(function() {
1560                 replyElement = this;
1561                 if ($(replyElement).hasClass("new") || ((typeof skipRequest != "undefined"))) {
1562                         (function(replyElement) {
1563                                 $(replyElement).removeClass("new");
1564                                 if ((typeof skipRequest == "undefined") || !skipRequest) {
1565                                         ajaxGet("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "reply", "id": getReplyId(replyElement)});
1566                                         requestNotifications();
1567                                 }
1568                         })(replyElement);
1569                 }
1570         });
1571 }
1572
1573 /**
1574  * Updates the time of the post with the given ID.
1575  *
1576  * @param postId
1577  *            The ID of the post to update
1578  * @param timeText
1579  *            The text of the time to show
1580  * @param refreshTime
1581  *            The refresh time after which to request a new time (in seconds)
1582  * @param tooltip
1583  *            The tooltip to show
1584  */
1585 function updatePostTime(postId, timeText, refreshTime, tooltip) {
1586         if (!getPost(postId).is(":visible")) {
1587                 return;
1588         }
1589         getPost(postId).find(".post-status-line > .time a").html(timeText).attr("title", tooltip);
1590         (function(postId, refreshTime) {
1591                 setTimeout(function() {
1592                         updatePostTimes(postId);
1593                 }, refreshTime * 1000);
1594         })(postId, refreshTime);
1595 }
1596
1597 /**
1598  * Requests new rendered times for the posts with the given IDs.
1599  *
1600  * @param postIds
1601  *            Comma-separated post IDs
1602  */
1603 function updatePostTimes(postIds) {
1604         ajaxGet("getTimes.ajax", { "posts" : postIds }, function(data, textStatus) {
1605                 if ((data != null) && data.success) {
1606                         $.each(data.postTimes, function(index, value) {
1607                                 updatePostTime(index, value.timeText, value.refreshTime, value.tooltip);
1608                         });
1609                 }
1610         });
1611 }
1612
1613 /**
1614  * Updates the time of the reply with the given ID.
1615  *
1616  * @param postId
1617  *            The ID of the reply to update
1618  * @param timeText
1619  *            The text of the time to show
1620  * @param refreshTime
1621  *            The refresh time after which to request a new time (in seconds)
1622  * @param tooltip
1623  *            The tooltip to show
1624  */
1625 function updateReplyTime(replyId, timeText, refreshTime, tooltip) {
1626         getReply(replyId).find(".reply-status-line > .time").html(timeText).attr("title", tooltip);
1627         (function(replyId, refreshTime) {
1628                 setTimeout(function() {
1629                         updateReplyTimes(replyId);
1630                 }, refreshTime * 1000);
1631         })(replyId, refreshTime);
1632 }
1633
1634 /**
1635  * Requests new rendered times for the posts with the given IDs.
1636  *
1637  * @param postIds
1638  *            Comma-separated post IDs
1639  */
1640 function updateReplyTimes(replyIds) {
1641         ajaxGet("getTimes.ajax", { "replies" : replyIds }, function(data, textStatus) {
1642                 if ((data != null) && data.success) {
1643                         $.each(data.replyTimes, function(index, value) {
1644                                 updateReplyTime(index, value.timeText, value.refreshTime, value.tooltip);
1645                         });
1646                 }
1647         });
1648 }
1649
1650 function resetActivity() {
1651         title = document.title;
1652         if (title.indexOf('(') == 0) {
1653                 setTitle(title.substr(title.indexOf(' ') + 1));
1654         }
1655         iconBlinking = false;
1656 }
1657
1658 function setActivity() {
1659         if (!focus) {
1660                 title = document.title;
1661                 if (title.indexOf('(') != 0) {
1662                         setTitle("(!) " + title);
1663                 }
1664                 if (!iconBlinking) {
1665                         setTimeout(toggleIcon, 1500);
1666                         iconBlinking = true;
1667                 }
1668         }
1669 }
1670
1671 /**
1672  * Sets the window title after a small delay to prevent race-condition issues.
1673  *
1674  * @param title
1675  *            The title to set
1676  */
1677 function setTitle(title) {
1678         setTimeout(function() {
1679                 document.title = title;
1680         }, 50);
1681 }
1682
1683 /** Whether the icon is currently showing activity. */
1684 var iconActive = false;
1685
1686 /** Whether the icon is currently supposed to blink. */
1687 var iconBlinking = false;
1688
1689 /**
1690  * Toggles the icon. If the window has gained focus and the icon is still
1691  * showing the activity state, it is returned to normal.
1692  */
1693 function toggleIcon() {
1694         if (focus || !iconBlinking) {
1695                 if (iconActive) {
1696                         changeIcon("images/icon.png");
1697                         iconActive = false;
1698                 }
1699                 iconBlinking = false;
1700         } else {
1701                 iconActive = !iconActive;
1702                 changeIcon(iconActive ? "images/icon-activity.png" : "images/icon.png");
1703                 setTimeout(toggleIcon, 1500);
1704         }
1705 }
1706
1707 /**
1708  * Changes the icon of the page.
1709  *
1710  * @param iconUrl
1711  *            The new URL of the icon
1712  */
1713 function changeIcon(iconUrl) {
1714         $("link[rel=icon]").remove();
1715         $("head").append($("<link>").attr("rel", "icon").attr("type", "image/png").attr("href", iconUrl));
1716         $("iframe[id=icon-update]")[0].src += "";
1717 }
1718
1719 /**
1720  * Creates a new notification.
1721  *
1722  * @param id
1723  *            The ID of the notificaiton
1724  * @param text
1725  *            The text of the notification
1726  * @param dismissable
1727  *            <code>true</code> if the notification can be dismissed by the
1728  *            user
1729  */
1730 function createNotification(id, lastUpdatedTime, text, dismissable) {
1731         notification = $("<div></div>").addClass("notification").attr("id", id).attr("lastUpdatedTime", lastUpdatedTime);
1732         if (dismissable) {
1733                 dismissForm = $("#sone #notification-area #notification-dismiss-template").clone().removeClass("hidden").removeAttr("id");
1734                 dismissForm.find("input[name=notification]").val(id);
1735                 notification.append(dismissForm);
1736         }
1737         notification.append(text);
1738         return notification;
1739 }
1740
1741 /**
1742  * Shows the details of the notification with the given ID.
1743  *
1744  * @param notificationId
1745  *            The ID of the notification
1746  */
1747 function showNotificationDetails(notificationId) {
1748         $("#sone .notification#" + notificationId + " .text").removeClass("hidden");
1749         $("#sone .notification#" + notificationId + " .short-text").addClass("hidden");
1750 }
1751
1752 /**
1753  * Deletes the field with the given ID from the profile.
1754  *
1755  * @param fieldId
1756  *            The ID of the field to delete
1757  */
1758 function deleteProfileField(fieldId) {
1759         ajaxGet("deleteProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId}, function(data, textStatus) {
1760                 if (data && data.success) {
1761                         $("#sone .profile-field#" + data.field.id).slideUp();
1762                 }
1763         });
1764 }
1765
1766 /**
1767  * Renames a profile field.
1768  *
1769  * @param fieldId
1770  *            The ID of the field to rename
1771  * @param newName
1772  *            The new name of the field
1773  * @param successFunction
1774  *            Called when the renaming was successful
1775  */
1776 function editProfileField(fieldId, newName, successFunction) {
1777         ajaxGet("editProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId, "name": newName}, function(data, textStatus) {
1778                 if (data && data.success) {
1779                         successFunction();
1780                 }
1781         });
1782 }
1783
1784 /**
1785  * Moves the profile field with the given ID one slot in the given direction.
1786  *
1787  * @param fieldId
1788  *            The ID of the field to move
1789  * @param direction
1790  *            The direction to move in (“up” or “down”)
1791  * @param successFunction
1792  *            Function to call on success
1793  */
1794 function moveProfileField(fieldId, direction, successFunction) {
1795         ajaxGet("moveProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId, "direction": direction}, function(data, textStatus) {
1796                 if (data && data.success) {
1797                         successFunction();
1798                 }
1799         });
1800 }
1801
1802 /**
1803  * Moves the profile field with the given ID up one slot.
1804  *
1805  * @param fieldId
1806  *            The ID of the field to move
1807  * @param successFunction
1808  *            Function to call on success
1809  */
1810 function moveProfileFieldUp(fieldId, successFunction) {
1811         moveProfileField(fieldId, "up", successFunction);
1812 }
1813
1814 /**
1815  * Moves the profile field with the given ID down one slot.
1816  *
1817  * @param fieldId
1818  *            The ID of the field to move
1819  * @param successFunction
1820  *            Function to call on success
1821  */
1822 function moveProfileFieldDown(fieldId, successFunction) {
1823         moveProfileField(fieldId, "down", successFunction);
1824 }
1825
1826 var statusRequestQueued = true;
1827
1828 /**
1829  * Sets the status of the web interface as offline.
1830  */
1831 function ajaxError() {
1832         online = false;
1833         showOfflineMarker(true);
1834         if (!statusRequestQueued) {
1835                 setTimeout(getStatus, 5000);
1836                 statusRequestQueued = true;
1837         }
1838 }
1839
1840 /**
1841  * Sets the status of the web interface as online.
1842  */
1843 function ajaxSuccess() {
1844         online = true;
1845         showOfflineMarker(!online || (initiallyLoggedIn && notLoggedIn));
1846 }
1847
1848 /**
1849  * Shows or hides the offline marker.
1850  *
1851  * @param visible
1852  *            {@code true} to display the offline marker, {@code false} to hide
1853  *            it
1854  */
1855 function showOfflineMarker(visible) {
1856         /* jQuery documentation says toggle() works the other way around?! */
1857         $("#sone #offline-marker").toggle(visible);
1858         if (visible) {
1859                 $("#sone #main").addClass("offline");
1860         } else {
1861                 $("#sone #main").removeClass("offline");
1862         }
1863 }
1864
1865 //
1866 // EVERYTHING BELOW HERE IS EXECUTED AFTER LOADING THE PAGE
1867 //
1868
1869 var focus = true;
1870 var online = true;
1871 var initiallyLoggedIn = $("#sone #loggedIn").text() == "true";
1872 var notLoggedIn = !initiallyLoggedIn;
1873
1874 /** ID of the next-to-show Sone context menu. */
1875 var currentSoneMenuId;
1876
1877 /** Timeout handler for the next-to-show Sone context menu. */
1878 var currentSoneMenuTimeoutHandler;
1879
1880 $(document).ready(function() {
1881
1882         /* rip out the status update textarea. */
1883         $("#sone .rip-out").each(function() {
1884                 var oldElement = $(this);
1885                 newElement = $("<input type='text'/>");
1886                 newElement.attr("class", oldElement.attr("class")).attr("name", oldElement.attr("name"));
1887                 oldElement.before(newElement).remove();
1888         });
1889
1890         /* this initializes the status update input field. */
1891         getTranslation("WebInterface.DefaultText.StatusUpdate", function(defaultText) {
1892                 registerInputTextareaSwap("#sone #update-status .status-input", defaultText, "text", false, false);
1893                 $("#sone #update-status .select-sender").css("display", "inline");
1894                 $("#sone #update-status .sender").hide();
1895                 $("#sone #update-status .select-sender button").click(function() {
1896                         $("#sone #update-status .sender").show();
1897                         $("#sone #update-status .select-sender").hide();
1898                         return false;
1899                 });
1900                 $("#sone #update-status").submit(function() {
1901                         button = $("button:submit", this);
1902                         button.attr("disabled", "disabled");
1903                         if ($(this).find(":input.default:enabled").length > 0) {
1904                                 return false;
1905                         }
1906                         sender = $(this).find(":input[name=sender]").val();
1907                         text = $(this).find(":input[name=text]:enabled").val();
1908                         ajaxGet("createPost.ajax", { "formPassword": getFormPassword(), "sender": sender, "text": text }, function(data, textStatus) {
1909                                 button.removeAttr("disabled");
1910                         });
1911                         $(this).find(":input[name=sender]").val(getCurrentSoneId());
1912                         $(this).find(":input[name=text]:enabled").val("").blur();
1913                         $(this).find(".sender").hide();
1914                         $(this).find(".select-sender").show();
1915                         return false;
1916                 });
1917         });
1918
1919         /* ajaxify the search input field. */
1920         getTranslation("WebInterface.DefaultText.Search", function(defaultText) {
1921                 registerInputTextareaSwap("#sone #search input[name=query]", defaultText, "query", false, true);
1922         });
1923
1924         /* ajaxify input field on “view Sone” page. */
1925         getTranslation("WebInterface.DefaultText.Message", function(defaultText) {
1926                 registerInputTextareaSwap("#sone #post-message input[name=text]", defaultText, "text", false, false);
1927                 $("#sone #post-message .select-sender").css("display", "inline");
1928                 $("#sone #post-message .sender").hide();
1929                 $("#sone #post-message .select-sender button").click(function() {
1930                         $("#sone #post-message .sender").show();
1931                         $("#sone #post-message .select-sender").hide();
1932                         return false;
1933                 });
1934                 $("#sone #post-message").submit(function() {
1935                         sender = $(this).find(":input[name=sender]").val();
1936                         text = $(this).find(":input[name=text]:enabled").val();
1937                         ajaxGet("createPost.ajax", { "formPassword": getFormPassword(), "recipient": getShownSoneId(), "sender": sender, "text": text });
1938                         $(this).find(":input[name=sender]").val(getCurrentSoneId());
1939                         $(this).find(":input[name=text]:enabled").val("").blur();
1940                         $(this).find(".sender").hide();
1941                         $(this).find(".select-sender").show();
1942                         return false;
1943                 });
1944         });
1945
1946         /* Ajaxifies all posts. */
1947         /* calling getTranslation here will cache the necessary values. */
1948         getTranslation("WebInterface.Confirmation.DeletePostButton", function(text) {
1949                 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(text) {
1950                         getTranslation("WebInterface.DefaultText.Reply", function(text) {
1951                                 getTranslation("WebInterface.Button.Comment", function(text) {
1952                                         $("#sone .post").each(function() {
1953                                                 ajaxifyPost(this);
1954                                         });
1955                                 });
1956                         });
1957                 });
1958         });
1959
1960         /* update post times. */
1961         postIds = [];
1962         $("#sone .post").each(function() {
1963                 postIds.push(getPostId(this));
1964         });
1965         updatePostTimes(postIds.join(","));
1966
1967         /* hides all replies but the latest two. */
1968         if (!isViewPostPage()) {
1969                 getTranslation("WebInterface.ClickToShow.Replies", function(text) {
1970                         $("#sone .post .replies").each(function() {
1971                                 allReplies = $(this).find(".reply");
1972                                 if (allReplies.length > 2) {
1973                                         newHidden = false;
1974                                         for (replyIndex = 0; replyIndex < (allReplies.length - 2); ++replyIndex) {
1975                                                 $(allReplies[replyIndex]).addClass("hidden");
1976                                                 newHidden |= $(allReplies[replyIndex]).hasClass("new");
1977                                         }
1978                                         clickToShowElement = $("<div></div>").addClass("click-to-show");
1979                                         if (newHidden) {
1980                                                 clickToShowElement.addClass("new");
1981                                         }
1982                                         (function(clickToShowElement, allReplies, text) {
1983                                                 clickToShowElement.text(text);
1984                                                 clickToShowElement.click(function() {
1985                                                         allReplies.removeClass("hidden");
1986                                                         clickToShowElement.addClass("hidden");
1987                                                 });
1988                                         })(clickToShowElement, allReplies, text);
1989                                         $(allReplies[0]).before(clickToShowElement);
1990                                 }
1991                         });
1992                 });
1993         }
1994
1995         $("#sone .sone").each(function() {
1996                 ajaxifySone($(this));
1997         });
1998
1999         /* process all existing notifications, ajaxify dismiss buttons. */
2000         $("#sone #notification-area .notification").each(function() {
2001                 ajaxifyNotification($(this));
2002         });
2003
2004         /* activate status polling. */
2005         setTimeout(getStatus, 5000);
2006
2007         /* reset activity counter when the page has focus. */
2008         $(window).focus(function() {
2009                 focus = true;
2010                 resetActivity();
2011         }).blur(function() {
2012                 focus = false;
2013         });
2014
2015 });