]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - js/util.js
Merge branch '0.9.x' into 1.0.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: 'notice_data-geo_cookie',
57             NoticeDataGeoSelected: 'notice_data-geo_selected',
58             StatusNetInstance:'StatusNetInstance'
59         }
60     },
61
62     U: { // Utils
63         FormNoticeEnhancements: function(form) {
64             form_id = form.attr('id');
65
66             if (jQuery.data(form[0], 'ElementData') === undefined) {
67                 MaxLength = $('#'+form_id+' #'+SN.C.S.NoticeTextCount).text();
68                 if (typeof(MaxLength) == 'undefined') {
69                      MaxLength = SN.C.I.MaxLength;
70                 }
71                 jQuery.data(form[0], 'ElementData', {MaxLength:MaxLength});
72
73                 SN.U.Counter(form);
74
75                 NDT = $('#'+form_id+' #'+SN.C.S.NoticeDataText);
76
77                 NDT.bind('keyup', function(e) {
78                     SN.U.Counter(form);
79                 });
80
81                 NDT.bind('keydown', function(e) {
82                     SN.U.SubmitOnReturn(e, form);
83                 });
84             }
85             else {
86                 $('#'+form_id+' #'+SN.C.S.NoticeTextCount).text(jQuery.data(form[0], 'ElementData').MaxLength);
87             }
88
89             if ($('body')[0].id != 'conversation') {
90                 $('#'+form_id+' textarea').focus();
91             }
92         },
93
94         SubmitOnReturn: function(event, el) {
95             if (event.keyCode == 13 || event.keyCode == 10) {
96                 el.submit();
97                 event.preventDefault();
98                 event.stopPropagation();
99                 $('#'+el[0].id+' #'+SN.C.S.NoticeDataText).blur();
100                 $('body').focus();
101                 return false;
102             }
103             return true;
104         },
105
106         Counter: function(form) {
107             SN.C.I.FormNoticeCurrent = form;
108             form_id = form.attr('id');
109
110             var MaxLength = jQuery.data(form[0], 'ElementData').MaxLength;
111
112             if (MaxLength <= 0) {
113                 return;
114             }
115
116             var remaining = MaxLength - $('#'+form_id+' #'+SN.C.S.NoticeDataText).val().length;
117             var counter = $('#'+form_id+' #'+SN.C.S.NoticeTextCount);
118
119             if (remaining.toString() != counter.text()) {
120                 if (!SN.C.I.CounterBlackout || remaining === 0) {
121                     if (counter.text() != String(remaining)) {
122                         counter.text(remaining);
123                     }
124                     if (remaining < 0) {
125                         form.addClass(SN.C.S.Warning);
126                     } else {
127                         form.removeClass(SN.C.S.Warning);
128                     }
129                     // Skip updates for the next 500ms.
130                     // On slower hardware, updating on every keypress is unpleasant.
131                     if (!SN.C.I.CounterBlackout) {
132                         SN.C.I.CounterBlackout = true;
133                         SN.C.I.FormNoticeCurrent = form;
134                         window.setTimeout("SN.U.ClearCounterBlackout(SN.C.I.FormNoticeCurrent);", 500);
135                     }
136                 }
137             }
138         },
139
140         ClearCounterBlackout: function(form) {
141             // Allow keyup events to poke the counter again
142             SN.C.I.CounterBlackout = false;
143             // Check if the string changed since we last looked
144             SN.U.Counter(form);
145         },
146
147         FormXHR: function(form) {
148             $.ajax({
149                 type: 'POST',
150                 dataType: 'xml',
151                 url: form.attr('action'),
152                 data: form.serialize() + '&ajax=1',
153                 beforeSend: function(xhr) {
154                     form
155                         .addClass(SN.C.S.Processing)
156                         .find('.submit')
157                             .addClass(SN.C.S.Disabled)
158                             .attr(SN.C.S.Disabled, SN.C.S.Disabled);
159                 },
160                 error: function (xhr, textStatus, errorThrown) {
161                     alert(errorThrown || textStatus);
162                 },
163                 success: function(data, textStatus) {
164                     if (typeof($('form', data)[0]) != 'undefined') {
165                         form_new = document._importNode($('form', data)[0], true);
166                         form.replaceWith(form_new);
167                     }
168                     else {
169                         form.replaceWith(document._importNode($('p', data)[0], true));
170                     }
171                 }
172             });
173         },
174
175         FormNoticeXHR: function(form) {
176             SN.C.I.NoticeDataGeo = {};
177             form_id = form.attr('id');
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                             var notices = $('#notices_primary .notices');
266                             if (notices.length > 0) {
267                                 var notice = document._importNode($('li', data)[0], true);
268                                 if ($('#'+notice.id).length === 0) {
269                                     var notice_irt_value = $('#'+SN.C.S.NoticeInReplyTo).val();
270                                     var notice_irt = '#notices_primary #notice-'+notice_irt_value;
271                                     if($('body')[0].id == 'conversation') {
272                                         if(notice_irt_value.length > 0 && $(notice_irt+' .notices').length < 1) {
273                                             $(notice_irt).append('<ul class="notices"></ul>');
274                                         }
275                                         $($(notice_irt+' .notices')[0]).append(notice);
276                                     }
277                                     else {
278                                         notices.prepend(notice);
279                                     }
280                                     $('#'+notice.id)
281                                         .css({display:'none'})
282                                         .fadeIn(2500);
283                                     SN.U.NoticeWithAttachment($('#'+notice.id));
284                                     SN.U.NoticeReplyTo($('#'+notice.id));
285                                 }
286                             }
287                             else {
288                                 result = document._importNode($('title', data)[0], true);
289                                 result_title = result.textContent || result.innerHTML;
290                                 form.append('<p class="form_response success">'+result_title+'</p>');
291                             }
292                         }
293                         form.resetForm();
294                         form.find('#'+SN.C.S.NoticeInReplyTo).val('');
295                         form.find('#'+SN.C.S.NoticeDataAttachSelected).remove();
296                         SN.U.FormNoticeEnhancements(form);
297                     }
298                 },
299                 complete: function(xhr, textStatus) {
300                     form
301                         .removeClass(SN.C.S.Processing)
302                         .find('#'+SN.C.S.NoticeActionSubmit)
303                             .removeAttr(SN.C.S.Disabled)
304                             .removeClass(SN.C.S.Disabled);
305
306                     $('#'+SN.C.S.NoticeLat).val(SN.C.I.NoticeDataGeo.NLat);
307                     $('#'+SN.C.S.NoticeLon).val(SN.C.I.NoticeDataGeo.NLon);
308                     if ($('#'+SN.C.S.NoticeLocationNs)) {
309                         $('#'+SN.C.S.NoticeLocationNs).val(SN.C.I.NoticeDataGeo.NLNS);
310                         $('#'+SN.C.S.NoticeLocationId).val(SN.C.I.NoticeDataGeo.NLID);
311                     }
312                     $('#'+SN.C.S.NoticeDataGeo).attr('checked', SN.C.I.NoticeDataGeo.NDG);
313                 }
314             });
315         },
316
317         NoticeReply: function() {
318             if ($('#'+SN.C.S.NoticeDataText).length > 0 && $('#content .notice_reply').length > 0) {
319                 $('#content .notice').each(function() { SN.U.NoticeReplyTo($(this)); });
320             }
321         },
322
323         NoticeReplyTo: function(notice_item) {
324             var notice = notice_item[0];
325             var notice_reply = $('.notice_reply', notice)[0];
326
327             if (jQuery.data(notice_reply, "ElementData") === undefined) {
328                 jQuery.data(notice_reply, "ElementData", {Bind:'submit'});
329                 $(notice_reply).bind('click', function() {
330                     var nickname = ($('.author .nickname', notice).length > 0) ? $($('.author .nickname', notice)[0]) : $('.author .nickname.uid');
331                     SN.U.NoticeReplySet(nickname.text(), $($('.notice_id', notice)[0]).text());
332                     return false;
333                 });
334             }
335         },
336
337         NoticeReplySet: function(nick,id) {
338             if (nick.match(SN.C.I.PatternUsername)) {
339                 var text = $('#'+SN.C.S.NoticeDataText);
340                 if (text.length > 0) {
341                     replyto = '@' + nick + ' ';
342                     text.val(replyto + text.val().replace(RegExp(replyto, 'i'), ''));
343                     $('#'+SN.C.S.FormNotice+' #'+SN.C.S.NoticeInReplyTo).val(id);
344
345                     text[0].focus();
346                     if (text[0].setSelectionRange) {
347                         var len = text.val().length;
348                         text[0].setSelectionRange(len,len);
349                     }
350                 }
351             }
352         },
353
354         NoticeFavor: function() {
355             $('.form_favor').live('click', function() { SN.U.FormXHR($(this)); return false; });
356             $('.form_disfavor').live('click', function() { SN.U.FormXHR($(this)); return false; });
357         },
358
359         NoticeRepeat: function() {
360             $('.form_repeat').live('click', function(e) {
361                 e.preventDefault();
362
363                 SN.U.NoticeRepeatConfirmation($(this));
364                 return false;
365             });
366         },
367
368         NoticeRepeatConfirmation: function(form) {
369             var submit_i = form.find('.submit');
370
371             var submit = submit_i.clone();
372             submit
373                 .addClass('submit_dialogbox')
374                 .removeClass('submit');
375             form.append(submit);
376             submit.bind('click', function() { SN.U.FormXHR(form); return false; });
377
378             submit_i.hide();
379
380             form
381                 .addClass('dialogbox')
382                 .append('<button class="close">&#215;</button>')
383                 .closest('.notice-options')
384                     .addClass('opaque');
385
386             form.find('button.close').click(function(){
387                 $(this).remove();
388
389                 form
390                     .removeClass('dialogbox')
391                     .closest('.notice-options')
392                         .removeClass('opaque');
393
394                 form.find('.submit_dialogbox').remove();
395                 form.find('.submit').show();
396
397                 return false;
398             });
399         },
400
401         NoticeAttachments: function() {
402             $('.notice a.attachment').each(function() {
403                 SN.U.NoticeWithAttachment($(this).closest('.notice'));
404             });
405         },
406
407         NoticeWithAttachment: function(notice) {
408             if (notice.find('.attachment').length === 0) {
409                 return;
410             }
411
412             $.fn.jOverlay.options = {
413                 method : 'GET',
414                 data : '',
415                 url : '',
416                 color : '#000',
417                 opacity : '0.6',
418                 zIndex : 9999,
419                 center : false,
420                 imgLoading : $('address .url')[0].href+'theme/base/images/illustrations/illu_progress_loading-01.gif',
421                 bgClickToClose : true,
422                 success : function() {
423                     $('#jOverlayContent').append('<button class="close">&#215;</button>');
424                     $('#jOverlayContent button').click($.closeOverlay);
425                 },
426                 timeout : 0,
427                 autoHide : true,
428                 css : {'max-width':'542px', 'top':'5%', 'left':'32.5%'}
429             };
430
431             notice.find('a.attachment').click(function() {
432                 $().jOverlay({url: $('address .url')[0].href+'attachment/' + ($(this).attr('id').substring('attachment'.length + 1)) + '/ajax'});
433                 return false;
434             });
435
436             if ($('#shownotice').length == 0) {
437                 var t;
438                 notice.find('a.thumbnail').hover(
439                     function() {
440                         var anchor = $(this);
441                         $('a.thumbnail').children('img').hide();
442                         anchor.closest(".entry-title").addClass('ov');
443
444                         if (anchor.children('img').length === 0) {
445                             t = setTimeout(function() {
446                                 $.get($('address .url')[0].href+'attachment/' + (anchor.attr('id').substring('attachment'.length + 1)) + '/thumbnail', null, function(data) {
447                                     anchor.append(data);
448                                 });
449                             }, 500);
450                         }
451                         else {
452                             anchor.children('img').show();
453                         }
454                     },
455                     function() {
456                         clearTimeout(t);
457                         $('a.thumbnail').children('img').hide();
458                         $(this).closest('.entry-title').removeClass('ov');
459                     }
460                 );
461             }
462         },
463
464         NoticeDataAttach: function() {
465             NDA = $('#'+SN.C.S.NoticeDataAttach);
466             NDA.change(function() {
467                 S = '<div id="'+SN.C.S.NoticeDataAttachSelected+'" class="'+SN.C.S.Success+'"><code>'+$(this).val()+'</code> <button class="close">&#215;</button></div>';
468                 NDAS = $('#'+SN.C.S.NoticeDataAttachSelected);
469                 if (NDAS.length > 0) {
470                     NDAS.replaceWith(S);
471                 }
472                 else {
473                     $('#'+SN.C.S.FormNotice).append(S);
474                 }
475                 $('#'+SN.C.S.NoticeDataAttachSelected+' button').click(function(){
476                     $('#'+SN.C.S.NoticeDataAttachSelected).remove();
477                     NDA.val('');
478
479                     return false;
480                 });
481             });
482         },
483
484         NoticeLocationAttach: function() {
485             var NLat = $('#'+SN.C.S.NoticeLat).val();
486             var NLon = $('#'+SN.C.S.NoticeLon).val();
487             var NLNS = $('#'+SN.C.S.NoticeLocationNs).val();
488             var NLID = $('#'+SN.C.S.NoticeLocationId).val();
489             var NLN = $('#'+SN.C.S.NoticeGeoName).text();
490             var NDGe = $('#'+SN.C.S.NoticeDataGeo);
491
492             function removeNoticeDataGeo() {
493                 $('label[for='+SN.C.S.NoticeDataGeo+']')
494                     .attr('title', jQuery.trim($('label[for='+SN.C.S.NoticeDataGeo+']').text()))
495                     .removeClass('checked');
496
497                 $('#'+SN.C.S.NoticeLat).val('');
498                 $('#'+SN.C.S.NoticeLon).val('');
499                 $('#'+SN.C.S.NoticeLocationNs).val('');
500                 $('#'+SN.C.S.NoticeLocationId).val('');
501                 $('#'+SN.C.S.NoticeDataGeo).attr('checked', false);
502
503                 $.cookie(SN.C.S.NoticeDataGeoCookie, 'disabled', { path: '/', expires: SN.U.GetFullYear(2029, 0, 1) });
504             }
505
506             function getJSONgeocodeURL(geocodeURL, data) {
507                 $.getJSON(geocodeURL, data, function(location) {
508                     var lns, lid;
509
510                     if (typeof(location.location_ns) != 'undefined') {
511                         $('#'+SN.C.S.NoticeLocationNs).val(location.location_ns);
512                         lns = location.location_ns;
513                     }
514
515                     if (typeof(location.location_id) != 'undefined') {
516                         $('#'+SN.C.S.NoticeLocationId).val(location.location_id);
517                         lid = location.location_id;
518                     }
519
520                     if (typeof(location.name) == 'undefined') {
521                         NLN_text = data.lat + ';' + data.lon;
522                     }
523                     else {
524                         NLN_text = location.name;
525                     }
526
527                     $('label[for='+SN.C.S.NoticeDataGeo+']')
528                         .attr('title', NoticeDataGeo_text.ShareDisable + ' (' + NLN_text + ')');
529
530                     $('#'+SN.C.S.NoticeLat).val(data.lat);
531                     $('#'+SN.C.S.NoticeLon).val(data.lon);
532                     $('#'+SN.C.S.NoticeLocationNs).val(lns);
533                     $('#'+SN.C.S.NoticeLocationId).val(lid);
534                     $('#'+SN.C.S.NoticeDataGeo).attr('checked', true);
535
536                     var cookieValue = {
537                         NLat: data.lat,
538                         NLon: data.lon,
539                         NLNS: lns,
540                         NLID: lid,
541                         NLN: NLN_text,
542                         NLNU: location.url,
543                         NDG: true
544                     };
545
546                     $.cookie(SN.C.S.NoticeDataGeoCookie, JSON.stringify(cookieValue), { path: '/', expires: SN.U.GetFullYear(2029, 0, 1) });
547                 });
548             }
549
550             if (NDGe.length > 0) {
551                 if ($.cookie(SN.C.S.NoticeDataGeoCookie) == 'disabled') {
552                     NDGe.attr('checked', false);
553                 }
554                 else {
555                     NDGe.attr('checked', true);
556                 }
557
558                 var NGW = $('#notice_data-geo_wrap');
559                 var geocodeURL = NGW.attr('title');
560                 NGW.removeAttr('title');
561
562                 $('label[for='+SN.C.S.NoticeDataGeo+']')
563                     .attr('title', jQuery.trim($('label[for='+SN.C.S.NoticeDataGeo+']').text()));
564
565                 NDGe.change(function() {
566                     if ($('#'+SN.C.S.NoticeDataGeo).attr('checked') === true || $.cookie(SN.C.S.NoticeDataGeoCookie) === null) {
567                         $('label[for='+SN.C.S.NoticeDataGeo+']')
568                             .attr('title', NoticeDataGeo_text.ShareDisable)
569                             .addClass('checked');
570
571                         if ($.cookie(SN.C.S.NoticeDataGeoCookie) === null || $.cookie(SN.C.S.NoticeDataGeoCookie) == 'disabled') {
572                             if (navigator.geolocation) {
573                                 navigator.geolocation.getCurrentPosition(
574                                     function(position) {
575                                         $('#'+SN.C.S.NoticeLat).val(position.coords.latitude);
576                                         $('#'+SN.C.S.NoticeLon).val(position.coords.longitude);
577
578                                         var data = {
579                                             lat: position.coords.latitude,
580                                             lon: position.coords.longitude,
581                                             token: $('#token').val()
582                                         };
583
584                                         getJSONgeocodeURL(geocodeURL, data);
585                                     },
586
587                                     function(error) {
588                                         switch(error.code) {
589                                             case error.PERMISSION_DENIED:
590                                                 removeNoticeDataGeo();
591                                                 break;
592                                             case error.TIMEOUT:
593                                                 $('#'+SN.C.S.NoticeDataGeo).attr('checked', false);
594                                                 break;
595                                         }
596                                     },
597
598                                     {
599                                         timeout: 10000
600                                     }
601                                 );
602                             }
603                             else {
604                                 if (NLat.length > 0 && NLon.length > 0) {
605                                     var data = {
606                                         lat: NLat,
607                                         lon: NLon,
608                                         token: $('#token').val()
609                                     };
610
611                                     getJSONgeocodeURL(geocodeURL, data);
612                                 }
613                                 else {
614                                     removeNoticeDataGeo();
615                                     $('#'+SN.C.S.NoticeDataGeo).remove();
616                                     $('label[for='+SN.C.S.NoticeDataGeo+']').remove();
617                                 }
618                             }
619                         }
620                         else {
621                             var cookieValue = JSON.parse($.cookie(SN.C.S.NoticeDataGeoCookie));
622
623                             $('#'+SN.C.S.NoticeLat).val(cookieValue.NLat);
624                             $('#'+SN.C.S.NoticeLon).val(cookieValue.NLon);
625                             $('#'+SN.C.S.NoticeLocationNs).val(cookieValue.NLNS);
626                             $('#'+SN.C.S.NoticeLocationId).val(cookieValue.NLID);
627                             $('#'+SN.C.S.NoticeDataGeo).attr('checked', cookieValue.NDG);
628
629                             $('label[for='+SN.C.S.NoticeDataGeo+']')
630                                 .attr('title', NoticeDataGeo_text.ShareDisable + ' (' + cookieValue.NLN + ')')
631                                 .addClass('checked');
632                         }
633                     }
634                     else {
635                         removeNoticeDataGeo();
636                     }
637                 }).change();
638             }
639         },
640
641         NewDirectMessage: function() {
642             NDM = $('.entity_send-a-message a');
643             NDM.attr({'href':NDM.attr('href')+'&ajax=1'});
644             NDM.bind('click', function() {
645                 var NDMF = $('.entity_send-a-message form');
646                 if (NDMF.length === 0) {
647                     $(this).addClass(SN.C.S.Processing);
648                     $.get(NDM.attr('href'), null, function(data) {
649                         $('.entity_send-a-message').append(document._importNode($('form', data)[0], true));
650                         NDMF = $('.entity_send-a-message .form_notice');
651                         SN.U.FormNoticeXHR(NDMF);
652                         SN.U.FormNoticeEnhancements(NDMF);
653                         NDMF.append('<button class="close">&#215;</button>');
654                         $('.entity_send-a-message button').click(function(){
655                             NDMF.hide();
656                             return false;
657                         });
658                         NDM.removeClass(SN.C.S.Processing);
659                     });
660                 }
661                 else {
662                     NDMF.show();
663                     $('.entity_send-a-message textarea').focus();
664                 }
665                 return false;
666             });
667         },
668
669         GetFullYear: function(year, month, day) {
670             var date = new Date();
671             date.setFullYear(year, month, day);
672
673             return date;
674         },
675
676         StatusNetInstance: {
677             Set: function(value) {
678                 var SNI = SN.U.StatusNetInstance.Get();
679                 if (SNI !== null) {
680                     value = $.extend(SNI, value);
681                 }
682
683                 $.cookie(
684                     SN.C.S.StatusNetInstance,
685                     JSON.stringify(value),
686                     {
687                         path: '/',
688                         expires: SN.U.GetFullYear(2029, 0, 1)
689                     });
690             },
691
692             Get: function() {
693                 var cookieValue = $.cookie(SN.C.S.StatusNetInstance);
694                 if (cookieValue !== null) {
695                     return JSON.parse(cookieValue);
696                 }
697                 return null;
698             },
699
700             Delete: function() {
701                 $.cookie(SN.C.S.StatusNetInstance, null);
702             }
703         }
704     },
705
706     Init: {
707         NoticeForm: function() {
708             if ($('body.user_in').length > 0) {
709                 SN.U.NoticeLocationAttach();
710
711                 $('.'+SN.C.S.FormNotice).each(function() {
712                     SN.U.FormNoticeXHR($(this));
713                     SN.U.FormNoticeEnhancements($(this));
714                 });
715
716                 SN.U.NoticeDataAttach();
717             }
718         },
719
720         Notices: function() {
721             if ($('body.user_in').length > 0) {
722                 SN.U.NoticeFavor();
723                 SN.U.NoticeRepeat();
724                 SN.U.NoticeReply();
725             }
726
727             SN.U.NoticeAttachments();
728         },
729
730         EntityActions: function() {
731             if ($('body.user_in').length > 0) {
732                 $('.form_user_subscribe').live('click', function() { SN.U.FormXHR($(this)); return false; });
733                 $('.form_user_unsubscribe').live('click', function() { SN.U.FormXHR($(this)); return false; });
734                 $('.form_group_join').live('click', function() { SN.U.FormXHR($(this)); return false; });
735                 $('.form_group_leave').live('click', function() { SN.U.FormXHR($(this)); return false; });
736                 $('.form_user_nudge').live('click', function() { SN.U.FormXHR($(this)); return false; });
737
738                 SN.U.NewDirectMessage();
739             }
740         },
741
742         Login: function() {
743             if (SN.U.StatusNetInstance.Get() !== null) {
744                 var nickname = SN.U.StatusNetInstance.Get().Nickname;
745                 if (nickname !== null) {
746                     $('#form_login #nickname').val(nickname);
747                 }
748             }
749
750             $('#form_login').bind('submit', function() {
751                 SN.U.StatusNetInstance.Set({Nickname: $('#form_login #nickname').val()});
752                 return true;
753             });
754         }
755     }
756 };
757
758 $(document).ready(function(){
759     if ($('.'+SN.C.S.FormNotice).length > 0) {
760         SN.Init.NoticeForm();
761     }
762     if ($('#content .notices').length > 0) {
763         SN.Init.Notices();
764     }
765     if ($('#content .entity_actions').length > 0) {
766         SN.Init.EntityActions();
767     }
768     if ($('#form_login').length > 0) {
769         SN.Init.Login();
770     }
771 });
772