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