Merge branch 'production'
[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": "Möchtest du wirklich dieses Bücherregal entfernen?",
10         "HIDE_DESCRIPTION": "Beschreibung zuklappen",
11         "EXPAND_DESCRIPTION": "Beschreibung aufklappen",
12         "LOADING": "Herunterladen"
13     },
14     "fr": {
15         "DELETE_SHELF": "Voulez-vous supprimer l'étagère  définitivement?",
16         "HIDE_DESCRIPTION": "Montrer la description",
17         "EXPAND_DESCRIPTION": "Cacher la description",
18         "LOADING": "Chargement"
19     },
20     "en": {
21         "DELETE_SHELF": "Are you sure you want to delete this shelf?",
22         "HIDE_DESCRIPTION": "Hide",
23         "EXPAND_DESCRIPTION": "Expand",
24         "LOADING": "Loading"
25     },
26     "ru": {
27         "DELETE_SHELF": "Уверены ли вы том, чтобы удалить полку?",
28         "HIDE_DESCRIPTION": "Свернуть описание",
29         "EXPAND_DESCRIPTION": "Раскрыть описание",
30         "LOADING": "Загрузка"
31     },
32     "es": {
33         "DELETE_SHELF": "¿Estás seguro que quieres borrar este estante?",
34         "HIDE_DESCRIPTION": "Esconder la descripción",
35         "EXPAND_DESCRIPTION": "Ampliar la descripción",
36         "LOADING": "Cargando"
37     },
38     "lt":{
39         "DELETE_SHELF": "Ar tikrai nori pašalinti lentną?",
40         "HIDE_DESCRIPTION": "Suvyniok aprašymą ",
41         "EXPAND_DESCRIPTION": "Išplėsk aprašymą",
42         "LOADING": "Krovimas"
43     },
44     "uk":{
45         "DELETE_SHELF": "Ви впевнені, що хочете видалити полицю?",
46         "HIDE_DESCRIPTION": "Сховати опис",
47         "EXPAND_DESCRIPTION": "Показати опис",
48         "LOADING": "Завантажується"
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         $.fn.toggle_slide = function(p) {
112             cont = $(this);
113             short_el = p['short_el'] || $(':first-child', this);
114             long_el = p['long_el'] || short_el.next();
115             button = p['button'];
116             short_text = p['short_text'],
117             long_text = p['long_text'];
118
119             var toggle_fun = function(cont, short_el, long_el, button, short_text, long_text) {
120                 var toggle = function() {
121                     if (cont.hasClass('short')) {
122                         cont.animate({"height": long_el.attr("cont_h")+'px'}, {duration: "fast" }).removeClass('short');
123                         short_el.hide();
124                         long_el.show();
125                         if (button && long_text) button.html(long_text);
126                     } else {
127                         cont.animate({"height": short_el.attr("cont_h")+'px'}, {duration: "fast" }).addClass('short');
128                         long_el.hide();
129                         short_el.show();
130                         if (button && short_text) button.html(short_text);
131                     }
132                     return false;
133                 }
134                 return toggle;
135             }
136             if (long_el.html().length <= short_el.html().length)
137                 return;
138
139             // ensure long element shown first
140             long_el.show();short_el.hide();
141             long_el.attr("cont_h", $(this).height()).hide();
142             short_el.show().attr("cont_h", $(this).height());
143             $(this).addClass('short');
144
145             if (button && short_text) button.html(short_text);
146             if (button) button.hover(
147                 function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
148                 function() { $(this).css({background: '#EEE'}); }
149             ).click(toggle_fun(cont, short_el, long_el, button, short_text, long_text));
150             short_el.hover(
151                 function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
152                 function() { $(this).css({background: '#FFF'}); }
153             ).click(toggle_fun(cont, short_el, long_el, button, short_text, long_text));
154             long_el.hover(
155                 function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
156                 function() { $(this).css({background: '#FFF'}); }
157             ).click(toggle_fun(cont, short_el, long_el, button, short_text, long_text));
158         };
159
160         $('form input').labelify({labelledClass: 'blur'});
161
162         target = $('#login-register-window div.target');
163
164         $('#show-registration-form').click(function() {
165             $('#login-form').hide();
166             $('#registration-form').show();
167         });
168
169         $('#show-login-form').click(function() {
170             $('#registration-form').hide();
171             $('#login-form').show();
172         });
173
174         // Fragments
175         $('.fragment-short-text').each(function() {
176             var fragment = $(this).closest('.fragment');
177             fragment.toggle_slide({
178                 short_el: $(this),
179                 long_el: fragment.find('.fragment-text')
180             })
181         });
182
183         $('.show-all-tags').click(function() {
184             $(this).parent().parent().fadeOut(function() {
185                 $(this).next().fadeIn();
186             });
187             return false;
188         });
189
190         $('.hide-all-tags').click(function() {
191            $(this).parent().parent().fadeOut(function() {
192                $(this).prev().fadeIn();
193            });
194            return false;
195         });
196
197         $('#registration-form').ajaxForm({
198             dataType: 'json',
199             beforeSubmit: function() {
200                 $('#registration-form input[type=submit]')
201                     .attr('disabled', 'disabled')
202                     .after('<img src="/static/img/indicator.gif" style="margin-left: 0.5em"/>');
203             },
204             success: function(response) {
205                 if (response.success) {
206                     location.reload(true);
207                 } else {
208                     $('#registration-form span.error').remove();
209                     $.each(response.errors, function(id, errors) {
210                         $('#id_registration-' + id).before('<span class="error">' + errors[0] + '</span>');
211                     });
212                     $('#registration-form input[type=submit]').removeAttr('disabled');
213                     $('#registration-form img').remove();
214                 }
215             }
216         });
217
218         $('#login-form').ajaxForm({
219             dataType: 'json',
220             beforeSubmit: function() {
221                 $('#login-form input[type=submit]')
222                     .attr('disabled', 'disabled')
223                     .after('<img src="/static/img/indicator.gif" style="margin-left: 0.5em"/>');
224             },
225             success: function(response) {
226                 if (response.success) {
227                     location.reload(true);
228                 } else {
229                     $('#login-form span.error').remove();
230                     $.each(response.errors, function(id, errors) {
231                         $('#id_login-' + id).before('<span class="error">' + errors[0] + '</span>');
232                     });
233                     $('#login-form input[type=submit]').removeAttr('disabled');
234                     $('#login-form img').remove();
235                 }
236             }
237         });
238
239         $('#login-register-window').jqm({
240             target: target[0],
241             overlay: 60,
242             trigger: '.login-register-link',
243             onShow: function(hash) {
244                 var offset = $(hash.t).offset();
245                 hash.w.css({position: 'absolute', left: offset.left - hash.w.width() + $(hash.t).width(), top: offset.top});
246                 $('div.header', hash.w).css({width: $(hash.t).width()});
247                 hash.w.show();
248             }
249         });
250
251         $('ul.shelf-list li').hover(function() {
252             $(this).css({background: '#EEE', cursor: 'pointer'});
253         }, function() {
254             $(this).css({background: 'transparent'});
255         }).click(function() {
256             location.href = $('a.visit-shelf', this).attr('href');
257         });
258
259         $('.delete-shelf').click(function() {
260             var link = $(this);
261             var shelf_name = $('.visit-shelf', link.parent()).text();
262             if (confirm(LOCALE_TEXTS[LANGUAGE_CODE]['DELETE_SHELF']+ ' '+ shelf_name + '?')) {
263                 $.post(link.attr('href'), function(data, textStatus) {
264                     link.parent().remove();
265                 });
266             }
267             return false;
268         });
269
270         $('#user-shelves-window').jqm({
271             ajax: '@href',
272             target: $('#user-shelves-window div.target')[0],
273             overlay: 60,
274             trigger: '#user-shelves-link',
275             onShow: function(hash) {
276                 var offset = $(hash.t).offset();
277                 hash.w.css({position: 'absolute', left: offset.left - hash.w.width() + $(hash.t).width(), top: offset.top});
278                 $('div.header', hash.w).css({width: $(hash.t).width()});
279                 hash.w.show();
280             },
281             onLoad: function(hash) {
282                 $('form', hash.w).ajaxForm({
283                     target: $('#user-shelves-window div.target'),
284                     success: function() { setTimeout(function() { $('#user-shelves-window').jqmHide() }, 1000) }
285                 });
286
287                 $('input', hash.w).labelify({labelledClass: 'blur'});
288
289                 $('ul.shelf-list li', hash.w).hover(function() {
290                     $(this).css({background: '#EEE', cursor: 'pointer'});
291                 }, function() {
292                     $(this).css({background: 'transparent'});
293                 }).click(function() {
294                     location.href = $('a.visit-shelf', this).attr('href');
295                 });
296
297                 $('.delete-shelf').click(function() {
298                     var link = $(this);
299                     var shelf_name = $('.visit-shelf', link.parent()).text();
300                     if (confirm(LOCALE_TEXTS[LANGUAGE_CODE]['DELETE_SHELF'] + ' ' + shelf_name + '?')) {
301                         $.post(link.attr('href'), function(data, textStatus) {
302                             link.parent().remove();
303                         });
304                     }
305                     return false;
306                 });
307             }
308         });
309
310         $('#suggest-window').jqm({
311             ajax: '@href',
312             target: $('#suggest-window div.target')[0],
313             overlay: 60,
314             trigger: '#suggest-link',
315             onShow: function(hash) {
316                 var offset = $(hash.t).offset();
317                 hash.w.css({position: 'absolute', left: offset.left - hash.w.width() + $(hash.t).width(), top: offset.top});
318                 $('div.header', hash.w).css({width: $(hash.t).width()});
319                 hash.w.show();
320             },
321             onLoad: function(hash) {
322                 $('form', hash.w).ajaxForm({
323                     dataType: 'json',
324                     target: $('#suggest-window div.target'),
325                     success: function(response) {
326                         if (response.success) {
327                             $('#suggest-window div.target').text(response.message);
328                             setTimeout(function() { $('#suggest-window').jqmHide() }, 1000)
329                         }
330                         else {
331                             $('#suggest-form .error').remove();
332                             $.each(response.errors, function(id, errors) {
333                                 $('#suggest-form #id_' + id).before('<span class="error">' + errors[0] + '</span>');
334                             });
335                             $('#suggest-form input[type=submit]').removeAttr('disabled');
336                             return false;
337                         }
338                     }
339                 });
340             }
341         });
342
343         $('#books-list .book').hover(
344             function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
345             function() { $(this).css({background: '#FFF'}); }
346         ).click(function() {
347             location.href = $('h2 a', this).attr('href');
348         });
349
350         $('#description').each(function(){$(this).toggle_slide({
351             long_el: $('#description-long', this),
352             short_el: $('#description-short', this),
353             button: $(this).nextAll('#toggle-description').first().find('p'),
354             long_text: LOCALE_TEXTS[LANGUAGE_CODE]['HIDE_DESCRIPTION'] + ' ▲',
355             short_text: LOCALE_TEXTS[LANGUAGE_CODE]['EXPAND_DESCRIPTION'] + ' ▼'
356         })});
357
358         $('#toggle-share-shelf').hover(
359             function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
360             function() { $(this).css({background: '#EEE'}); }
361         ).click(function() {
362             if ($('#share-shelf').hasClass('hidden')) {
363                 $('#share-shelf').slideDown('fast').removeClass('hidden');
364             } else {
365                 $('#share-shelf').slideUp('fast').addClass('hidden');
366             }
367         });
368
369         var target = $('#set-window div.target');
370
371         $('#set-window').jqm({
372             ajax: '@href',
373             target: target[0],
374             overlay: 60,
375             trigger: 'a.jqm-trigger',
376             onShow: function(hash) {
377                 var offset = $(hash.t).offset();
378                 target.html('<p><img src="/static/img/indicator.gif" />'+LOCALE_TEXTS[LANGUAGE_CODE]['DELETE_SHELF']+'</p>');
379                 hash.w.css({position: 'absolute', left: offset.left, top: offset.top}).show() },
380             onLoad: function(hash) {
381         try {
382             $('#createShelfTrigger').click(function(){
383                 $('#createNewShelf').show();
384             });
385         } catch (e){}
386
387                 $('form', hash.w).ajaxForm({
388                     target: target,
389                     success: function() {
390             setTimeout(function() {
391                     $('#set-window').jqmHide();
392                        }, 1000)}
393                 });
394             }
395         });
396
397         $('a.remove-from-shelf').click(function(event) {
398             event.preventDefault();
399             link = $(this);
400             $.post(link.attr('href'), function(data, textStatus) {
401                 link.parent().remove();
402             });
403         });
404
405         $('#share-shelf').hide().addClass('hidden');
406         $('#share-shelf input').focus(function(){this.select();});
407
408         $('#user-info').show();
409         changeBannerText();
410         $('#onepercent-banner').show();
411
412         var formatsDownloaded = false;
413         $('#download-shelf').click(function() {
414             $('#download-shelf-menu').slideDown('fast');
415
416             if (!formatsDownloaded) {
417                 // Get info about the formats
418                 formatsDownloaded = true;
419                 $.ajax({
420                     url: $('#download-formats-form').attr('data-formats-feed'),
421                     type: 'GET',
422                     dataType: 'json',
423                     complete: function() {
424                         $('#download-formats-form-submit').attr('disabled', null);
425                         $('#download-formats-form-submit-li img').remove();
426                         $('#updating-formats').fadeOut('fast', function() {
427                             $('#formats-updated').fadeIn('fast');
428                         });
429                     },
430                     success: function(data) {
431                         $('#download-formats-form li').each(function() {
432                             var item = $(this);
433                             if (!!item.attr('data-format') && !data[item.attr('data-format')]) {
434                                 item.fadeOut('fast', function() {
435                                     item.remove();
436                                 });
437                             }
438                         });
439                     }
440                 });
441             }
442             return false;
443         });
444
445         $('#download-formats-form-cancel').click(function() {
446             $('#download-shelf-menu').slideUp('fast');
447             return false;
448         });
449
450         $('marquee').marquee().mouseover(function () {
451             $(this).trigger('stop');
452         }).mouseout(function () {
453             $(this).trigger('start');
454              $(this).data('drag', false);
455         }).mousemove(function (event) {
456             if ($(this).data('drag') == true) {
457                 this.scrollLeft = $(this).data('scrollX') + ($(this).data('x') - event.clientX);
458             }
459         }).mousedown(function (event) {
460             $(this).data('drag', true).data('x', event.clientX).data('scrollX', this.scrollLeft);
461         }).mouseup(function () {
462             $(this).data('drag', false);
463         });
464
465     });
466 })(jQuery)