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