]> git.mxchange.org Git - friendica.git/blob - view/js/main.js
Merge pull request #5665 from tobiasd/20180825-nl
[friendica.git] / view / js / main.js
1 function resizeIframe(obj) {
2         _resizeIframe(obj, 0);
3 }
4
5 function _resizeIframe(obj, desth) {
6         var h = obj.style.height;
7         var ch = obj.contentWindow.document.body.scrollHeight;
8
9         if (h == (ch + 'px')) {
10                 return;
11         }
12         if (desth == ch && ch > 0) {
13                 obj.style.height  = ch + 'px';
14         }
15         setTimeout(_resizeIframe, 100, obj, ch);
16 }
17
18 function openClose(theID) {
19         if (document.getElementById(theID).style.display == "block") {
20                 document.getElementById(theID).style.display = "none"
21         } else {
22                 document.getElementById(theID).style.display = "block"
23         }
24 }
25
26 function openMenu(theID) {
27         var el = document.getElementById(theID)
28
29         if (el) {
30                 el.style.display = "block";
31         }
32 }
33
34 function closeMenu(theID) {
35         var el = document.getElementById(theID)
36
37         if (el) {
38                 el.style.display = "none";
39         }
40 }
41
42 function decodeHtml(html) {
43         var txt = document.createElement("textarea");
44
45         txt.innerHTML = html;
46         return txt.value;
47 }
48
49 var src = null;
50 var prev = null;
51 var livetime = null;
52 var force_update = false;
53 var update_item = 0;
54 var stopped = false;
55 var totStopped = false;
56 var timer = null;
57 var pr = 0;
58 var liking = 0;
59 var in_progress = false;
60 var langSelect = false;
61 var commentBusy = false;
62 var last_popup_menu = null;
63 var last_popup_button = null;
64 var lockLoadContent = false;
65
66 $(function() {
67         $.ajaxSetup({cache: false});
68
69         /* setup comment textarea buttons */
70         /* comment textarea buttons needs some "data-*" attributes to work:
71          *              data-role="insert-formatting" : to mark the element as a formatting button
72          *              data-bbcode="<string>" : name of the bbcode element to insert. insertFormatting() will insert it as "[name][/name]"
73          *              data-id="<string>" : id of the comment, used to find other comment-related element, like the textarea
74          * */
75         $('body').on('click','[data-role="insert-formatting"]', function(e) {
76                 e.preventDefault();
77                 var o = $(this);
78                 var bbcode = o.data('bbcode');
79                 var id = o.data('id');
80                 if (bbcode == "img") {
81                         Dialog.doImageBrowser("comment", id);
82                         return;
83                 }
84                 insertFormatting(bbcode, id);
85         });
86
87         /* event from comment textarea button popups */
88         /* insert returned bbcode at cursor position or replace selected text */
89         $("body").on("fbrowser.image.comment", function(e, filename, bbcode, id) {
90                 $.colorbox.close();
91                 var textarea = document.getElementById("comment-edit-text-" +id);
92                 var start = textarea.selectionStart;
93                 var end = textarea.selectionEnd;
94                 textarea.value = textarea.value.substring(0, start) + bbcode + textarea.value.substring(end, textarea.value.length);
95                 $(textarea).trigger('change');
96         });
97
98         /* setup onoff widgets */
99         $(".onoff input").each(function() {
100                 val = $(this).val();
101                 id = $(this).attr("id");
102                 $("#"+id+"_onoff ." + (val == 0 ? "on":"off")).addClass("hidden");
103         });
104
105         $(".onoff > a").click(function(event) {
106                 event.preventDefault();
107                 var input = $(this).siblings("input");
108                 var val = 1-input.val();
109                 var id = input.attr("id");
110                 $("#"+id+"_onoff ." + (val == 0 ? "on":"off")).addClass("hidden");
111                 $("#"+id+"_onoff ." + (val == 1 ? "on":"off")).removeClass("hidden");
112                 input.val(val);
113         });
114
115         /* popup menus */
116         function close_last_popup_menu() {
117                 if (last_popup_menu) {
118                         last_popup_menu.hide();
119                         last_popup_menu.off('click', function(e) {e.stopPropagation()});
120                         last_popup_button.removeClass("selected");
121                         last_popup_menu = null;
122                         last_popup_button = null;
123                 }
124         }
125         $('a[rel^="#"]').click(function(e) {
126                 e.preventDefault();
127                 var parent = $(this).parent();
128                 var isSelected = (last_popup_button && parent.attr('id') == last_popup_button.attr('id'));
129                 close_last_popup_menu();
130                 if (isSelected) {
131                         return false;
132                 }
133                 menu = $($(this).attr('rel'));
134                 e.preventDefault();
135                 e.stopPropagation();
136                 if (menu.attr('popup') == "false") {
137                         return false;
138                 }
139                 parent.toggleClass("selected");
140                 menu.toggle();
141                 if (menu.css("display") == "none") {
142                         last_popup_menu = null;
143                         last_popup_button = null;
144                 } else {
145                         last_popup_menu = menu;
146                         last_popup_menu.on('click', function(e) {e.stopPropagation()});
147                         last_popup_button = parent;
148                         $('#nav-notifications-menu').perfectScrollbar('update');
149                 }
150                 return false;
151         });
152         $('html').click(function() {
153                 close_last_popup_menu();
154         });
155
156         // fancyboxes
157         $("a.popupbox").colorbox({
158                 'inline' : true,
159                 'transition' : 'elastic',
160                 'maxWidth' : '100%'
161         });
162         $("a.ajax-popupbox").colorbox({
163                 'transition' : 'elastic',
164                 'maxWidth' : '100%'
165         });
166
167         /* notifications template */
168         var notifications_tpl= unescape($("#nav-notifications-template[rel=template]").html());
169         var notifications_all = unescape($('<div>').append($("#nav-notifications-see-all").clone()).html()); //outerHtml hack
170         var notifications_mark = unescape($('<div>').append($("#nav-notifications-mark-all").clone()).html()); //outerHtml hack
171         var notifications_empty = unescape($("#nav-notifications-menu").html());
172
173         /* enable perfect-scrollbars for different elements */
174         $('#nav-notifications-menu, aside').perfectScrollbar();
175
176         /* nav update event  */
177         $('nav').bind('nav-update', function(e, data) {
178                 var invalid = data.invalid || 0;
179                 if (invalid == 1) {
180                         window.location.href=window.location.href
181                 }
182
183                 ['net', 'home', 'intro', 'mail', 'events', 'birthdays', 'notify'].forEach(function(type) {
184                         var number = data[type];
185                         if (number == 0) {
186                                 number = '';
187                                 $('#' + type + '-update').removeClass('show');
188                         } else {
189                                 $('#' + type + '-update').addClass('show');
190                         }
191                         $('#' + type + '-update').text(number);
192                 });
193
194                 var intro = data['intro'];
195                 if (intro == 0) {
196                         intro = ''; $('#intro-update-li').removeClass('show')
197                 } else {
198                         $('#intro-update-li').addClass('show')
199                 }
200
201                 $('#intro-update-li').html(intro);
202
203                 var mail = data['mail'];
204                 if (mail == 0) {
205                         mail = ''; $('#mail-update-li').removeClass('show')
206                 } else {
207                         $('#mail-update-li').addClass('show')
208                 }
209
210                 $('#mail-update-li').html(mail);
211
212                 $(".sidebar-group-li .notify").removeClass("show");
213                 $(data.groups).each(function(key, group) {
214                         var gid = group.id;
215                         var gcount = group.count;
216                         $(".group-"+gid+" .notify").addClass("show").text(gcount);
217                 });
218
219                 $(".forum-widget-entry .notify").removeClass("show");
220                 $(data.forums).each(function(key, forum) {
221                         var fid = forum.id;
222                         var fcount = forum.count;
223                         $(".forum-"+fid+" .notify").addClass("show").text(fcount);
224                 });
225
226                 if (data.notifications.length == 0) {
227                         $("#nav-notifications-menu").html(notifications_empty);
228                 } else {
229                         var nnm = $("#nav-notifications-menu");
230                         nnm.html(notifications_all + notifications_mark);
231
232                         var lastItemStorageKey = "notification-lastitem:" + localUser;
233                         var notification_lastitem = parseInt(localStorage.getItem(lastItemStorageKey));
234                         var notification_id = 0;
235
236                         // Insert notifs into the notifications-menu
237                         $(data.notifications).each(function(key, notif) {
238                                 var text = notif.message.format('<span class="contactname">' + notif.name + '</span>');
239                                 var contact = ('<a href="' + notif.url + '"><span class="contactname">' + notif.name + '</span></a>');
240                                 var seenclass = (notif.seen == 1) ? "notify-seen" : "notify-unseen";
241                                 var html = notifications_tpl.format(
242                                         notif.href,                     // {0}  // link to the source
243                                         notif.photo,                    // {1}  // photo of the contact
244                                         text,                           // {2}  // preformatted text (autor + text)
245                                         notif.date,                     // {3}  // date of notification (time ago)
246                                         seenclass,                      // {4}  // visited status of the notification
247                                         new Date(notif.timestamp*1000), // {5}  // date of notification
248                                         notif.url,                      // {6}  // profile url of the contact
249                                         notif.message.format(contact),  // {7}  // preformatted html (text including author profile url)
250                                         ''                              // {8}  // Deprecated
251                                 );
252                                 nnm.append(html);
253                         });
254
255                         // Desktop Notifications
256                         $(data.notifications.reverse()).each(function(key, e) {
257                                 notification_id = parseInt(e.timestamp);
258                                 if (notification_lastitem !== null && notification_id > notification_lastitem && Number(e.seen) === 0) {
259                                         if (getNotificationPermission() === "granted") {
260                                                 var notification = new Notification(document.title, {
261                                                                                   body: decodeHtml(e.message.replace('&rarr; ', '').format(e.name)),
262                                                                                   icon: e.photo,
263                                                                                 });
264                                                 notification['url'] = e.href;
265                                                 notification.addEventListener("click", function(ev) {
266                                                         window.location = ev.target.url;
267                                                 });
268                                         }
269                                 }
270
271                         });
272                         notification_lastitem = notification_id;
273                         localStorage.setItem(lastItemStorageKey, notification_lastitem)
274
275                         $("img[data-src]", nnm).each(function(i, el) {
276                                 // Add src attribute for images with a data-src attribute
277                                 // However, don't bother if the data-src attribute is empty, because
278                                 // an empty "src" tag for an image will cause some browsers
279                                 // to prefetch the root page of the Friendica hub, which will
280                                 // unnecessarily load an entire profile/ or network/ page
281                                 if ($(el).data("src") != '') {
282                                         $(el).attr('src', $(el).data("src"));
283                                 }
284                         });
285                 }
286
287                 var notif = data['notify'];
288                 if (notif > 0) {
289                         $("#nav-notifications-linkmenu").addClass("on");
290                 } else {
291                         $("#nav-notifications-linkmenu").removeClass("on");
292                 }
293
294                 $(data.sysmsgs.notice).each(function(key, message) {
295                         $.jGrowl(message, {sticky: true, theme: 'notice'});
296                 });
297                 $(data.sysmsgs.info).each(function(key, message) {
298                         $.jGrowl(message, {sticky: false, theme: 'info', life: 5000});
299                 });
300
301                 // Update the js scrollbars
302                 $('#nav-notifications-menu').perfectScrollbar('update');
303         });
304
305         // Asynchronous calls are deferred until the very end of the page load to ease on slower connections
306         window.addEventListener("load", function(){
307                 NavUpdate();
308                 acl.get(0, 100);
309         });
310
311         // Allow folks to stop the ajax page updates with the pause/break key
312         $(document).keydown(function(event) {
313                 if (event.keyCode == '8') {
314                         var target = event.target || event.srcElement;
315                         if (!/input|textarea/i.test(target.nodeName)) {
316                                 return false;
317                         }
318                 }
319
320                 if (event.keyCode == '19' || (event.ctrlKey && event.which == '32')) {
321                         event.preventDefault();
322                         if (stopped == false) {
323                                 stopped = true;
324                                 if (event.ctrlKey) {
325                                         totStopped = true;
326                                 }
327                                 $('#pause').html('<img src="images/pause.gif" alt="pause" style="border: 1px solid black;" />');
328                         } else {
329                                 unpause();
330                         }
331                 } else if (!totStopped) {
332                         unpause();
333                 }
334         });
335
336         // Scroll to the next/previous thread when pressing J and K
337         $(document).keydown(function (event) {
338                 var threads = $('.thread_level_1');
339                 if ((event.keyCode === 74 || event.keyCode === 75) && !$(event.target).is('textarea, input')) {
340                         var scrollTop = $(window).scrollTop();
341                         if (event.keyCode === 75) {
342                                 threads = $(threads.get().reverse());
343                         }
344                         threads.each(function(key, item) {
345                                 var comparison;
346                                 var top = $(item).offset().top - 100;
347                                 if (event.keyCode === 74) {
348                                         comparison = top > scrollTop + 1;
349                                 } else if (event.keyCode === 75) {
350                                         comparison = top < scrollTop - 1;
351                                 }
352                                 if (comparison) {
353                                         $('html, body').animate({scrollTop: top}, 200);
354                                         return false;
355                                 }
356                         });
357                 }
358         });
359
360         // Set an event listener for infinite scroll
361         if (typeof infinite_scroll !== 'undefined') {
362                 $(window).scroll(function(e) {
363                         if ($(document).height() != $(window).height()) {
364                                 // First method that is expected to work - but has problems with Chrome
365                                 if ($(window).scrollTop() > ($(document).height() - $(window).height() * 1.5))
366                                         loadScrollContent();
367                         } else {
368                                 // This method works with Chrome - but seems to be much slower in Firefox
369                                 if ($(window).scrollTop() > (($("section").height() + $("header").height() + $("footer").height()) - $(window).height() * 1.5)) {
370                                         loadScrollContent();
371                                 }
372                         }
373                 });
374         }
375 });
376
377 function NavUpdate() {
378         if (!stopped) {
379                 var pingCmd = 'ping?format=json' + ((localUser != 0) ? '&f=&uid=' + localUser : '');
380                 $.get(pingCmd, function(data) {
381                         if (data.result) {
382                                 // send nav-update event
383                                 $('nav').trigger('nav-update', data.result);
384
385                                 // start live update
386                                 ['network', 'profile', 'community', 'notes', 'display', 'contacts'].forEach(function (src) {
387                                         if ($('#live-' + src).length) {
388                                                 liveUpdate(src);
389                                         }
390                                 });
391                                 if ($('#live-photos').length) {
392                                         if (liking) {
393                                                 liking = 0;
394                                                 window.location.href = window.location.href;
395                                         }
396                                 }
397                         }
398                 });
399         }
400         timer = setTimeout(NavUpdate, updateInterval);
401 }
402
403 function updateConvItems(data) {
404         // add a new thread
405         $('.toplevel_item',data).each(function() {
406                 var ident = $(this).attr('id');
407
408                 // Add new top-level item.
409                 if ($('#' + ident).length == 0 && profile_page == 1) {
410                         $('#' + prev).after($(this));
411
412                 // Replace already existing thread.
413                 } else {
414                         // Find out if the hidden comments are open, so we can keep it that way
415                         // if a new comment has been posted
416                         var id = $('.hide-comments-total', this).attr('id');
417                         if (typeof id != 'undefined') {
418                                 id = id.split('-')[3];
419                                 var commentsOpen = $("#collapsed-comments-" + id).is(":visible");
420                         }
421
422                         $('#' + ident).replaceWith($(this));
423
424                         if (typeof id != 'undefined') {
425                                 if (commentsOpen) {
426                                         showHideComments(id);
427                                 }
428                         }
429                 }
430                 prev = ident;
431         });
432
433         $('.like-rotator').hide();
434         if (commentBusy) {
435                 commentBusy = false;
436                 $('body').css('cursor', 'auto');
437         }
438         /* autocomplete @nicknames */
439         $(".comment-edit-form  textarea").editor_autocomplete(baseurl+"/acl");
440         /* autocomplete bbcode */
441         $(".comment-edit-form  textarea").bbco_autocomplete('bbcode');
442 }
443
444 function liveUpdate(src) {
445         if ((src == null) || stopped || !profile_uid) {
446                 $('.like-rotator').hide(); return;
447         }
448
449         if (($('.comment-edit-text-full').length) || in_progress) {
450                 if (livetime) {
451                         clearTimeout(livetime);
452                 }
453                 livetime = setTimeout(function() {liveUpdate(src)}, 5000);
454                 return;
455         }
456
457         if (livetime != null) {
458                 livetime = null;
459         }
460         prev = 'live-' + src;
461
462         in_progress = true;
463
464         if ($(document).scrollTop() == 0) {
465                 force_update = true;
466         }
467
468         var orgHeight = $("section").height();
469
470         var udargs = ((netargs.length) ? '/' + netargs : '');
471         var update_url = 'update_' + src + udargs + '&p=' + profile_uid + '&page=' + profile_page + '&force=' + ((force_update) ? 1 : 0) + '&item=' + update_item;
472
473         $.get(update_url,function(data) {
474                 in_progress = false;
475                 force_update = false;
476                 update_item = 0;
477
478                 $('.wall-item-body', data).imagesLoaded(function() {
479                         updateConvItems(data);
480
481                         // Update the scroll position.
482                         $(window).scrollTop($(window).scrollTop() + $("section").height() - orgHeight);
483                 });
484
485                 callAddonHooks("postprocess_liveupdate");
486
487         });
488
489 }
490
491 function imgbright(node) {
492         $(node).removeClass("drophide").addClass("drop");
493 }
494
495 function imgdull(node) {
496         $(node).removeClass("drop").addClass("drophide");
497 }
498
499 // Since our ajax calls are asynchronous, we will give a few
500 // seconds for the first ajax call (setting like/dislike), then
501 // run the updater to pick up any changes and display on the page.
502 // The updater will turn any rotators off when it's done.
503 // This function will have returned long before any of these
504 // events have completed and therefore there won't be any
505 // visible feedback that anything changed without all this
506 // trickery. This still could cause confusion if the "like" ajax call
507 // is delayed and NavUpdate runs before it completes.
508
509 function dolike(ident,verb) {
510         unpause();
511         $('#like-rotator-' + ident.toString()).show();
512         $.get('like/' + ident.toString() + '?verb=' + verb, NavUpdate);
513         liking = 1;
514         force_update = true;
515         update_item = ident.toString();
516 }
517
518 function dosubthread(ident) {
519         unpause();
520         $('#like-rotator-' + ident.toString()).show();
521         $.get('subthread/' + ident.toString(), NavUpdate);
522         liking = 1;
523 }
524
525 function dostar(ident) {
526         ident = ident.toString();
527         $('#like-rotator-' + ident).show();
528         $.get('starred/' + ident, function(data) {
529                 if (data.match(/1/)) {
530                         $('#starred-' + ident).addClass('starred');
531                         $('#starred-' + ident).removeClass('unstarred');
532                         $('#star-' + ident).addClass('hidden');
533                         $('#unstar-' + ident).removeClass('hidden');
534                 } else {
535                         $('#starred-' + ident).addClass('unstarred');
536                         $('#starred-' + ident).removeClass('starred');
537                         $('#star-' + ident).removeClass('hidden');
538                         $('#unstar-' + ident).addClass('hidden');
539                 }
540                 $('#like-rotator-' + ident).hide();
541         });
542 }
543
544 function doignore(ident) {
545         ident = ident.toString();
546         $('#like-rotator-' + ident).show();
547         $.get('ignored/' + ident, function(data) {
548                 if (data.match(/1/)) {
549                         $('#ignored-' + ident).addClass('ignored');
550                         $('#ignored-' + ident).removeClass('unignored');
551                         $('#ignore-' + ident).addClass('hidden');
552                         $('#unignore-' + ident).removeClass('hidden');
553                 } else {
554                         $('#ignored-' + ident).addClass('unignored');
555                         $('#ignored-' + ident).removeClass('ignored');
556                         $('#ignore-' + ident).removeClass('hidden');
557                         $('#unignore-' + ident).addClass('hidden');
558                 }
559                 $('#like-rotator-' + ident).hide();
560         });
561 }
562
563 function getPosition(e) {
564         var cursor = {x:0, y:0};
565
566         if (e.pageX || e.pageY) {
567                 cursor.x = e.pageX;
568                 cursor.y = e.pageY;
569         } else {
570                 if (e.clientX || e.clientY) {
571                         cursor.x = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
572                         cursor.y = e.clientY + (document.documentElement.scrollTop  || document.body.scrollTop)  - document.documentElement.clientTop;
573                 } else if (e.x || e.y) {
574                         cursor.x = e.x;
575                         cursor.y = e.y;
576                 }
577         }
578         return cursor;
579 }
580
581 var lockvisible = false;
582
583 function lockview(event,id) {
584         event = event || window.event;
585         cursor = getPosition(event);
586         if (lockvisible) {
587                 lockviewhide();
588         } else {
589                 lockvisible = true;
590                 $.get('lockview/' + id, function(data) {
591                         $('#panel').html(data);
592                         $('#panel').css({'left': cursor.x + 5 , 'top': cursor.y + 5});
593                         $('#panel').show();
594                 });
595         }
596 }
597
598 function lockviewhide() {
599         lockvisible = false;
600         $('#panel').hide();
601 }
602
603 function post_comment(id) {
604         unpause();
605         commentBusy = true;
606         $('body').css('cursor', 'wait');
607         $("#comment-preview-inp-" + id).val("0");
608         $.post(
609                 "item",
610                 $("#comment-edit-form-" + id).serialize(),
611                 function(data) {
612                         if (data.success) {
613                                 $("#comment-edit-wrapper-" + id).hide();
614                                 $("#comment-edit-text-" + id).val('');
615                                 var tarea = document.getElementById("comment-edit-text-" + id);
616                                 if (tarea) {
617                                         commentClose(tarea,id);
618                                 }
619                                 if (timer) {
620                                         clearTimeout(timer);
621                                 }
622                                 timer = setTimeout(NavUpdate,10);
623                                 force_update = true;
624                                 update_item = id;
625                         }
626                         if (data.reload) {
627                                 window.location.href=data.reload;
628                         }
629                 },
630                 "json"
631         );
632         return false;
633 }
634
635 function preview_comment(id) {
636         $("#comment-preview-inp-" + id).val("1");
637         $("#comment-edit-preview-" + id).show();
638         $.post(
639                 "item",
640                 $("#comment-edit-form-" + id).serialize(),
641                 function(data) {
642                         if (data.preview) {
643                                 $("#comment-edit-preview-" + id).html(data.preview);
644                                 $("#comment-edit-preview-" + id + " a").click(function() {return false;});
645                         }
646                 },
647                 "json"
648         );
649         return true;
650 }
651
652 function showHideComments(id) {
653         if ($("#collapsed-comments-" + id).is(":visible")) {
654                 $("#collapsed-comments-" + id).hide();
655                 $("#hide-comments-" + id).html(window.showMore);
656         } else {
657                 $("#collapsed-comments-" + id).show();
658                 $("#hide-comments-" + id).html(window.showFewer);
659         }
660 }
661
662 function preview_post() {
663         $("#jot-preview").val("1");
664         $("#jot-preview-content").show();
665         $.post(
666                 "item",
667                 $("#profile-jot-form").serialize(),
668                 function(data) {
669                         if (data.preview) {
670                                 $("#jot-preview-content").html(data.preview);
671                                 $("#jot-preview-content" + " a").click(function() {return false;});
672                         }
673                 },
674                 "json"
675         );
676         $("#jot-preview").val("0");
677         return true;
678 }
679
680 function unpause() {
681         // unpause auto reloads if they are currently stopped
682         totStopped = false;
683         stopped = false;
684         $('#pause').html('');
685 }
686
687 // load more network content (used for infinite scroll)
688 function loadScrollContent() {
689         if (lockLoadContent) {
690                 return;
691         }
692         lockLoadContent = true;
693
694         $("#scroll-loader").fadeIn('normal');
695
696         // the page number to load is one higher than the actual
697         // page number
698         infinite_scroll.pageno+=1;
699
700         match = $("span.received").last();
701         if (match.length > 0) {
702                 received = match[0].innerHTML;
703         } else {
704                 received = "0000-00-00 00:00:00";
705         }
706
707         match = $("span.created").last();
708         if (match.length > 0) {
709                 created = match[0].innerHTML;
710         } else {
711                 created = "0000-00-00 00:00:00";
712         }
713
714         match = $("span.commented").last();
715         if (match.length > 0) {
716                 commented = match[0].innerHTML;
717         } else {
718                 commented = "0000-00-00 00:00:00";
719         }
720
721         match = $("span.id").last();
722         if (match.length > 0) {
723                 id = match[0].innerHTML;
724         } else {
725                 id = "0";
726         }
727
728         // get the raw content from the next page and insert this content
729         // right before "#conversation-end"
730         $.get('network?mode=raw' + infinite_scroll.reload_uri + '&last_received=' + received + '&last_commented=' + commented + '&last_created=' + created + '&last_id=' + id + '&page=' + infinite_scroll.pageno, function(data) {
731                 $("#scroll-loader").hide();
732                 if ($(data).length > 0) {
733                         $(data).insertBefore('#conversation-end');
734                         lockLoadContent = false;
735                 } else {
736                         $("#scroll-end").fadeIn('normal');
737                 }
738         });
739 }
740
741 function bin2hex(s) {
742         // Converts the binary representation of data to hex
743         //
744         // version: 812.316
745         // discuss at: http://phpjs.org/functions/bin2hex
746         // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
747         // +   bugfixed by: Onno Marsman
748         // +   bugfixed by: Linuxworld
749         // *     example 1: bin2hex('Kev');
750         // *     returns 1: '4b6576'
751         // *     example 2: bin2hex(String.fromCharCode(0x00));
752         // *     returns 2: '00'
753         var v,i, f = 0, a = [];
754         s += '';
755         f = s.length;
756
757         for (i = 0; i<f; i++) {
758                 a[i] = s.charCodeAt(i).toString(16).replace(/^([\da-f])$/,"0$1");
759         }
760
761         return a.join('');
762 }
763
764 function groupChangeMember(gid, cid, sec_token) {
765         $('body .fakelink').css('cursor', 'wait');
766         $.get('group/' + gid + '/' + cid + "?t=" + sec_token, function(data) {
767                         $('#group-update-wrapper').html(data);
768                         $('body .fakelink').css('cursor', 'auto');
769         });
770 }
771
772 function profChangeMember(gid,cid) {
773         $('body .fakelink').css('cursor', 'wait');
774         $.get('profperm/' + gid + '/' + cid, function(data) {
775                         $('#prof-update-wrapper').html(data);
776                         $('body .fakelink').css('cursor', 'auto');
777         });
778 }
779
780 function contactgroupChangeMember(gid,cid) {
781         $('body').css('cursor', 'wait');
782         $.get('contactgroup/' + gid + '/' + cid, function(data) {
783                         $('body').css('cursor', 'auto');
784         });
785 }
786
787 function checkboxhighlight(box) {
788         if ($(box).is(':checked')) {
789                 $(box).addClass('checkeditem');
790         } else {
791                 $(box).removeClass('checkeditem');
792         }
793 }
794
795 function notifyMarkAll() {
796         $.get('notify/mark/all', function(data) {
797                 if (timer) {
798                         clearTimeout(timer);
799                 }
800                 timer = setTimeout(NavUpdate,1000);
801                 force_update = true;
802         });
803 }
804
805 /**
806  * sprintf in javascript
807  *      "{0} and {1}".format('zero','uno');
808  **/
809 String.prototype.format = function() {
810         var formatted = this;
811         for (var i = 0; i < arguments.length; i++) {
812                 var regexp = new RegExp('\\{'+i+'\\}', 'gi');
813                 formatted = formatted.replace(regexp, arguments[i]);
814         }
815         return formatted;
816 };
817 // Array Remove
818 Array.prototype.remove = function(item) {
819         to=undefined; from=this.indexOf(item);
820         var rest = this.slice((to || from) + 1 || this.length);
821         this.length = from < 0 ? this.length + from : from;
822         return this.push.apply(this, rest);
823 };
824
825 function previewTheme(elm) {
826         theme = $(elm).val();
827         $.getJSON('pretheme?f=&theme=' + theme,function(data) {
828                         $('#theme-preview').html('<div id="theme-desc">' + data.desc + '</div><div id="theme-version">' + data.version + '</div><div id="theme-credits">' + data.credits + '</div><a href="' + data.img + '"><img src="' + data.img + '" width="320" height="240" alt="' + theme + '" /></a>');
829         });
830
831 }
832
833 // notification permission settings in localstorage
834 // set by settings page
835 function getNotificationPermission() {
836         if (window["Notification"] === undefined) {
837                 return null;
838         }
839
840         if (Notification.permission === 'granted') {
841                 var val = localStorage.getItem('notification-permissions');
842                 if (val === null) {
843                         return 'denied';
844                 }
845                 return val;
846         } else {
847                 return Notification.permission;
848         }
849 }
850
851 /**
852  * Show a dialog loaded from an url
853  * By defaults this load the url in an iframe in colorbox
854  * Themes can overwrite `show()` function to personalize it
855  */
856 var Dialog = {
857         /**
858          * Show the dialog
859          *
860          * @param string url
861          * @return object colorbox
862          */
863         show : function (url) {
864                 var size = Dialog._get_size();
865                 return $.colorbox({href: url, iframe:true,innerWidth: size.width+'px',innerHeight: size.height+'px'})
866         },
867
868         /**
869          * Show the Image browser dialog
870          *
871          * @param string name
872          * @param string id (optional)
873          * @return object
874          *
875          * The name will be used to build the event name
876          * fired by image browser dialog when the user select
877          * an image. The optional id will be passed as argument
878          * to the event handler
879          */
880         doImageBrowser : function (name, id) {
881                 var url = Dialog._get_url("image",name,id);
882                 return Dialog.show(url);
883         },
884
885         /**
886          * Show the File browser dialog
887          *
888          * @param string name
889          * @param string id (optional)
890          * @return object
891          *
892          * The name will be used to build the event name
893          * fired by file browser dialog when the user select
894          * a file. The optional id will be passed as argument
895          * to the event handler
896          */
897         doFileBrowser : function (name, id) {
898                 var url = Dialog._get_url("file",name,id);
899                 return Dialog.show(url);
900         },
901
902         _get_url : function(type, name, id) {
903                 var hash = name;
904                 if (id !== undefined) {
905                         hash = hash + "-" + id;
906                 }
907                 return baseurl + "/fbrowser/"+type+"/?mode=minimal#"+hash;
908         },
909
910         _get_size: function() {
911                 return {
912                         width: window.innerWidth-50,
913                         height: window.innerHeight-100
914                 };
915         }
916 }