]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - js/util.js
Merge branch 'swat0' into 0.9.x
[quix0rs-gnu-social.git] / js / util.js
1 /*
2  * StatusNet - a distributed open-source microblogging tool
3  * Copyright (C) 2008, StatusNet, Inc.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @category  UI interaction
19  * @package   StatusNet
20  * @author    Sarven Capadisli <csarven@status.net>
21  * @author    Evan Prodromou <evan@status.net>
22  * @copyright 2009 StatusNet, Inc.
23  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
24  * @link      http://status.net/
25  */
26
27 var SN = { // StatusNet
28     C: { // Config
29         I: { // Init
30             CounterBlackout: false,
31             MaxLength: 140,
32             PatternUsername: /^[0-9a-zA-Z\-_.]*$/,
33             HTTP20x30x: [200, 201, 202, 203, 204, 205, 206, 300, 301, 302, 303, 304, 305, 306, 307]
34         },
35
36         S: { // Selector
37             Disabled: 'disabled',
38             Warning: 'warning',
39             Error: 'error',
40             Success: 'success',
41             Processing: 'processing',
42             CommandResult: 'command_result',
43             FormNotice: 'form_notice',
44             NoticeDataText: 'notice_data-text',
45             NoticeTextCount: 'notice_text-count',
46             NoticeInReplyTo: 'notice_in-reply-to',
47             NoticeDataAttach: 'notice_data-attach',
48             NoticeDataAttachSelected: 'notice_data-attach_selected',
49             NoticeActionSubmit: 'notice_action-submit',
50             NoticeLat: 'notice_data-lat',
51             NoticeLon: 'notice_data-lon',
52             NoticeLocationId: 'notice_data-location_id',
53             NoticeLocationNs: 'notice_data-location_ns',
54             NoticeGeoName: 'notice_data-geo_name',
55             NoticeDataGeo: 'notice_data-geo',
56             NoticeDataGeoCookie: 'NoticeDataGeo',
57             NoticeDataGeoSelected: 'notice_data-geo_selected',
58             StatusNetInstance:'StatusNetInstance'
59         }
60     },
61
62     U: { // Utils
63         FormNoticeEnhancements: function(form) {
64             if (jQuery.data(form[0], 'ElementData') === undefined) {
65                 MaxLength = form.find('#'+SN.C.S.NoticeTextCount).text();
66                 if (typeof(MaxLength) == 'undefined') {
67                      MaxLength = SN.C.I.MaxLength;
68                 }
69                 jQuery.data(form[0], 'ElementData', {MaxLength:MaxLength});
70
71                 SN.U.Counter(form);
72
73                 NDT = form.find('#'+SN.C.S.NoticeDataText);
74
75                 NDT.bind('keyup', function(e) {
76                     SN.U.Counter(form);
77                 });
78
79                 NDT.bind('keydown', function(e) {
80                     SN.U.SubmitOnReturn(e, form);
81                 });
82             }
83             else {
84                 form.find('#'+SN.C.S.NoticeTextCount).text(jQuery.data(form[0], 'ElementData').MaxLength);
85             }
86
87             if ($('body')[0].id != 'conversation' && window.location.hash.length === 0 && $(window).scrollTop() == 0) {
88                 form.find('textarea').focus();
89             }
90         },
91
92         SubmitOnReturn: function(event, el) {
93             if (event.keyCode == 13 || event.keyCode == 10) {
94                 el.submit();
95                 event.preventDefault();
96                 event.stopPropagation();
97                 $('#'+el[0].id+' #'+SN.C.S.NoticeDataText).blur();
98                 $('body').focus();
99                 return false;
100             }
101             return true;
102         },
103
104         Counter: function(form) {
105             SN.C.I.FormNoticeCurrent = form;
106
107             var MaxLength = jQuery.data(form[0], 'ElementData').MaxLength;
108
109             if (MaxLength <= 0) {
110                 return;
111             }
112
113             var remaining = MaxLength - SN.U.CharacterCount(form);
114             var counter = form.find('#'+SN.C.S.NoticeTextCount);
115
116             if (remaining.toString() != counter.text()) {
117                 if (!SN.C.I.CounterBlackout || remaining === 0) {
118                     if (counter.text() != String(remaining)) {
119                         counter.text(remaining);
120                     }
121                     if (remaining < 0) {
122                         form.addClass(SN.C.S.Warning);
123                     } else {
124                         form.removeClass(SN.C.S.Warning);
125                     }
126                     // Skip updates for the next 500ms.
127                     // On slower hardware, updating on every keypress is unpleasant.
128                     if (!SN.C.I.CounterBlackout) {
129                         SN.C.I.CounterBlackout = true;
130                         SN.C.I.FormNoticeCurrent = form;
131                         window.setTimeout("SN.U.ClearCounterBlackout(SN.C.I.FormNoticeCurrent);", 500);
132                     }
133                 }
134             }
135         },
136
137         CharacterCount: function(form) {
138             return form.find('#'+SN.C.S.NoticeDataText).val().length;
139         },
140
141         ClearCounterBlackout: function(form) {
142             // Allow keyup events to poke the counter again
143             SN.C.I.CounterBlackout = false;
144             // Check if the string changed since we last looked
145             SN.U.Counter(form);
146         },
147
148         FormXHR: function(form) {
149             $.ajax({
150                 type: 'POST',
151                 dataType: 'xml',
152                 url: form.attr('action'),
153                 data: form.serialize() + '&ajax=1',
154                 beforeSend: function(xhr) {
155                     form
156                         .addClass(SN.C.S.Processing)
157                         .find('.submit')
158                             .addClass(SN.C.S.Disabled)
159                             .attr(SN.C.S.Disabled, SN.C.S.Disabled);
160                 },
161                 error: function (xhr, textStatus, errorThrown) {
162                     alert(errorThrown || textStatus);
163                 },
164                 success: function(data, textStatus) {
165                     if (typeof($('form', data)[0]) != 'undefined') {
166                         form_new = document._importNode($('form', data)[0], true);
167                         form.replaceWith(form_new);
168                     }
169                     else {
170                         form.replaceWith(document._importNode($('p', data)[0], true));
171                     }
172                 }
173             });
174         },
175
176         FormNoticeXHR: function(form) {
177             SN.C.I.NoticeDataGeo = {};
178             form.append('<input type="hidden" name="ajax" value="1"/>');
179             form.ajaxForm({
180                 dataType: 'xml',
181                 timeout: '60000',
182                 beforeSend: function(formData) {
183                     if (form.find('#'+SN.C.S.NoticeDataText)[0].value.length === 0) {
184                         form.addClass(SN.C.S.Warning);
185                         return false;
186                     }
187                     form
188                         .addClass(SN.C.S.Processing)
189                         .find('#'+SN.C.S.NoticeActionSubmit)
190                             .addClass(SN.C.S.Disabled)
191                             .attr(SN.C.S.Disabled, SN.C.S.Disabled);
192
193                     SN.C.I.NoticeDataGeo.NLat = $('#'+SN.C.S.NoticeLat).val();
194                     SN.C.I.NoticeDataGeo.NLon = $('#'+SN.C.S.NoticeLon).val();
195                     SN.C.I.NoticeDataGeo.NLNS = $('#'+SN.C.S.NoticeLocationNs).val();
196                     SN.C.I.NoticeDataGeo.NLID = $('#'+SN.C.S.NoticeLocationId).val();
197                     SN.C.I.NoticeDataGeo.NDG = $('#'+SN.C.S.NoticeDataGeo).attr('checked');
198
199                     cookieValue = $.cookie(SN.C.S.NoticeDataGeoCookie);
200
201                     if (cookieValue !== null && cookieValue != 'disabled') {
202                         cookieValue = JSON.parse(cookieValue);
203                         SN.C.I.NoticeDataGeo.NLat = $('#'+SN.C.S.NoticeLat).val(cookieValue.NLat).val();
204                         SN.C.I.NoticeDataGeo.NLon = $('#'+SN.C.S.NoticeLon).val(cookieValue.NLon).val();
205                         if ($('#'+SN.C.S.NoticeLocationNs).val(cookieValue.NLNS)) {
206                             SN.C.I.NoticeDataGeo.NLNS = $('#'+SN.C.S.NoticeLocationNs).val(cookieValue.NLNS).val();
207                             SN.C.I.NoticeDataGeo.NLID = $('#'+SN.C.S.NoticeLocationId).val(cookieValue.NLID).val();
208                         }
209                     }
210                     if (cookieValue == 'disabled') {
211                         SN.C.I.NoticeDataGeo.NDG = $('#'+SN.C.S.NoticeDataGeo).attr('checked', false).attr('checked');
212                     }
213                     else {
214                         SN.C.I.NoticeDataGeo.NDG = $('#'+SN.C.S.NoticeDataGeo).attr('checked', true).attr('checked');
215                     }
216
217                     return true;
218                 },
219                 error: function (xhr, textStatus, errorThrown) {
220                     form
221                         .removeClass(SN.C.S.Processing)
222                         .find('#'+SN.C.S.NoticeActionSubmit)
223                             .removeClass(SN.C.S.Disabled)
224                             .removeAttr(SN.C.S.Disabled, SN.C.S.Disabled);
225                     form.find('.form_response').remove();
226                     if (textStatus == 'timeout') {
227                         form.append('<p class="form_response error">Sorry! We had trouble sending your notice. The servers are overloaded. Please try again, and contact the site administrator if this problem persists.</p>');
228                     }
229                     else {
230                         if ($('.'+SN.C.S.Error, xhr.responseXML).length > 0) {
231                             form.append(document._importNode($('.'+SN.C.S.Error, xhr.responseXML)[0], true));
232                         }
233                         else {
234                             if (parseInt(xhr.status) === 0 || jQuery.inArray(parseInt(xhr.status), SN.C.I.HTTP20x30x) >= 0) {
235                                 form
236                                     .resetForm()
237                                     .find('#'+SN.C.S.NoticeDataAttachSelected).remove();
238                                 SN.U.FormNoticeEnhancements(form);
239                             }
240                             else {
241                                 form.append('<p class="form_response error">(Sorry! We had trouble sending your notice ('+xhr.status+' '+xhr.statusText+'). Please report the problem to the site administrator if this happens again.</p>');
242                             }
243                         }
244                     }
245                 },
246                 success: function(data, textStatus) {
247                     form.find('.form_response').remove();
248                     var result;
249                     if ($('#'+SN.C.S.Error, data).length > 0) {
250                         result = document._importNode($('p', data)[0], true);
251                         result = result.textContent || result.innerHTML;
252                         form.append('<p class="form_response error">'+result+'</p>');
253                     }
254                     else {
255                         if($('body')[0].id == 'bookmarklet') {
256                             self.close();
257                         }
258
259                         if ($('#'+SN.C.S.CommandResult, data).length > 0) {
260                             result = document._importNode($('p', data)[0], true);
261                             result = result.textContent || result.innerHTML;
262                             form.append('<p class="form_response success">'+result+'</p>');
263                         }
264                         else {
265                             // New notice post was successful. If on our timeline, show it!
266                             var notice = document._importNode($('li', data)[0], true);
267                             var notices = $('#notices_primary .notices');
268                             if (notices.length > 0 && SN.U.belongsOnTimeline(notice)) {
269                                 if ($('#'+notice.id).length === 0) {
270                                     var notice_irt_value = $('#'+SN.C.S.NoticeInReplyTo).val();
271                                     var notice_irt = '#notices_primary #notice-'+notice_irt_value;
272                                     if($('body')[0].id == 'conversation') {
273                                         if(notice_irt_value.length > 0 && $(notice_irt+' .notices').length < 1) {
274                                             $(notice_irt).append('<ul class="notices"></ul>');
275                                         }
276                                         $($(notice_irt+' .notices')[0]).append(notice);
277                                     }
278                                     else {
279                                         notices.prepend(notice);
280                                     }
281                                     $('#'+notice.id)
282                                         .css({display:'none'})
283                                         .fadeIn(2500);
284                                     SN.U.NoticeWithAttachment($('#'+notice.id));
285                                     SN.U.NoticeReplyTo($('#'+notice.id));
286                                 }
287                             }
288                             else {
289                                 // Not on a timeline that this belongs on?
290                                 // Just show a success message.
291                                 result = document._importNode($('title', data)[0], true);
292                                 result_title = result.textContent || result.innerHTML;
293                                 form.append('<p class="form_response success">'+result_title+'</p>');
294                             }
295                         }
296                         form.resetForm();
297                         form.find('#'+SN.C.S.NoticeInReplyTo).val('');
298                         form.find('#'+SN.C.S.NoticeDataAttachSelected).remove();
299                         SN.U.FormNoticeEnhancements(form);
300                     }
301                 },
302                 complete: function(xhr, textStatus) {
303                     form
304                         .removeClass(SN.C.S.Processing)
305                         .find('#'+SN.C.S.NoticeActionSubmit)
306                             .removeAttr(SN.C.S.Disabled)
307                             .removeClass(SN.C.S.Disabled);
308
309                     $('#'+SN.C.S.NoticeLat).val(SN.C.I.NoticeDataGeo.NLat);
310                     $('#'+SN.C.S.NoticeLon).val(SN.C.I.NoticeDataGeo.NLon);
311                     if ($('#'+SN.C.S.NoticeLocationNs)) {
312                         $('#'+SN.C.S.NoticeLocationNs).val(SN.C.I.NoticeDataGeo.NLNS);
313                         $('#'+SN.C.S.NoticeLocationId).val(SN.C.I.NoticeDataGeo.NLID);
314                     }
315                     $('#'+SN.C.S.NoticeDataGeo).attr('checked', SN.C.I.NoticeDataGeo.NDG);
316                 }
317             });
318         },
319
320         NoticeReply: function() {
321             if ($('#'+SN.C.S.NoticeDataText).length > 0 && $('#content .notice_reply').length > 0) {
322                 $('#content .notice').each(function() { SN.U.NoticeReplyTo($(this)); });
323             }
324         },
325
326         NoticeReplyTo: function(notice) {
327             notice.find('.notice_reply').live('click', function() {
328                 var nickname = ($('.author .nickname', notice).length > 0) ? $($('.author .nickname', notice)[0]) : $('.author .nickname.uid');
329                 SN.U.NoticeReplySet(nickname.text(), $($('.notice_id', notice)[0]).text());
330                 return false;
331             });
332         },
333
334         NoticeReplySet: function(nick,id) {
335             if (nick.match(SN.C.I.PatternUsername)) {
336                 var text = $('#'+SN.C.S.NoticeDataText);
337                 if (text.length > 0) {
338                     replyto = '@' + nick + ' ';
339                     text.val(replyto + text.val().replace(RegExp(replyto, 'i'), ''));
340                     $('#'+SN.C.S.FormNotice+' #'+SN.C.S.NoticeInReplyTo).val(id);
341
342                     text[0].focus();
343                     if (text[0].setSelectionRange) {
344                         var len = text.val().length;
345                         text[0].setSelectionRange(len,len);
346                     }
347                 }
348             }
349         },
350
351         NoticeFavor: function() {
352             $('.form_favor').live('click', function() { SN.U.FormXHR($(this)); return false; });
353             $('.form_disfavor').live('click', function() { SN.U.FormXHR($(this)); return false; });
354         },
355
356         NoticeRepeat: function() {
357             $('.form_repeat').live('click', function(e) {
358                 e.preventDefault();
359
360                 SN.U.NoticeRepeatConfirmation($(this));
361                 return false;
362             });
363         },
364
365         NoticeRepeatConfirmation: function(form) {
366             var submit_i = form.find('.submit');
367
368             var submit = submit_i.clone();
369             submit
370                 .addClass('submit_dialogbox')
371                 .removeClass('submit');
372             form.append(submit);
373             submit.bind('click', function() { SN.U.FormXHR(form); return false; });
374
375             submit_i.hide();
376
377             form
378                 .addClass('dialogbox')
379                 .append('<button class="close">&#215;</button>')
380                 .closest('.notice-options')
381                     .addClass('opaque');
382
383             form.find('button.close').click(function(){
384                 $(this).remove();
385
386                 form
387                     .removeClass('dialogbox')
388                     .closest('.notice-options')
389                         .removeClass('opaque');
390
391                 form.find('.submit_dialogbox').remove();
392                 form.find('.submit').show();
393
394                 return false;
395             });
396         },
397
398         NoticeAttachments: function() {
399             $('.notice a.attachment').each(function() {
400                 SN.U.NoticeWithAttachment($(this).closest('.notice'));
401             });
402         },
403
404         NoticeWithAttachment: function(notice) {
405             if (notice.find('.attachment').length === 0) {
406                 return;
407             }
408
409             var attachment_more = notice.find('.attachment.more');
410             if (attachment_more.length > 0) {
411                 $(attachment_more[0]).click(function() {
412                     var m = $(this);
413                     m.addClass(SN.C.S.Processing);
414                     $.get(m.attr('href')+'/ajax', null, function(data) {
415                         m.parent('.entry-content').html($(data).find('#attachment_view .entry-content').html());
416                     });
417
418                     return false;
419                 });
420             }
421             else {
422                 $.fn.jOverlay.options = {
423                     method : 'GET',
424                     data : '',
425                     url : '',
426                     color : '#000',
427                     opacity : '0.6',
428                     zIndex : 9999,
429                     center : false,
430                     imgLoading : $('address .url')[0].href+'theme/base/images/illustrations/illu_progress_loading-01.gif',
431                     bgClickToClose : true,
432                     success : function() {
433                         $('#jOverlayContent').append('<button class="close">&#215;</button>');
434                         $('#jOverlayContent button').click($.closeOverlay);
435                     },
436                     timeout : 0,
437                     autoHide : true,
438                     css : {'max-width':'542px', 'top':'5%', 'left':'32.5%'}
439                 };
440
441                 notice.find('a.attachment').click(function() {
442                     var attachId = ($(this).attr('id').substring('attachment'.length + 1));
443                     if (attachId) {
444                         $().jOverlay({url: $('address .url')[0].href+'attachment/' + attachId + '/ajax'});
445                         return false;
446                     }
447                 });
448
449                 if ($('#shownotice').length == 0) {
450                     var t;
451                     notice.find('a.thumbnail').hover(
452                         function() {
453                             var anchor = $(this);
454                             $('a.thumbnail').children('img').hide();
455                             anchor.closest(".entry-title").addClass('ov');
456
457                             if (anchor.children('img').length === 0) {
458                                 t = setTimeout(function() {
459                                     $.get($('address .url')[0].href+'attachment/' + (anchor.attr('id').substring('attachment'.length + 1)) + '/thumbnail', null, function(data) {
460                                         anchor.append(data);
461                                     });
462                                 }, 500);
463                             }
464                             else {
465                                 anchor.children('img').show();
466                             }
467                         },
468                         function() {
469                             clearTimeout(t);
470                             $('a.thumbnail').children('img').hide();
471                             $(this).closest('.entry-title').removeClass('ov');
472                         }
473                     );
474                 }
475             }
476         },
477
478         NoticeDataAttach: function() {
479             NDA = $('#'+SN.C.S.NoticeDataAttach);
480             NDA.change(function() {
481                 S = '<div id="'+SN.C.S.NoticeDataAttachSelected+'" class="'+SN.C.S.Success+'"><code>'+$(this).val()+'</code> <button class="close">&#215;</button></div>';
482                 NDAS = $('#'+SN.C.S.NoticeDataAttachSelected);
483                 if (NDAS.length > 0) {
484                     NDAS.replaceWith(S);
485                 }
486                 else {
487                     $('#'+SN.C.S.FormNotice).append(S);
488                 }
489                 $('#'+SN.C.S.NoticeDataAttachSelected+' button').click(function(){
490                     $('#'+SN.C.S.NoticeDataAttachSelected).remove();
491                     NDA.val('');
492
493                     return false;
494                 });
495             });
496         },
497
498         NoticeLocationAttach: function() {
499             var NLat = $('#'+SN.C.S.NoticeLat).val();
500             var NLon = $('#'+SN.C.S.NoticeLon).val();
501             var NLNS = $('#'+SN.C.S.NoticeLocationNs).val();
502             var NLID = $('#'+SN.C.S.NoticeLocationId).val();
503             var NLN = $('#'+SN.C.S.NoticeGeoName).text();
504             var NDGe = $('#'+SN.C.S.NoticeDataGeo);
505
506             function removeNoticeDataGeo() {
507                 $('label[for='+SN.C.S.NoticeDataGeo+']')
508                     .attr('title', jQuery.trim($('label[for='+SN.C.S.NoticeDataGeo+']').text()))
509                     .removeClass('checked');
510
511                 $('#'+SN.C.S.NoticeLat).val('');
512                 $('#'+SN.C.S.NoticeLon).val('');
513                 $('#'+SN.C.S.NoticeLocationNs).val('');
514                 $('#'+SN.C.S.NoticeLocationId).val('');
515                 $('#'+SN.C.S.NoticeDataGeo).attr('checked', false);
516
517                 $.cookie(SN.C.S.NoticeDataGeoCookie, 'disabled', { path: '/' });
518             }
519
520             function getJSONgeocodeURL(geocodeURL, data) {
521                 $.getJSON(geocodeURL, data, function(location) {
522                     var lns, lid;
523
524                     if (typeof(location.location_ns) != 'undefined') {
525                         $('#'+SN.C.S.NoticeLocationNs).val(location.location_ns);
526                         lns = location.location_ns;
527                     }
528
529                     if (typeof(location.location_id) != 'undefined') {
530                         $('#'+SN.C.S.NoticeLocationId).val(location.location_id);
531                         lid = location.location_id;
532                     }
533
534                     if (typeof(location.name) == 'undefined') {
535                         NLN_text = data.lat + ';' + data.lon;
536                     }
537                     else {
538                         NLN_text = location.name;
539                     }
540
541                     $('label[for='+SN.C.S.NoticeDataGeo+']')
542                         .attr('title', NoticeDataGeo_text.ShareDisable + ' (' + NLN_text + ')');
543
544                     $('#'+SN.C.S.NoticeLat).val(data.lat);
545                     $('#'+SN.C.S.NoticeLon).val(data.lon);
546                     $('#'+SN.C.S.NoticeLocationNs).val(lns);
547                     $('#'+SN.C.S.NoticeLocationId).val(lid);
548                     $('#'+SN.C.S.NoticeDataGeo).attr('checked', true);
549
550                     var cookieValue = {
551                         NLat: data.lat,
552                         NLon: data.lon,
553                         NLNS: lns,
554                         NLID: lid,
555                         NLN: NLN_text,
556                         NLNU: location.url,
557                         NDG: true
558                     };
559
560                     $.cookie(SN.C.S.NoticeDataGeoCookie, JSON.stringify(cookieValue), { path: '/' });
561                 });
562             }
563
564             if (NDGe.length > 0) {
565                 if ($.cookie(SN.C.S.NoticeDataGeoCookie) == 'disabled') {
566                     NDGe.attr('checked', false);
567                 }
568                 else {
569                     NDGe.attr('checked', true);
570                 }
571
572                 var NGW = $('#notice_data-geo_wrap');
573                 var geocodeURL = NGW.attr('title');
574                 NGW.removeAttr('title');
575
576                 $('label[for='+SN.C.S.NoticeDataGeo+']')
577                     .attr('title', jQuery.trim($('label[for='+SN.C.S.NoticeDataGeo+']').text()));
578
579                 NDGe.change(function() {
580                     if ($('#'+SN.C.S.NoticeDataGeo).attr('checked') === true || $.cookie(SN.C.S.NoticeDataGeoCookie) === null) {
581                         $('label[for='+SN.C.S.NoticeDataGeo+']')
582                             .attr('title', NoticeDataGeo_text.ShareDisable)
583                             .addClass('checked');
584
585                         if ($.cookie(SN.C.S.NoticeDataGeoCookie) === null || $.cookie(SN.C.S.NoticeDataGeoCookie) == 'disabled') {
586                             if (navigator.geolocation) {
587                                 navigator.geolocation.getCurrentPosition(
588                                     function(position) {
589                                         $('#'+SN.C.S.NoticeLat).val(position.coords.latitude);
590                                         $('#'+SN.C.S.NoticeLon).val(position.coords.longitude);
591
592                                         var data = {
593                                             lat: position.coords.latitude,
594                                             lon: position.coords.longitude,
595                                             token: $('#token').val()
596                                         };
597
598                                         getJSONgeocodeURL(geocodeURL, data);
599                                     },
600
601                                     function(error) {
602                                         switch(error.code) {
603                                             case error.PERMISSION_DENIED:
604                                                 removeNoticeDataGeo();
605                                                 break;
606                                             case error.TIMEOUT:
607                                                 $('#'+SN.C.S.NoticeDataGeo).attr('checked', false);
608                                                 break;
609                                         }
610                                     },
611
612                                     {
613                                         timeout: 10000
614                                     }
615                                 );
616                             }
617                             else {
618                                 if (NLat.length > 0 && NLon.length > 0) {
619                                     var data = {
620                                         lat: NLat,
621                                         lon: NLon,
622                                         token: $('#token').val()
623                                     };
624
625                                     getJSONgeocodeURL(geocodeURL, data);
626                                 }
627                                 else {
628                                     removeNoticeDataGeo();
629                                     $('#'+SN.C.S.NoticeDataGeo).remove();
630                                     $('label[for='+SN.C.S.NoticeDataGeo+']').remove();
631                                 }
632                             }
633                         }
634                         else {
635                             var cookieValue = JSON.parse($.cookie(SN.C.S.NoticeDataGeoCookie));
636
637                             $('#'+SN.C.S.NoticeLat).val(cookieValue.NLat);
638                             $('#'+SN.C.S.NoticeLon).val(cookieValue.NLon);
639                             $('#'+SN.C.S.NoticeLocationNs).val(cookieValue.NLNS);
640                             $('#'+SN.C.S.NoticeLocationId).val(cookieValue.NLID);
641                             $('#'+SN.C.S.NoticeDataGeo).attr('checked', cookieValue.NDG);
642
643                             $('label[for='+SN.C.S.NoticeDataGeo+']')
644                                 .attr('title', NoticeDataGeo_text.ShareDisable + ' (' + cookieValue.NLN + ')')
645                                 .addClass('checked');
646                         }
647                     }
648                     else {
649                         removeNoticeDataGeo();
650                     }
651                 }).change();
652             }
653         },
654
655         NewDirectMessage: function() {
656             NDM = $('.entity_send-a-message a');
657             NDM.attr({'href':NDM.attr('href')+'&ajax=1'});
658             NDM.bind('click', function() {
659                 var NDMF = $('.entity_send-a-message form');
660                 if (NDMF.length === 0) {
661                     $(this).addClass(SN.C.S.Processing);
662                     $.get(NDM.attr('href'), null, function(data) {
663                         $('.entity_send-a-message').append(document._importNode($('form', data)[0], true));
664                         NDMF = $('.entity_send-a-message .form_notice');
665                         SN.U.FormNoticeXHR(NDMF);
666                         SN.U.FormNoticeEnhancements(NDMF);
667                         NDMF.append('<button class="close">&#215;</button>');
668                         $('.entity_send-a-message button').click(function(){
669                             NDMF.hide();
670                             return false;
671                         });
672                         NDM.removeClass(SN.C.S.Processing);
673                     });
674                 }
675                 else {
676                     NDMF.show();
677                     $('.entity_send-a-message textarea').focus();
678                 }
679                 return false;
680             });
681         },
682
683         GetFullYear: function(year, month, day) {
684             var date = new Date();
685             date.setFullYear(year, month, day);
686
687             return date;
688         },
689
690         StatusNetInstance: {
691             Set: function(value) {
692                 var SNI = SN.U.StatusNetInstance.Get();
693                 if (SNI !== null) {
694                     value = $.extend(SNI, value);
695                 }
696
697                 $.cookie(
698                     SN.C.S.StatusNetInstance,
699                     JSON.stringify(value),
700                     {
701                         path: '/',
702                         expires: SN.U.GetFullYear(2029, 0, 1)
703                     });
704             },
705
706             Get: function() {
707                 var cookieValue = $.cookie(SN.C.S.StatusNetInstance);
708                 if (cookieValue !== null) {
709                     return JSON.parse(cookieValue);
710                 }
711                 return null;
712             },
713
714             Delete: function() {
715                 $.cookie(SN.C.S.StatusNetInstance, null);
716             }
717         },
718
719         /**
720          * Check if the current page is a timeline where the current user's
721          * posts should be displayed immediately on success.
722          *
723          * @fixme this should be done in a saner way, with machine-readable
724          * info about what page we're looking at.
725          */
726         belongsOnTimeline: function(notice) {
727             var action = $("body").attr('id');
728             if (action == 'public') {
729                 return true;
730             }
731
732             var profileLink = $('#nav_profile a').attr('href');
733             if (profileLink) {
734                 var authorUrl = $(notice).find('.entry-title .author a.url').attr('href');
735                 if (authorUrl == profileLink) {
736                     if (action == 'all' || action == 'showstream') {
737                         // Posts always show on your own friends and profile streams.
738                         return true;
739                     }
740                 }
741             }
742
743             // @fixme tag, group, reply timelines should be feasible as well.
744             // Mismatch between id-based and name-based user/group links currently complicates
745             // the lookup, since all our inline mentions contain the absolute links but the
746             // UI links currently on the page use malleable names.
747
748             return false;
749         }
750     },
751
752     Init: {
753         NoticeForm: function() {
754             if ($('body.user_in').length > 0) {
755                 SN.U.NoticeLocationAttach();
756
757                 $('.'+SN.C.S.FormNotice).each(function() {
758                     SN.U.FormNoticeXHR($(this));
759                     SN.U.FormNoticeEnhancements($(this));
760                 });
761
762                 SN.U.NoticeDataAttach();
763             }
764         },
765
766         Notices: function() {
767             if ($('body.user_in').length > 0) {
768                 SN.U.NoticeFavor();
769                 SN.U.NoticeRepeat();
770                 SN.U.NoticeReply();
771             }
772
773             SN.U.NoticeAttachments();
774         },
775
776         EntityActions: function() {
777             if ($('body.user_in').length > 0) {
778                 $('.form_user_subscribe').live('click', function() { SN.U.FormXHR($(this)); return false; });
779                 $('.form_user_unsubscribe').live('click', function() { SN.U.FormXHR($(this)); return false; });
780                 $('.form_group_join').live('click', function() { SN.U.FormXHR($(this)); return false; });
781                 $('.form_group_leave').live('click', function() { SN.U.FormXHR($(this)); return false; });
782                 $('.form_user_nudge').live('click', function() { SN.U.FormXHR($(this)); return false; });
783
784                 SN.U.NewDirectMessage();
785             }
786         },
787
788         Login: function() {
789             if (SN.U.StatusNetInstance.Get() !== null) {
790                 var nickname = SN.U.StatusNetInstance.Get().Nickname;
791                 if (nickname !== null) {
792                     $('#form_login #nickname').val(nickname);
793                 }
794             }
795
796             $('#form_login').bind('submit', function() {
797                 SN.U.StatusNetInstance.Set({Nickname: $('#form_login #nickname').val()});
798                 return true;
799             });
800         }
801     }
802 };
803
804 $(document).ready(function(){
805     if ($('.'+SN.C.S.FormNotice).length > 0) {
806         SN.Init.NoticeForm();
807     }
808     if ($('#content .notices').length > 0) {
809         SN.Init.Notices();
810     }
811     if ($('#content .entity_actions').length > 0) {
812         SN.Init.EntityActions();
813     }
814     if ($('#form_login').length > 0) {
815         SN.Init.Login();
816     }
817 });
818