Log out whether or not the server reports success
[blerg.git] / www / js / blerg.js
1 /* Blerg is (C) 2011 The Dominion of Awesome, and is distributed under a
2  * BSD-style license.  Please see the COPYING file for details.
3  */
4
5 // Config
6 var baseURL = '';
7 var recordTemplate = new Template(
8     '<div class="record">#{data}<div class="info">Posted #{date}. <a href="/\##{author}/#{record}">[permalink]</a> <a href="#" onclick="postPopup(\'@#{author}/#{record}: \'); return false">[reply]</a></div></div>'
9 );
10 var tagRecordTemplate = new Template(
11     '<div class="record">#{data}<div class="info">Posted by <a class="author ref" href="/\##{author}" onclick="return qlink()">@#{author}</a> on #{date}. <a href="/\##{author}/#{record}">[permalink]</a> <a href="#" onclick="postPopup(\'@#{author}/#{record}: \'); return false">[reply]</a></div></div>'
12 );
13 var latestRecordsTemplate = new Template(
14     '<div class="record"><a class="author ref" href="/\##{author}" onclick="return qlink()">@#{author}</a> #{data}</div>'
15 );
16
17 // Page elements
18 var items;
19
20 // Other globals
21 var currentPager;
22 var loginStatus;
23
24 // Object to keep track of login status
25 function LoginStatus() {
26     var cookies = {};
27     document.cookie.split(/;\s+/).each(function(v) {
28         kv = v.split('=');
29         cookies[kv[0]] = kv[1];
30     });
31     if (cookies.auth && cookies.username) {
32         this.loggedIn = true;
33         this.username = cookies.username;
34     } else {
35         this.loggedIn = false;
36         this.username = null;
37     }
38     this.update();
39 }
40
41 LoginStatus.prototype.login = function(username, password) {
42     new Ajax.Request(baseURL + '/login', {
43         parameters: {
44             username: username,
45             password: password
46         },
47         onSuccess: function(r) {
48             var j = r.responseText.evalJSON();
49             if (j && j.status == 'success') {
50                 this.loggedIn = true;
51                 this.username = username;
52                 document.cookie = "username=" + username;
53                 $('login.password').value = '';
54                 this.update();
55             } else {
56                 alert("Could not log in");
57                 $('login.username').focus();
58             }
59         }.bind(this),
60         onFailure: function(r) {
61             alert("Could not log in");
62             $('login.username').focus();
63         }
64     });
65 }
66
67 LoginStatus.prototype.logout = function() {
68     new Ajax.Request(baseURL + '/logout', {
69         parameters: {
70             username: this.username
71         },
72         onComplete: function(r) {
73             this.loggedIn = false;
74             document.cookie = "auth=; expires=1-Jan-1970 00:00:00 GMT";
75             this.update();
76         }.bind(this)
77     });
78     document.cookie = "username=; expires=1-Jan-1970 00:00:00 GMT";
79 }
80
81 LoginStatus.prototype.update = function() {
82     if (this.loggedIn) {
83         $('userlink').href = '/#' + this.username;
84         $('userlink').update('@' + this.username);
85         $('reflink').href = '/#/ref/' + this.username;
86         $('login').hide();
87         $('logout').show();
88     } else {
89         $('post').hide();
90         $('login').show();
91         $('logout').hide();
92     }
93 }
94
95 LoginStatus.prototype.post = function(msg) {
96     if (!this.loggedIn) {
97         alert("You are not logged in!");
98         return;
99     }
100
101     new Ajax.Request(baseURL + '/put', {
102         parameters: {
103             username: this.username,
104             data: msg
105         },
106         onSuccess: function(r) {
107             var j = r.responseText.evalJSON();
108             if (j && j.status == 'success') {
109                 $('post.content').value = '';
110                 if (location.hash != '#' + this.username) {
111                     qlink(this.username);
112                 } else {
113                     currentPager.itemCount++;
114                     currentPager.reload();
115                 }
116             } else {
117                 alert('Post failed!');
118             }
119         }.bind(this),
120         onFailure: function(r) {
121             alert('Post failed!');
122         }
123     });
124 }
125
126
127 // Base object for paged data
128 function Pager() {
129     this.itemsPerPage = 10;
130 }
131
132 Pager.prototype.initPager = function() {
133     this.itemCache = new Hash();
134     this.pageStart = null;
135 }
136
137 Pager.prototype.olderPage = function() {
138     if (this.pageStart >= this.itemsPerPage) {
139         qlink(this.baseFrag + '/p' + (this.pageStart - this.itemsPerPage));
140     }
141 }
142
143 Pager.prototype.newerPage = function() {
144     if (this.pageStart + this.itemsPerPage < this.itemCount) {
145         qlink(this.baseFrag + '/p' + (this.pageStart + this.itemsPerPage));
146     }
147 }
148
149 Pager.prototype.addItems = function(items) {
150     items.each(function(v) {
151         if (!this.itemCache[v.id])
152             this.itemCache[v.id] = v;
153     }.bind(this));
154 }
155
156 Pager.prototype.displayItems = function() {
157     if (this.pageStart == undefined)
158         this.pageStart == this.itemCount - 1;
159     items.update();
160
161     if (this.pageStart != undefined && this.itemCache[this.pageStart]) {
162         var end = (this.pageStart >= this.itemsPerPage ? this.pageStart - this.itemsPerPage + 1 : 0);
163         for (var i = this.pageStart; i >= end; i--) {
164             items.insert(this.itemCache[i].html);
165         }
166     } else {
167         items.insert("There doesn't seem to be anything here!");
168     }
169
170     if (this.pageStart < this.itemCount - 1) {
171         $('newer_link').href = '/#' + this.baseFrag + '/p' + (this.pageStart + this.itemsPerPage);
172         $('newer_link').show();
173     } else {
174         $('newer_link').hide();
175     }
176
177     if (this.pageStart >= 10) {
178         $('older_link').href = '/#' + this.baseFrag + '/p' + (this.pageStart - this.itemsPerPage);
179         $('older_link').show();
180     } else {
181         $('older_link').hide();
182     }
183
184     document.body.scrollTo();
185 }
186
187 Pager.prototype.reload = function() {
188     this.pageStart = null;
189     this.loadItems(null, null, Pager.prototype.showPageAt.bind(this, this.itemCount - 1));
190 }
191
192 Pager.prototype.showPageAt = function(r) {
193     var end = (r - 9 > 0 ? r - 9 : 0);
194     if (this.itemCache[r] && this.itemCache[end]) {
195         this.pageStart = r;
196         this.displayItems();
197     } else {
198         this.loadItems((r >= 49 ? r - 49 : 0), r, Pager.prototype.showPageAt.bind(this, r));
199     }
200 }
201
202 Pager.prototype.showRecord = function(r) {
203     if (this.itemCache[r]) {
204         $('older_link').hide();
205         $('newer_link').hide();
206         items.update(this.itemCache[r].html);
207     } else {
208         this.loadItems(r, r, Pager.prototype.showRecord.bind(this, r));
209     }
210 }
211
212 Pager.prototype.loadItems = function(from, to, continuation) { }
213
214
215 // Object to render user pages
216 function User(username) {
217     this.initPager();
218     this.username = username;
219     this.baseFrag = username;
220 }
221 User.prototype = new Pager();
222 User.prototype.constructor = User;
223
224 User.prototype.show = function() {
225     $$('[name=section]').each(function(v) { v.update(' @' + this.username) }.bind(this));
226     $('welcome').hide();
227     items.show();
228     $('rss').show();
229     $('rsslink').href = '/rss/' + this.username;
230     $$('[name=user.reflink]').each(function(e) {
231         e.href = '/#/ref/' + this.username;
232     }.bind(this));
233     $('usercontrols').show();
234 }
235
236 User.prototype.reload = function() {
237     this.pageStart = null;
238
239     $$('[name=user.subscribelink]').each(Element.hide);
240     $$('[name=user.unsubscribelink]').each(Element.hide);
241
242     if (loginStatus.loggedIn) {
243         new Ajax.Request(baseURL + '/feedinfo/' + this.username, {
244             method: 'post',
245             parameters: {
246                 username: loginStatus.username
247             },
248             onSuccess: function(r) {
249                 var json = r.responseText.evalJSON();
250                 if (json.subscribed) {
251                     $$('[name=user.subscribelink]').each(Element.hide);
252                     $$('[name=user.unsubscribelink]').each(Element.show);
253                 } else {
254                     $$('[name=user.subscribelink]').each(Element.show);
255                     $$('[name=user.unsubscribelink]').each(Element.hide);
256                 }
257             }
258         });
259     }
260
261     new Ajax.Request(baseURL + '/info/' + this.username, {
262         method: 'get',
263         onSuccess: function(r) {
264             var j = r.responseText.evalJSON();
265             if (j) {
266                 this.itemCount = parseInt(j.record_count);
267                 if (!this.pageStart)
268                     this.showPageAt(this.itemCount - 1);
269             }
270         }.bind(this)
271     });
272 }
273
274 User.prototype.loadItems = function(from, to, continuation) {
275     var url;
276     if (from != undefined && to != undefined) {
277         url = baseURL + '/get/' + this.username + '/' + from + '-' + to;
278         this.pageStart = to;
279     } else {
280         url = baseURL + '/get/' + this.username;
281     }
282
283     new Ajax.Request(url, {
284         method: 'get',
285         onSuccess: function(r) {
286             var records = r.responseText.evalJSON();
287             if (records && records.length > 0) {
288                 records.each(function(v) {
289                     v.id = v.record;
290                     v.author = this.username;
291                     mangleRecord(v, recordTemplate);
292                 }.bind(this));
293                 this.addItems(records);
294                 if (!this.pageStart)
295                     this.pageStart = records[0].recInt;
296             }
297             continuation();
298         }.bind(this),
299         onFailure: function(r) {
300             this.displayItems();
301         }.bind(this),
302         on404: function(r) {
303             displayError('User not found');
304         }
305     });
306 }
307
308 function mangleRecord(record, template) {
309     record.recInt = parseInt(record.record);
310
311     var lines = record.data.split(/\r?\n/);
312     if (lines[lines.length - 1] == '')
313         lines.pop();
314
315     var out = ['<p>'];
316     var endpush = null;
317     var listMode = false;
318     lines.each(function(l) {
319         if (l == '') {
320             if (out[out.length - 1] == '<br>') {
321                 out[out.length - 1] = '<p>';
322             }
323             if (out[out.length - 1] == '</li>') {
324                 out.push('</ul>');
325                 out.push('<p>');
326                 listMode = false;
327             }
328             return;
329         }
330
331         // Put quoted material into a special paragraph
332         if (l[0] == '>') {
333             var pi = out.lastIndexOf('<p>');
334             if (pi != -1) {
335                 out[pi] = '<p class="quote">';
336                 l = l.replace(/^>\s*/, '');
337             }
338         }
339
340         // Sanitize HTML input
341         l = l.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
342
343         // Turn HTTP URLs into links
344         l = l.replace(/(\s|^)(https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/([^\s"]*[^.!,;?()\s])?)?)/g, '$1<a href="$2">$2</a>');
345
346         // Turn markdown links into links
347         l = l.replace(/(\s|^)\[([^\]]+)\]\((https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/[^)"]*?)?)\)/g, '$1<a href="$3">$2</a>');
348
349         // Turn *foo* into italics and **foo** into bold
350         l = l.replace(/([^\w\\]|^)\*\*(\w[^*]*)\*\*(\W|$)/g, '$1<b>$2</b>$3');
351         l = l.replace(/([^\w\\]|^)\*(\w[^*]*)\*(\W|$)/g, '$1<i>$2</i>$3');
352
353         // Turn refs and tags into links
354         l = l.replace(/(\s|^)#([A-Za-z0-9_-]+)/g, '$1<a href="#/tag/$2" class="ref" onclick="return qlink()">#$2</a>');
355         l = l.replace(/(\s|^)@([A-Za-z0-9_-]+)(\/\d+)?/g, '$1<a href="#$2$3" class="ref" onclick="return qlink()">@$2</a>');
356
357         // Create lists when lines begin with *
358         if (l[0] == '*') {
359             if (!listMode) {
360                 var pi = out.lastIndexOf('<p>');
361                 out[pi] = '<ul>';
362                 listMode = true;
363             }
364             l = l.replace(/^\*\s*/, '');
365             out.push('<li>');
366             endpush = '</li>';
367         }
368
369         // Create headers when lines begin with = or #
370         if (l[0] == '=' || l[0] == '#') {
371             var m = l.match(/^([=#]+)/);
372             var depth = m[1].length;
373             if (depth <= 5) {
374                 l = l.replace(/^[=#]+\s*/, '').replace(/\s*[=#]+$/, '');
375                 out.push('<h' + depth + '>');
376                 endpush = '</h' + depth + '>';
377             }
378         }
379
380         // Remove backslashes from escaped metachars
381         l = l.replace(/\\([*\[\]@#])/g, '$1');
382
383         out.push(l);
384         if (endpush) {
385             out.push(endpush);
386             endpush = null;
387         } else {
388             out.push('<br>');
389         }
390     });
391     while (out[out.length - 1] == '<br>' || out[out.length - 1] == '<p>')
392         out.pop();
393     if (listMode)
394         out.push('</ul>');
395
396     record.data = out.join('');
397     record.date = (new Date(record.timestamp * 1000)).toString();
398     record.html = template.evaluate(record);
399 }
400
401 function displayError(msg) {
402     items.innerText = msg;
403 }
404
405
406 // Object for browsing tags
407 function Tag(type, tag) {
408     this.initPager();
409     this.type = type;
410     this.tag = tag;
411
412     var url = baseURL + "/tag/";
413     switch(type) {
414     case 'tag':
415         //url += '%23';
416         url += 'H';  // apache is eating the hash, even encoded.  Probably a security feature.
417         this.baseFrag = '/tag/' + tag;
418         break;
419     case 'ref':
420         url += '%40';
421         this.baseFrag = '/ref/' + tag;
422         break;
423     default:
424         alert('Invalid tag type: ' + type);
425         return;
426     }
427     url += tag;
428
429     new Ajax.Request(url, {
430         method: 'get',
431         onSuccess: function(r) {
432             var j = r.responseText.evalJSON();
433             if (j) {
434                 var maxid = j.length - 1;
435                 j.each(function(v, i) {
436                     v.id = maxid - i;
437                     mangleRecord(v, tagRecordTemplate)
438                 });
439                 this.addItems(j);
440                 this.pageStart = j.length - 1;
441                 this.itemCount = j.length;
442             }
443             this.displayItems();
444         }.bind(this),
445         onFailure: function(r) {
446             this.displayItems();
447         }.bind(this)
448     });
449
450 }
451 Tag.prototype = new Pager();
452 Tag.prototype.constructor = Tag;
453
454 Tag.prototype.show = function() {
455     var ctype = {ref: '@', tag: '#'}[this.type];
456
457     $$('[name=section]').each(function(v) {
458         v.update(' about ' + ctype + this.tag);
459     }.bind(this));
460     $('welcome').hide();
461     $('post').hide();
462     $('older_link').hide();
463     $('newer_link').hide();
464     $('rss').hide();
465     items.show();
466     $('usercontrols').hide();
467 }
468
469
470 // Pager for browsing subscription feeds
471 function Feed() {
472     this.initPager();
473     this.username = loginStatus.username;
474     this.baseFrag = '/feed';
475
476     new Ajax.Request(baseURL + '/feed', {
477         method: 'post',
478         parameters: {
479             username: loginStatus.username
480         },
481         onSuccess: function(r) {
482             var response = r.responseText.evalJSON();
483             if (response) {
484                 var maxid = response.length - 1;
485                 response.each(function(v, i) {
486                     v.id = maxid - i;
487                     mangleRecord(v, tagRecordTemplate)
488                 });
489                 this.addItems(response);
490                 this.pageStart = response.length - 1;
491                 this.itemCount = response.length;
492             }
493             this.displayItems();
494         }.bind(this),
495         onFailure: function(r) {
496             this.displayItems();
497         }.bind(this)
498     });
499 }
500 Feed.prototype = new Pager();
501 Feed.prototype.constructor = Feed;
502
503 Feed.prototype.show = function() {
504     $$('[name=section]').each(function(v) {
505         v.update(' ' + loginStatus.username + "'s spycam");
506     }.bind(this));
507     $('welcome').hide();
508     $('post').hide();
509     $('older_link').hide();
510     $('newer_link').hide();
511     $('rss').hide();
512     items.show();
513     $('usercontrols').hide();
514 }
515
516
517 function postPopup(initial) {
518     if (loginStatus.loggedIn || initial) {
519         var post = $('post');
520         if (post.visible()) {
521             post.hide();
522         } else {
523             post.show();
524             if (initial) {
525                 $('post.content').value = initial;
526             } else if (!$('post.content').value && currentPager.username && currentPager.username != loginStatus.username) {
527                 $('post.content').value = '@' + currentPager.username + ': ';
528             }
529             $('post.content').focus();
530         }
531     }
532 }
533
534 function signup() {
535     var username = $('signup.username').value;
536     var password = $('signup.password').value;
537
538     new Ajax.Request(baseURL + '/create', {
539         parameters: {
540             username: username,
541             password: password
542         },
543         onSuccess: function(r) {
544             $('signup').hide();
545             qlink(username);
546
547             loginStatus.login(username, password);
548         },
549         onFailure: function(r) {
550             alert("Failed to create user");
551         }
552     });
553 }
554
555 function signup_cancel() {
556     $('signup').hide();
557     hashSwitch();
558 }
559
560 function subscribe() {
561     new Ajax.Request(baseURL + '/subscribe/' + currentPager.username, {
562         method: 'post',
563         parameters: {
564             username: loginStatus.username
565         },
566         onSuccess: function(r) {
567             var response = r.responseText.evalJSON();
568             if (response.status == 'success') {
569                 alert("You call " + currentPager.username + " and begin breathing heavily into the handset.");
570                 $$('[name=user.subscribelink]').each(Element.hide);
571                 $$('[name=user.unsubscribelink]').each(Element.show);
572             } else {
573                 alert('Failed to subscribe. This is probably for the best');
574             }
575         },
576         onFailure: function(r) {
577             alert('Failed to subscribe. This is probably for the best');
578         }
579     });
580 }
581
582 function unsubscribe() {
583     new Ajax.Request(baseURL + '/unsubscribe/' + currentPager.username, {
584         method: 'post',
585         parameters: {
586             username: loginStatus.username
587         },
588         onSuccess: function(r) {
589             var response = r.responseText.evalJSON();
590             if (response.status == 'success') {
591                 alert("You come to your senses.");
592                 $$('[name=user.subscribelink]').each(Element.show);
593                 $$('[name=user.unsubscribelink]').each(Element.hide);
594             } else {
595                 alert('You are unable to tear yourself away (because something failed on the server)');
596             }
597         },
598         onFailure: function(r) {
599             alert('You are unable to tear yourself away (because something failed on the server)');
600         }
601     });
602 }
603
604 var resizePostContentTimeout = null;
605 function resizePostContent() {
606     if (resizePostContentTimeout)
607         clearTimeout(resizePostContentTimeout);
608     resizePostContentTimeout = setTimeout(function() {
609         var c = $('post.content');
610         var lines = Math.floor(c.value.length / (100 * (c.clientWidth / 1000))) + 1;
611         var m = c.value.match(/\r?\n/g);
612         if (m)
613             lines += m.length;
614         if (lines <= 3) {
615             c.style.height = "";
616         } else {
617             c.style.height = (lines * 17) + "pt";
618         }
619         resizePostContentTimeout = null;
620     }, 150);
621 }
622
623 var tickerTimer = null;
624 var tickerHead, tickerTail;
625
626 function tickerFader(a, b, p) {
627     var p2 = 1 - p;
628
629     a.style.opacity = p;
630     a.style.lineHeight = (100 * p) + '%';
631
632     b.style.opacity = p2;
633     b.style.lineHeight = (100 * p2) + '%';
634     if (p == 1.0)
635         b.hide();
636 }
637
638 function ticker() {
639     tickerHead.show();
640     Bytex64.FX.run(tickerFader.curry(tickerHead, tickerTail), 0.5);
641     tickerHead = tickerHead.nextSibling;
642     tickerTail = tickerTail.nextSibling;
643     if (tickerHead == null) {
644         stopTicker();
645         loadLatest.delay(10);
646     }
647 }
648
649 function startTicker() {
650     stopTicker();
651     for (var elem = $('latest-posts').firstChild; elem != null; elem = elem.nextSibling) {
652         elem.hide();
653     }
654
655     // Show the first five
656     tickerHead = $('latest-posts').firstChild;
657     for (var i = 0; i < 10 && tickerHead; i++) {
658         tickerHead.show();
659         tickerHead = tickerHead.nextSibling;
660     }
661     tickerTail = $('latest-posts').firstChild;
662     tickerTimer = setInterval(ticker, 5000);
663 }
664
665 function stopTicker() {
666     if (tickerTimer)
667         clearInterval(tickerTimer);
668     tickerTimer = null;
669 }
670
671 function loadLatest() {
672     new Ajax.Request(baseURL + '/latest.json', {
673         onSuccess: function(r) {
674             var j = r.responseText.evalJSON();
675
676             $('latest-tags').update();
677             j.tags.each(function(v) {
678                 var a = new Element('a', {href: '/#/tag/' + v});
679                 a.insert('#' + v);
680                 a.onclick = "return qlink()";
681                 a.className = 'ref';
682                 $('latest-tags').insert(a);
683                 $('latest-tags').appendChild(document.createTextNode(' '));
684             });
685
686             $('latest-posts').update();
687             j.records.each(function(v) {
688                 v.data = v.data.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
689                 v.date = (new Date(v.timestamp * 1000)).toString();
690                 var html = latestRecordsTemplate.evaluate(v);
691                 $('latest-posts').insert(html);
692             });
693             startTicker();
694         }
695     });
696 }
697
698 function qlink(loc) {
699     if (loc) {
700         location.hash = loc;
701     } else if (event && event.target) {
702         location.href = event.target.href;
703     } else {
704         // Bogus qlink
705         return;
706     }
707     hashSwitch();
708     return false;
709 }
710
711 function hashSwitch() {
712     var m;
713     stopTicker();
714     if (m = location.search.match(/^\?post\/([^/]+)\/(.+)/)) {
715         $('post').show();
716         $('post.content').value = '[' + decodeURIComponent(m[1]).replace(']','').replace('[','') + '](' + decodeURIComponent(m[2]) + ')';
717     } else if (m = location.hash.match(/^#\/(ref|tag)\/([A-Za-z0-9_-]+)(?:\/p(\d+))?$/)) {
718         if (!currentPager || !(currentPager instanceof Tag) || currentPager.type != m[1] || currentPager.tag != m[2])
719             currentPager = new Tag(m[1], m[2]);
720         currentPager.show();
721
722         if (m[3]) {
723             var r = parseInt(m[3]);
724             currentPager.showPageAt(r);
725         } else {
726             currentPager.showPageAt(currentPager.itemCount - 1);
727         }
728     } else if (m = location.hash.match(/^#\/feed(?:\/p(\d+))?$/)) {
729         if (loginStatus.loggedIn) {
730             if (!currentPager || !(currentPager instanceof Feed) || currentPager.username != loginStatus.username)
731                 currentPager = new Feed();
732             currentPager.show();
733             if (m[3]) {
734                 var r = parseInt(m[3]);
735                 currentPager.showPageAt(r);
736             } else {
737                 currentPager.showPageAt(currentPager.itemCount - 1);
738             }
739         } else {
740             location.href = baseURL;
741         }
742     } else if (m = location.hash.match(/^#([A-Za-z0-9_-]+)(?:\/(p)?(\d+))?$/)) {
743         if (!currentPager || !(currentPager instanceof User) || currentPager.username != m[1])
744             currentPager = new User(m[1]);
745         currentPager.show();
746         loginStatus.update();
747
748         if (m[3]) {
749             var r = parseInt(m[3]);
750             if (m[2] == 'p') {
751                 currentPager.showPageAt(r);
752             } else {
753                 currentPager.showRecord(r);
754             }
755         } else {
756             currentPager.reload();
757         }
758     } else {
759         $$('[name=section]').each(function(v) { v.update('Welcome') });
760         $('signup').hide();
761         items.update();
762         items.hide();
763         $('newer_link').hide();
764         $('older_link').hide();
765         $('welcome').show();
766         $('rss').hide();
767         $('usercontrols').hide();
768         loadLatest();
769     }
770 }
771
772 var lastHash;
773 function hashCheck() {
774     if (location.hash != lastHash) {
775         lastHash = location.hash;
776         hashSwitch();
777     }
778 }
779
780 function init() {
781     items = $('items');
782     loginStatus = new LoginStatus();
783
784     lastHash = location.hash;
785     hashSwitch();
786
787     setInterval(hashCheck, 250);
788
789     document.body.observe('keyup', function(event) {
790         if (event.shiftKey && event.keyCode == 32) {
791             postPopup();
792             event.stop();
793         }
794     });
795     $('post.content').addEventListener('keyup', function(event) {
796         event.stopPropagation();
797     }, true);
798 }