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