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