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