pretty book descriptions and don't hide short descriptions
[wolnelektury.git] / wolnelektury / static / js / catalogue.js
1 var LOCALE_TEXTS = {
2         "pl": {
3                 "DELETE_SHELF": "Czy na pewno usunąć półkę",
4                 "HIDE_DESCRIPTION": "Zwiń opis",
5                 "EXPAND_DESCRIPTION": "Rozwiń opis",
6                 "LOADING": "Ładowanie"
7         },
8     "de": {
9         "DELETE_SHELF": "Translate me!",
10         "HIDE_DESCRIPTION": "Translate me!",
11         "EXPAND_DESCRIPTION": "Translate me!",
12         "LOADING": "Translate me!"
13     },
14         "fr": {
15                 "DELETE_SHELF": "Translate me!",
16                 "HIDE_DESCRIPTION": "Translate me!",
17                 "EXPAND_DESCRIPTION": "Translate me!",
18                 "LOADING": "Translate me!"
19         },
20         "en": {
21                 "DELETE_SHELF": "Translate me!",
22                 "HIDE_DESCRIPTION": "Translate me!",
23                 "EXPAND_DESCRIPTION": "Translate me!",
24                 "LOADING": "Translate me!"
25         },
26         "ru": {
27                 "DELETE_SHELF": "Translate me!",
28                 "HIDE_DESCRIPTION": "Translate me!",
29                 "EXPAND_DESCRIPTION": "Translate me!",
30                 "LOADING": "Translate me!"
31         },
32         "es": {
33                 "DELETE_SHELF": "Translate me!",
34                 "HIDE_DESCRIPTION": "Translate me!",
35                 "EXPAND_DESCRIPTION": "Translate me!",
36                 "LOADING": "Translate me!"
37         },
38     "lt":{
39         "DELETE_SHELF": "Translate me!",
40         "HIDE_DESCRIPTION": "Translate me!",
41         "EXPAND_DESCRIPTION": "Translate me!",
42         "LOADING": "Translate me!"
43     },
44     "uk":{
45         "DELETE_SHELF": "Translate me!",
46         "HIDE_DESCRIPTION": "Translate me!",
47         "EXPAND_DESCRIPTION": "Translate me!",
48         "LOADING": "Translate me!"
49     }
50 }
51 var BANNER_TEXTS = [
52     'Przekaż 1% żeby ukryć ten baner.',
53     'Jak dobrze wydać 1% swojego podatku? <strong>Poradnik dla opornych</strong>.',
54     'Wiadomość systemowa: wystąpił błąd brak funduszy. Wykonaj procedurę 1%.',
55     '<strong>FREE!</strong> Wygraj darmowe lektury!',
56     'Confidential business offer. Not scam! 1% for you.',
57     'Biblioteka Wolne Lektury wymaga aktualizacji. Kliknij dalej.',
58     '1000 lektur. <strong>1 procent</strong>.',
59     '1% dla biblioteki lektur szkolnych. 1% dla Twojej biblioteki.',
60     '1% na lektury szkolne.',
61     '1% dla wolności lektur szkolnych.',
62     'Podaruj Jeden Procent na rzecz szkolnej biblioteki internetowej.',
63     '1% podatku dla biblioteki szkolnej Wolne Lektury.',
64     '1% na rzecz darmowego dostępu do szkolnych lektur.',
65     'Żeby czytać teksty a nie skany. Przekaż 1%.',
66     'Czytaj teksty a nie skany. Przekaż 1%',
67     'Motyw artysty w literaturze - 47 cytatów. Pomóż znaleźć następne.',
68     'Twój 1% uwolni więcej lektur.',
69     'Ponad 400 motywów, blisko 10 000 000 cytatów. Pomóż znaleźć następne. Przekaż swój 1%.',
70     'Twój 1% uwolni lektury.',
71     'Rozlicz swój PIT z Wolnymi Lekturami. Skorzystaj z darmowego programu do rozliczania podatków.',
72     'Lektury 2010: Pan Tadeusz, Trylogia.',
73     'Pan Tadeusz też chce być w Internecie! Przekaż 1% swojego podatku.',
74     'Pomóż uwolnić 286 utworów z listy lektur szkolnych. Przekaż swój 1% na Wolne Lektury.'
75 ]
76
77
78 function changeBannerText() {
79     var index = Math.floor(Math.random() * BANNER_TEXTS.length);
80     if (BANNER_TEXTS[index] == $('#onepercent-text').html()) {
81         // try again
82         changeBannerText();
83     } else {
84         $('#onepercent-text').fadeOut('slow', function() {
85             $(this).html(BANNER_TEXTS[index]);
86             $(this).fadeIn('slow');
87         });
88
89         setTimeout(changeBannerText, 30 * 1000);
90     }
91 }
92
93 function autocomplete_result_handler(event, item) {
94     $(event.target).closest('form').submit();
95 }
96 function serverTime() {
97     var time = null;
98     $.ajax({url: '/katalog/zegar/',
99         async: false, dataType: 'text',
100         success: function(text) {
101             time = new Date(text);
102         }, error: function(http, message, exc) {
103             time = new Date();
104     }});
105     return time;
106 }
107
108 (function($) {
109     $(function() {
110
111         $('form input').labelify({labelledClass: 'blur'});
112
113         target = $('#login-register-window div.target');
114
115         $('#show-registration-form').click(function() {
116             $('#login-form').hide();
117             $('#registration-form').show();
118         });
119
120         $('#show-login-form').click(function() {
121             $('#registration-form').hide();
122             $('#login-form').show();
123         });
124
125         // Fragments
126         $('.fragment-text').each(function() {
127             if ($(this).prev().filter('.fragment-short-text').length) {
128                 $(this).hover(
129                     function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
130                     function() { $(this).css({background: '#FFF'}); }
131                 ).click(function() {
132                     $(this).fadeOut(function() {
133                         $(this).prev().fadeIn();
134                     });
135                     return false;
136                 })
137             }
138         });
139
140         $('.fragment-short-text').click(function() {
141             $(this).fadeOut(function() { $(this).next().fadeIn() });
142             return false;
143         }).hover(
144             function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
145             function() { $(this).css({background: '#FFF'}); }
146         );
147
148         $('.show-all-tags').click(function() {
149             $(this).parent().parent().fadeOut(function() {
150                 $(this).next().fadeIn();
151             });
152             return false;
153         });
154
155         $('.hide-all-tags').click(function() {
156            $(this).parent().parent().fadeOut(function() {
157                $(this).prev().fadeIn();
158            });
159            return false;
160         });
161
162         $('#registration-form').ajaxForm({
163             dataType: 'json',
164             beforeSubmit: function() {
165                 $('#registration-form input[type=submit]')
166                     .attr('disabled', 'disabled')
167                     .after('<img src="/static/img/indicator.gif" style="margin-left: 0.5em"/>');
168             },
169             success: function(response) {
170                 if (response.success) {
171                     location.reload(true);
172                 } else {
173                     $('#registration-form span.error').remove();
174                     $.each(response.errors, function(id, errors) {
175                         $('#id_registration-' + id).before('<span class="error">' + errors[0] + '</span>');
176                     });
177                     $('#registration-form input[type=submit]').removeAttr('disabled');
178                     $('#registration-form img').remove();
179                 }
180             }
181         });
182
183         $('#login-form').ajaxForm({
184             dataType: 'json',
185             beforeSubmit: function() {
186                 $('#login-form input[type=submit]')
187                     .attr('disabled', 'disabled')
188                     .after('<img src="/static/img/indicator.gif" style="margin-left: 0.5em"/>');
189             },
190             success: function(response) {
191                 if (response.success) {
192                     location.reload(true);
193                 } else {
194                     $('#login-form span.error').remove();
195                     $.each(response.errors, function(id, errors) {
196                         $('#id_login-' + id).before('<span class="error">' + errors[0] + '</span>');
197                     });
198                     $('#login-form input[type=submit]').removeAttr('disabled');
199                     $('#login-form img').remove();
200                 }
201             }
202         });
203
204         $('#login-register-window').jqm({
205             target: target[0],
206             overlay: 60,
207             trigger: '.login-register-link',
208             onShow: function(hash) {
209                 var offset = $(hash.t).offset();
210                 hash.w.css({position: 'absolute', left: offset.left - hash.w.width() + $(hash.t).width(), top: offset.top});
211                 $('div.header', hash.w).css({width: $(hash.t).width()});
212                 hash.w.show();
213             }
214         });
215
216         $('ul.shelf-list li').hover(function() {
217             $(this).css({background: '#EEE', cursor: 'pointer'});
218         }, function() {
219             $(this).css({background: 'transparent'});
220         }).click(function() {
221             location.href = $('a.visit-shelf', this).attr('href');
222         });
223
224         $('.delete-shelf').click(function() {
225             var link = $(this);
226             var shelf_name = $('.visit-shelf', link.parent()).text();
227             if (confirm(LOCALE_TEXTS[LANGUAGE_CODE]['DELETE_SHELF']+ ' '+ shelf_name + '?')) {
228                 $.post(link.attr('href'), function(data, textStatus) {
229                     link.parent().remove();
230                 });
231             }
232             return false;
233         });
234
235         $('#user-shelves-window').jqm({
236             ajax: '@href',
237             target: $('#user-shelves-window div.target')[0],
238             overlay: 60,
239             trigger: '#user-shelves-link',
240             onShow: function(hash) {
241                 var offset = $(hash.t).offset();
242                 hash.w.css({position: 'absolute', left: offset.left - hash.w.width() + $(hash.t).width(), top: offset.top});
243                 $('div.header', hash.w).css({width: $(hash.t).width()});
244                 hash.w.show();
245             },
246             onLoad: function(hash) {
247                 $('form', hash.w).ajaxForm({
248                     target: $('#user-shelves-window div.target'),
249                     success: function() { setTimeout(function() { $('#user-shelves-window').jqmHide() }, 1000) }
250                 });
251
252                 $('input', hash.w).labelify({labelledClass: 'blur'});
253
254                 $('ul.shelf-list li', hash.w).hover(function() {
255                     $(this).css({background: '#EEE', cursor: 'pointer'});
256                 }, function() {
257                     $(this).css({background: 'transparent'});
258                 }).click(function() {
259                     location.href = $('a.visit-shelf', this).attr('href');
260                 });
261
262                 $('.delete-shelf').click(function() {
263                     var link = $(this);
264                     var shelf_name = $('.visit-shelf', link.parent()).text();
265                     if (confirm(LOCALE_TEXTS[LANGUAGE_CODE]['DELETE_SHELF'] + ' ' + shelf_name + '?')) {
266                         $.post(link.attr('href'), function(data, textStatus) {
267                             link.parent().remove();
268                         });
269                     }
270                     return false;
271                 });
272             }
273         });
274
275         $('#suggest-window').jqm({
276             ajax: '@href',
277             target: $('#suggest-window div.target')[0],
278             overlay: 60,
279             trigger: '#suggest-link',
280             onShow: function(hash) {
281                 var offset = $(hash.t).offset();
282                 hash.w.css({position: 'absolute', left: offset.left - hash.w.width() + $(hash.t).width(), top: offset.top});
283                 $('div.header', hash.w).css({width: $(hash.t).width()});
284                 hash.w.show();
285             },
286             onLoad: function(hash) {
287                 $('form', hash.w).ajaxForm({
288                                         dataType: 'json',
289                     target: $('#suggest-window div.target'),
290                     success: function(response) {
291                                                 if (response.success) {
292                                                         $('#suggest-window div.target').text(response.message);
293                             setTimeout(function() { $('#suggest-window').jqmHide() }, 1000)
294                                                 }
295                                                 else {
296                                                         $('#suggest-form .error').remove();
297                                 $.each(response.errors, function(id, errors) {
298                                     $('#suggest-form #id_' + id).before('<span class="error">' + errors[0] + '</span>');
299                                 });
300                                 $('#suggest-form input[type=submit]').removeAttr('disabled');
301                                                         return false;
302                                                 }
303                             }
304                 });
305             }
306         });
307
308         $('#books-list .book').hover(
309             function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
310             function() { $(this).css({background: '#FFF'}); }
311         ).click(function() {
312             location.href = $('h2 a', this).attr('href');
313         });
314
315         function toggled_by_slide(cont, short_el, long_el, button, short_text, long_text) {
316             function toggle(cont, short_el, long_el, button, short_text, long_text) {
317                 if (cont.hasClass('short')) {
318                     cont.animate({"height": long_el.attr("cont_h")+'px'}, {duration: "fast" }).removeClass('short');
319                     short_el.hide();
320                     long_el.show();
321                     button.html(long_text);
322                 } else {
323                     cont.animate({"height": short_el.attr("cont_h")+'px'}, {duration: "fast" }).addClass('short');
324                     long_el.hide();
325                     short_el.show();
326                     button.html(short_text);
327                 }
328             }
329             if (long_el.html().length <= short_el.html().length)
330                 return;
331
332             long_el.attr("cont_h", cont.height()).hide();
333             short_el.show().attr("cont_h", cont.height());
334             cont.addClass('short');
335             button.html(short_text);
336             button.hover(
337                 function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
338                 function() { $(this).css({background: '#EEE'}); }
339             ).click(function(){
340                 toggle(cont, short_el, long_el, button, short_text, long_text)
341             });
342             cont.hover(
343                 function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
344                 function() { $(this).css({background: '#FFF'}); }
345             ).click(function(){
346                 toggle(cont, short_el, long_el, button, short_text, long_text)
347             })
348         }
349         toggled_by_slide($('#description'), $('#description-short'), $('#description-long'),
350           $('#toggle-description p'),
351           LOCALE_TEXTS[LANGUAGE_CODE]['EXPAND_DESCRIPTION']+' ▼',
352             LOCALE_TEXTS[LANGUAGE_CODE]['HIDE_DESCRIPTION'] + ' ▲'
353         );
354
355         $('#toggle-share-shelf').hover(
356             function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
357             function() { $(this).css({background: '#EEE'}); }
358         ).click(function() {
359             if ($('#share-shelf').hasClass('hidden')) {
360                 $('#share-shelf').slideDown('fast').removeClass('hidden');
361             } else {
362                 $('#share-shelf').slideUp('fast').addClass('hidden');
363             }
364         });
365
366         var target = $('#set-window div.target');
367
368         $('#set-window').jqm({
369             ajax: '@href',
370             target: target[0],
371             overlay: 60,
372             trigger: 'a.jqm-trigger',
373             onShow: function(hash) {
374                 var offset = $(hash.t).offset();
375                 target.html('<p><img src="/static/img/indicator.gif" />'+LOCALE_TEXTS[LANGUAGE_CODE]['DELETE_SHELF']+'</p>');
376                 hash.w.css({position: 'absolute', left: offset.left, top: offset.top}).show() },
377             onLoad: function(hash) {
378                 try {
379                         $('#createShelfTrigger').click(function(){
380                                 $('#createNewShelf').show();
381                         });
382                 } catch (e){}
383
384                 $('form', hash.w).ajaxForm({
385                     target: target,
386                     success: function() {
387                         setTimeout(function() {
388                                         $('#set-window').jqmHide();
389                                    }, 1000)}
390                 });
391             }
392         });
393
394         $('a.remove-from-shelf').click(function(event) {
395             event.preventDefault();
396             link = $(this);
397             $.post(link.attr('href'), function(data, textStatus) {
398                 link.parent().remove();
399             });
400         });
401
402         $('#share-shelf').hide().addClass('hidden');
403                 $('#share-shelf input').focus(function(){this.select();});
404
405         $('#user-info').show();
406         changeBannerText();
407         $('#onepercent-banner').show();
408
409         var formatsDownloaded = false;
410         $('#download-shelf').click(function() {
411             $('#download-shelf-menu').slideDown('fast');
412
413             if (!formatsDownloaded) {
414                 // Get info about the formats
415                 formatsDownloaded = true;
416                 $.ajax({
417                     url: $('#download-formats-form').attr('data-formats-feed'),
418                     type: 'GET',
419                     dataType: 'json',
420                     complete: function() {
421                         $('#download-formats-form-submit').attr('disabled', null);
422                         $('#download-formats-form-submit-li img').remove();
423                         $('#updating-formats').fadeOut('fast', function() {
424                             $('#formats-updated').fadeIn('fast');
425                         });
426                     },
427                     success: function(data) {
428                         $('#download-formats-form li').each(function() {
429                             var item = $(this);
430                             if (!!item.attr('data-format') && !data[item.attr('data-format')]) {
431                                 item.fadeOut('fast', function() {
432                                     item.remove();
433                                 });
434                             }
435                         });
436                     }
437                 });
438             }
439             return false;
440         });
441
442         $('#download-formats-form-cancel').click(function() {
443             $('#download-shelf-menu').slideUp('fast');
444             return false;
445         });
446     });
447 })(jQuery)