scroll fragments, 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         $.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 = function() {
120                 if (cont.hasClass('short')) {
121                     cont.animate({"height": long_el.attr("cont_h")+'px'}, {duration: "fast" }).removeClass('short');
122                     short_el.hide();
123                     long_el.show();
124                     if (button && long_text) button.html(long_text);
125                 } else {
126                     cont.animate({"height": short_el.attr("cont_h")+'px'}, {duration: "fast" }).addClass('short');
127                     long_el.hide();
128                     short_el.show();
129                     if (button && short_text) button.html(short_text);
130                 }
131                 return false;
132             }
133             if (long_el.html().length <= short_el.html().length)
134                 return;
135
136             // ensure long element shown first
137             long_el.show();short_el.hide();
138             long_el.attr("cont_h", $(this).height()).hide();
139             short_el.show().attr("cont_h", $(this).height());
140             $(this).addClass('short');
141
142             if (button && short_text) button.html(short_text);
143             if (button) button.hover(
144                 function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
145                 function() { $(this).css({background: '#EEE'}); }
146             ).click(toggle);
147             short_el.hover(
148                 function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
149                 function() { $(this).css({background: '#FFF'}); }
150             ).click(toggle);
151             long_el.hover(
152                 function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
153                 function() { $(this).css({background: '#FFF'}); }
154             ).click(toggle);
155         };
156
157         $('form input').labelify({labelledClass: 'blur'});
158
159         target = $('#login-register-window div.target');
160
161         $('#show-registration-form').click(function() {
162             $('#login-form').hide();
163             $('#registration-form').show();
164         });
165
166         $('#show-login-form').click(function() {
167             $('#registration-form').hide();
168             $('#login-form').show();
169         });
170
171         // Fragments
172         $('.fragment-short-text').each(function() {
173             var fragment = $(this).closest('.fragment');
174             fragment.toggle_slide({
175                 short_el: $(this),
176                 long_el: fragment.find('.fragment-text')
177             })
178         });
179
180         $('.show-all-tags').click(function() {
181             $(this).parent().parent().fadeOut(function() {
182                 $(this).next().fadeIn();
183             });
184             return false;
185         });
186
187         $('.hide-all-tags').click(function() {
188            $(this).parent().parent().fadeOut(function() {
189                $(this).prev().fadeIn();
190            });
191            return false;
192         });
193
194         $('#registration-form').ajaxForm({
195             dataType: 'json',
196             beforeSubmit: function() {
197                 $('#registration-form input[type=submit]')
198                     .attr('disabled', 'disabled')
199                     .after('<img src="/static/img/indicator.gif" style="margin-left: 0.5em"/>');
200             },
201             success: function(response) {
202                 if (response.success) {
203                     location.reload(true);
204                 } else {
205                     $('#registration-form span.error').remove();
206                     $.each(response.errors, function(id, errors) {
207                         $('#id_registration-' + id).before('<span class="error">' + errors[0] + '</span>');
208                     });
209                     $('#registration-form input[type=submit]').removeAttr('disabled');
210                     $('#registration-form img').remove();
211                 }
212             }
213         });
214
215         $('#login-form').ajaxForm({
216             dataType: 'json',
217             beforeSubmit: function() {
218                 $('#login-form input[type=submit]')
219                     .attr('disabled', 'disabled')
220                     .after('<img src="/static/img/indicator.gif" style="margin-left: 0.5em"/>');
221             },
222             success: function(response) {
223                 if (response.success) {
224                     location.reload(true);
225                 } else {
226                     $('#login-form span.error').remove();
227                     $.each(response.errors, function(id, errors) {
228                         $('#id_login-' + id).before('<span class="error">' + errors[0] + '</span>');
229                     });
230                     $('#login-form input[type=submit]').removeAttr('disabled');
231                     $('#login-form img').remove();
232                 }
233             }
234         });
235
236         $('#login-register-window').jqm({
237             target: target[0],
238             overlay: 60,
239             trigger: '.login-register-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         });
247
248         $('ul.shelf-list li').hover(function() {
249             $(this).css({background: '#EEE', cursor: 'pointer'});
250         }, function() {
251             $(this).css({background: 'transparent'});
252         }).click(function() {
253             location.href = $('a.visit-shelf', this).attr('href');
254         });
255
256         $('.delete-shelf').click(function() {
257             var link = $(this);
258             var shelf_name = $('.visit-shelf', link.parent()).text();
259             if (confirm(LOCALE_TEXTS[LANGUAGE_CODE]['DELETE_SHELF']+ ' '+ shelf_name + '?')) {
260                 $.post(link.attr('href'), function(data, textStatus) {
261                     link.parent().remove();
262                 });
263             }
264             return false;
265         });
266
267         $('#user-shelves-window').jqm({
268             ajax: '@href',
269             target: $('#user-shelves-window div.target')[0],
270             overlay: 60,
271             trigger: '#user-shelves-link',
272             onShow: function(hash) {
273                 var offset = $(hash.t).offset();
274                 hash.w.css({position: 'absolute', left: offset.left - hash.w.width() + $(hash.t).width(), top: offset.top});
275                 $('div.header', hash.w).css({width: $(hash.t).width()});
276                 hash.w.show();
277             },
278             onLoad: function(hash) {
279                 $('form', hash.w).ajaxForm({
280                     target: $('#user-shelves-window div.target'),
281                     success: function() { setTimeout(function() { $('#user-shelves-window').jqmHide() }, 1000) }
282                 });
283
284                 $('input', hash.w).labelify({labelledClass: 'blur'});
285
286                 $('ul.shelf-list li', hash.w).hover(function() {
287                     $(this).css({background: '#EEE', cursor: 'pointer'});
288                 }, function() {
289                     $(this).css({background: 'transparent'});
290                 }).click(function() {
291                     location.href = $('a.visit-shelf', this).attr('href');
292                 });
293
294                 $('.delete-shelf').click(function() {
295                     var link = $(this);
296                     var shelf_name = $('.visit-shelf', link.parent()).text();
297                     if (confirm(LOCALE_TEXTS[LANGUAGE_CODE]['DELETE_SHELF'] + ' ' + shelf_name + '?')) {
298                         $.post(link.attr('href'), function(data, textStatus) {
299                             link.parent().remove();
300                         });
301                     }
302                     return false;
303                 });
304             }
305         });
306
307         $('#suggest-window').jqm({
308             ajax: '@href',
309             target: $('#suggest-window div.target')[0],
310             overlay: 60,
311             trigger: '#suggest-link',
312             onShow: function(hash) {
313                 var offset = $(hash.t).offset();
314                 hash.w.css({position: 'absolute', left: offset.left - hash.w.width() + $(hash.t).width(), top: offset.top});
315                 $('div.header', hash.w).css({width: $(hash.t).width()});
316                 hash.w.show();
317             },
318             onLoad: function(hash) {
319                 $('form', hash.w).ajaxForm({
320                                         dataType: 'json',
321                     target: $('#suggest-window div.target'),
322                     success: function(response) {
323                                                 if (response.success) {
324                                                         $('#suggest-window div.target').text(response.message);
325                             setTimeout(function() { $('#suggest-window').jqmHide() }, 1000)
326                                                 }
327                                                 else {
328                                                         $('#suggest-form .error').remove();
329                                 $.each(response.errors, function(id, errors) {
330                                     $('#suggest-form #id_' + id).before('<span class="error">' + errors[0] + '</span>');
331                                 });
332                                 $('#suggest-form input[type=submit]').removeAttr('disabled');
333                                                         return false;
334                                                 }
335                             }
336                 });
337             }
338         });
339
340         $('#books-list .book').hover(
341             function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
342             function() { $(this).css({background: '#FFF'}); }
343         ).click(function() {
344             location.href = $('h2 a', this).attr('href');
345         });
346
347         $('#description').each(function(){$(this).toggle_slide({
348             long_el: $('#description-long', this),
349             short_el: $('#description-short', this),
350             button: $(this).nextAll('#toggle-description').first().find('p'),
351             long_text: LOCALE_TEXTS[LANGUAGE_CODE]['HIDE_DESCRIPTION'] + ' ▲',
352             short_text: LOCALE_TEXTS[LANGUAGE_CODE]['EXPAND_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)