tag description effects (#682)
[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         $('form input').labelify({labelledClass: 'blur'});
111         
112         target = $('#login-register-window div.target');
113         
114         $('#show-registration-form').click(function() {
115             $('#login-form').hide();
116             $('#registration-form').show();
117         });
118         
119         $('#show-login-form').click(function() {
120             $('#registration-form').hide();
121             $('#login-form').show();
122         });
123         
124         // Fragments
125         $('.fragment-text').each(function() {
126             if ($(this).prev().filter('.fragment-short-text').length) {
127                 $(this).hover(
128                     function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
129                     function() { $(this).css({background: '#FFF'}); }
130                 ).click(function() {
131                     $(this).fadeOut(function() {
132                         $(this).prev().fadeIn();
133                     });
134                     return false;
135                 })
136             }
137         });
138         
139         $('.fragment-short-text').click(function() {
140             $(this).fadeOut(function() { $(this).next().fadeIn() });
141             return false;
142         }).hover(
143             function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
144             function() { $(this).css({background: '#FFF'}); }
145         );
146         
147         $('.show-all-tags').click(function() {
148             $(this).parent().parent().fadeOut(function() { 
149                 $(this).next().fadeIn();
150             });
151             return false;
152         });
153         
154         $('.hide-all-tags').click(function() {
155            $(this).parent().parent().fadeOut(function() {
156                $(this).prev().fadeIn();
157            });
158            return false; 
159         });
160         
161         $('#registration-form').ajaxForm({
162             dataType: 'json',
163             beforeSubmit: function() {
164                 $('#registration-form input[type=submit]')
165                     .attr('disabled', 'disabled')
166                     .after('<img src="/static/img/indicator.gif" style="margin-left: 0.5em"/>');
167             },
168             success: function(response) {
169                 if (response.success) {
170                     location.reload(true);
171                 } else {
172                     $('#registration-form span.error').remove();
173                     $.each(response.errors, function(id, errors) {
174                         $('#id_registration-' + id).before('<span class="error">' + errors[0] + '</span>');
175                     });
176                     $('#registration-form input[type=submit]').removeAttr('disabled');
177                     $('#registration-form img').remove();
178                 }
179             }
180         });
181         
182         $('#login-form').ajaxForm({
183             dataType: 'json',
184             beforeSubmit: function() {
185                 $('#login-form input[type=submit]')
186                     .attr('disabled', 'disabled')
187                     .after('<img src="/static/img/indicator.gif" style="margin-left: 0.5em"/>');
188             },
189             success: function(response) {
190                 if (response.success) {
191                     location.reload(true);
192                 } else {
193                     $('#login-form span.error').remove();
194                     $.each(response.errors, function(id, errors) {
195                         $('#id_login-' + id).before('<span class="error">' + errors[0] + '</span>');
196                     });
197                     $('#login-form input[type=submit]').removeAttr('disabled');
198                     $('#login-form img').remove();
199                 }
200             }
201         });
202         
203         $('#login-register-window').jqm({
204             target: target[0],
205             overlay: 60,
206             trigger: '.login-register-link',
207             onShow: function(hash) {
208                 var offset = $(hash.t).offset();
209                 hash.w.css({position: 'absolute', left: offset.left - hash.w.width() + $(hash.t).width(), top: offset.top});
210                 $('div.header', hash.w).css({width: $(hash.t).width()});
211                 hash.w.show();
212             }
213         });
214         
215         $('ul.shelf-list li').hover(function() {
216             $(this).css({background: '#EEE', cursor: 'pointer'});
217         }, function() {
218             $(this).css({background: 'transparent'});
219         }).click(function() {
220             location.href = $('a.visit-shelf', this).attr('href');
221         });
222         
223         $('.delete-shelf').click(function() { 
224             var link = $(this);
225             var shelf_name = $('.visit-shelf', link.parent()).text();
226             if (confirm(LOCALE_TEXTS[LANGUAGE_CODE]['DELETE_SHELF']+ ' '+ shelf_name + '?')) {
227                 $.post(link.attr('href'), function(data, textStatus) {
228                     link.parent().remove();
229                 });
230             }
231             return false;
232         });
233         
234         $('#user-shelves-window').jqm({
235             ajax: '@href',
236             target: $('#user-shelves-window div.target')[0],
237             overlay: 60,
238             trigger: '#user-shelves-link',
239             onShow: function(hash) {
240                 var offset = $(hash.t).offset();
241                 hash.w.css({position: 'absolute', left: offset.left - hash.w.width() + $(hash.t).width(), top: offset.top});
242                 $('div.header', hash.w).css({width: $(hash.t).width()});
243                 hash.w.show();
244             },
245             onLoad: function(hash) { 
246                 $('form', hash.w).ajaxForm({
247                     target: $('#user-shelves-window div.target'),
248                     success: function() { setTimeout(function() { $('#user-shelves-window').jqmHide() }, 1000) }
249                 });
250                 
251                 $('input', hash.w).labelify({labelledClass: 'blur'});
252                 
253                 $('ul.shelf-list li', hash.w).hover(function() {
254                     $(this).css({background: '#EEE', cursor: 'pointer'});
255                 }, function() {
256                     $(this).css({background: 'transparent'});
257                 }).click(function() {
258                     location.href = $('a.visit-shelf', this).attr('href');
259                 });
260                 
261                 $('.delete-shelf').click(function() {
262                     var link = $(this);
263                     var shelf_name = $('.visit-shelf', link.parent()).text();
264                     if (confirm(LOCALE_TEXTS[LANGUAGE_CODE]['DELETE_SHELF'] + ' ' + shelf_name + '?')) {
265                         $.post(link.attr('href'), function(data, textStatus) {
266                             link.parent().remove();
267                         });
268                     }
269                     return false;
270                 });
271             }
272         });
273
274         $('#suggest-window').jqm({
275             ajax: '@href',
276             target: $('#suggest-window div.target')[0],
277             overlay: 60,
278             trigger: '#suggest-link',
279             onShow: function(hash) {
280                 var offset = $(hash.t).offset();
281                 hash.w.css({position: 'absolute', left: offset.left - hash.w.width() + $(hash.t).width(), top: offset.top});
282                 $('div.header', hash.w).css({width: $(hash.t).width()});
283                 hash.w.show();
284             },
285             onLoad: function(hash) { 
286                 $('form', hash.w).ajaxForm({
287                                         dataType: 'json',
288                     target: $('#suggest-window div.target'),
289                     success: function(response) {
290                                                 if (response.success) {
291                                                         $('#suggest-window div.target').text(response.message);
292                             setTimeout(function() { $('#suggest-window').jqmHide() }, 1000)
293                                                 }
294                                                 else {
295                                                         $('#suggest-form .error').remove();
296                                 $.each(response.errors, function(id, errors) {
297                                     $('#suggest-form #id_' + id).before('<span class="error">' + errors[0] + '</span>');
298                                 });
299                                 $('#suggest-form input[type=submit]').removeAttr('disabled');
300                                                         return false;
301                                                 }
302                             }
303                 });
304             }
305         });
306     
307         $('#books-list .book').hover(
308             function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
309             function() { $(this).css({background: '#FFF'}); }
310         ).click(function() {
311             location.href = $('h2 a', this).attr('href');
312         });
313         
314                 function toggled_by_slide(cont, short_el, long_el, button, short_text, long_text) {
315                         function toggle(cont, short_el, long_el, button, short_text, long_text) {
316                     if (cont.hasClass('short')) {
317                         cont.animate({"height": long_el.attr("cont_h")+'px'}, {duration: "fast" }).removeClass('short');
318                         short_el.hide();
319                         long_el.show();
320                         button.html(long_text);
321                     } else {
322                         cont.animate({"height": short_el.attr("cont_h")+'px'}, {duration: "fast" }).addClass('short');
323                         long_el.hide();
324                         short_el.show();
325                         button.html(short_text);
326                     }
327                         }
328             long_el.attr("cont_h", cont.height()).hide();
329             short_el.show().attr("cont_h", cont.height());
330                         cont.addClass('short');
331                         button.html(short_text);
332                         button.hover(
333                 function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
334                 function() { $(this).css({background: '#EEE'}); }
335                         ).click(function(){
336                                 toggle(cont, short_el, long_el, button, short_text, long_text)
337                         });
338                         cont.hover(
339                 function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
340                 function() { $(this).css({background: '#FFF'}); }
341             ).click(function(){
342                 toggle(cont, short_el, long_el, button, short_text, long_text)
343             })
344                 }
345         toggled_by_slide($('#description'), $('#description-short'), $('#description-long'),  
346           $('#toggle-description p'), 
347           LOCALE_TEXTS[LANGUAGE_CODE]['EXPAND_DESCRIPTION']+' ▼',
348                   LOCALE_TEXTS[LANGUAGE_CODE]['HIDE_DESCRIPTION'] + ' ▲'
349                   );
350
351         $('#toggle-share-shelf').hover(
352             function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
353             function() { $(this).css({background: '#EEE'}); }
354         ).click(function() {
355             if ($('#share-shelf').hasClass('hidden')) {
356                 $('#share-shelf').slideDown('fast').removeClass('hidden');
357             } else {
358                 $('#share-shelf').slideUp('fast').addClass('hidden');
359             }
360         });
361
362         var target = $('#set-window div.target');
363     
364         $('#set-window').jqm({
365             ajax: '@href', 
366             target: target[0],
367             overlay: 60,
368             trigger: 'a.jqm-trigger', 
369             onShow: function(hash) { 
370                 var offset = $(hash.t).offset();
371                 target.html('<p><img src="/static/img/indicator.gif" />'+LOCALE_TEXTS[LANGUAGE_CODE]['DELETE_SHELF']+'</p>');
372                 hash.w.css({position: 'absolute', left: offset.left, top: offset.top}).show() },
373             onLoad: function(hash) { 
374                 $('form', hash.w).ajaxForm({
375                     target: target,
376                     success: function() { setTimeout(function() { $('#set-window').jqmHide() }, 1000) }
377                 });
378             }
379         });
380         
381         $('a.remove-from-shelf').click(function(event) {
382             event.preventDefault();
383             link = $(this);
384             $.post(link.attr('href'), function(data, textStatus) {
385                 link.parent().remove();
386             });
387         });
388                 
389         $('#share-shelf').hide().addClass('hidden');
390                 $('#share-shelf input').focus(function(){this.select();});
391                 
392         $('#user-info').show();
393         changeBannerText();
394         $('#onepercent-banner').show();
395         
396         var formatsDownloaded = false;
397         $('#download-shelf').click(function() {
398             $('#download-shelf-menu').slideDown('fast');
399             
400             if (!formatsDownloaded) {
401                 // Get info about the formats
402                 formatsDownloaded = true;
403                 $.ajax({
404                     url: $('#download-formats-form').attr('data-formats-feed'),
405                     type: 'GET',
406                     dataType: 'json',
407                     complete: function() {
408                         $('#download-formats-form-submit').attr('disabled', null);
409                         $('#download-formats-form-submit-li img').remove();
410                         $('#updating-formats').fadeOut('fast', function() {
411                             $('#formats-updated').fadeIn('fast');
412                         });
413                     },
414                     success: function(data) {
415                         $('#download-formats-form li').each(function() {
416                             var item = $(this);
417                             if (!!item.attr('data-format') && !data[item.attr('data-format')]) {
418                                 item.fadeOut('fast', function() {
419                                     item.remove();
420                                 });
421                             }
422                         });
423                     }
424                 });
425             }
426             return false;
427         });
428         
429         $('#download-formats-form-cancel').click(function() {
430             $('#download-shelf-menu').slideUp('fast');
431             return false;
432         });
433     });
434 })(jQuery)