Oddzielenie STATIC_URL od MEDIA_URL.
[wolnelektury.git] / wolnelektury / static / js / catalogue.js
1 (function($) {
2     $(function() {
3         $('form input').labelify({labelledClass: 'blur'});
4         
5         target = $('#login-register-window div.target');
6         
7         $('#show-registration-form').click(function() {
8             $('#login-form').hide();
9             $('#registration-form').show();
10         });
11         
12         $('#show-login-form').click(function() {
13             $('#registration-form').hide();
14             $('#login-form').show();
15         });
16         
17         // Fragments
18         $('.fragment-text').each(function() {
19             if ($(this).prev().filter('.fragment-short-text').length) {
20                 $(this).hover(
21                     function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
22                     function() { $(this).css({background: '#FFF'}); }
23                 ).click(function() {
24                     $(this).fadeOut(function() {
25                         $(this).prev().fadeIn()
26                     });
27                 })
28             }
29         });
30         
31         $('.fragment-short-text').click(function() {
32             $(this).fadeOut(function() { $(this).next().fadeIn() });
33         }).hover(
34             function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
35             function() { $(this).css({background: '#FFF'}); }
36         );
37         
38         $('.show-all-tags').click(function() {
39             $(this).parent().parent().fadeOut(function() { 
40                 $(this).next().fadeIn();
41             });
42             return false;
43         });
44         
45         $('.hide-all-tags').click(function() {
46            $(this).parent().parent().fadeOut(function() {
47                $(this).prev().fadeIn();
48            });
49            return false; 
50         });
51         
52         $('#registration-form').ajaxForm({
53             dataType: 'json',
54             beforeSubmit: function() {
55                 $('#registration-form input[type=submit]')
56                     .attr('disabled', 'disabled')
57                     .after('<img src="/static/img/indicator.gif" style="margin-left: 0.5em"/>');
58             },
59             success: function(response) {
60                 if (response.success) {
61                     location.reload(true);
62                 } else {
63                     $('#registration-form span.error').remove();
64                     $.each(response.errors, function(id, errors) {
65                         $('#id_registration-' + id).before('<span class="error">' + errors[0] + '</span>');
66                     });
67                     $('#registration-form input[type=submit]').removeAttr('disabled');
68                     $('#registration-form img').remove();
69                 }
70             }
71         });
72         
73         $('#login-form').ajaxForm({
74             dataType: 'json',
75             beforeSubmit: function() {
76                 $('#login-form input[type=submit]')
77                     .attr('disabled', 'disabled')
78                     .after('<img src="/static/img/indicator.gif" style="margin-left: 0.5em"/>');
79             },
80             success: function(response) {
81                 if (response.success) {
82                     location.reload(true);
83                 } else {
84                     $('#login-form span.error').remove();
85                     $.each(response.errors, function(id, errors) {
86                         $('#id_login-' + id).before('<span class="error">' + errors[0] + '</span>');
87                     });
88                     $('#login-form input[type=submit]').removeAttr('disabled');
89                     $('#login-form img').remove();
90                 }
91             }
92         });
93         
94         $('#login-register-window').jqm({
95             target: target[0],
96             overlay: 60,
97             trigger: '.login-register-link',
98             onShow: function(hash) {
99                 var offset = $(hash.t).offset();
100                 hash.w.css({position: 'absolute', left: offset.left - hash.w.width() + $(hash.t).width(), top: offset.top});
101                 $('div.header', hash.w).css({width: $(hash.t).width()});
102                 hash.w.show();
103             }
104         });
105         
106         $('ul.shelf-list li').hover(function() {
107             $(this).css({background: '#EEE', cursor: 'pointer'});
108         }, function() {
109             $(this).css({background: 'transparent'});
110         }).click(function() {
111             location.href = $('a.visit-shelf', this).attr('href');
112         });
113         
114         $('.delete-shelf').click(function() { 
115             var link = $(this);
116             var shelf_name = $('.visit-shelf', link.parent()).text();
117             if (confirm('Czy na pewno usunąć półkę ' + shelf_name + '?')) {
118                 $.post(link.attr('href'), function(data, textStatus) {
119                     link.parent().remove();
120                 });
121             }
122             return false;
123         });
124         
125         $('#user-shelves-window').jqm({
126             ajax: '@href',
127             target: $('#user-shelves-window div.target')[0],
128             overlay: 60,
129             trigger: '#user-shelves-link',
130             onShow: function(hash) {
131                 var offset = $(hash.t).offset();
132                 hash.w.css({position: 'absolute', left: offset.left - hash.w.width() + $(hash.t).width(), top: offset.top});
133                 $('div.header', hash.w).css({width: $(hash.t).width()});
134                 hash.w.show();
135             },
136             onLoad: function(hash) { 
137                 $('form', hash.w).ajaxForm({
138                     target: $('#user-shelves-window div.target'),
139                     success: function() { setTimeout(function() { $('#user-shelves-window').jqmHide() }, 1000) }
140                 });
141                 
142                 $('input', hash.w).labelify({labelledClass: 'blur'});
143                 
144                 $('ul.shelf-list li', hash.w).hover(function() {
145                     $(this).css({background: '#EEE', cursor: 'pointer'});
146                 }, function() {
147                     $(this).css({background: 'transparent'});
148                 }).click(function() {
149                     location.href = $('a.visit-shelf', this).attr('href');
150                 });
151                 
152                 $('.delete-shelf').click(function() {
153                     var link = $(this);
154                     var shelf_name = $('.visit-shelf', link.parent()).text();
155                     if (confirm('Czy na pewno usunąć półkę ' + shelf_name + '?')) {
156                         $.post(link.attr('href'), function(data, textStatus) {
157                             link.parent().remove();
158                         });
159                     }
160                     return false;
161                 });
162             }
163         });
164     
165         $('#books-list .book').hover(
166             function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
167             function() { $(this).css({background: '#FFF'}); }
168         ).click(function() {
169             location.href = $('h2 a', this).attr('href');
170         });
171     
172         $('#toggle-description').hover(
173             function() { $(this).css({background: '#F3F3F3', cursor: 'pointer'}); },
174             function() { $(this).css({background: '#EEE'}); }
175         ).click(function() {
176             if ($('#description').hasClass('hidden')) {
177                 $('#description').slideDown('fast').removeClass('hidden');
178                 $('p', this).html('Zwiń opis ▲');
179             } else {
180                 $('#description').slideUp('fast').addClass('hidden');
181                 $('p', this).html('Rozwiń opis ▼');
182             }
183         });
184     
185         var target = $('#set-window div.target');
186     
187         $('#set-window').jqm({
188             ajax: '@href', 
189             target: target[0],
190             overlay: 60,
191             trigger: 'a.jqm-trigger', 
192             onShow: function(hash) { 
193                 var offset = $(hash.t).offset();
194                 target.html('<p><img src="/static/img/indicator.gif" /> Ładowanie</p>');
195                 hash.w.css({position: 'absolute', left: offset.left, top: offset.top}).show() },
196             onLoad: function(hash) { 
197                 $('form', hash.w).ajaxForm({
198                     target: target,
199                     success: function() { setTimeout(function() { $('#set-window').jqmHide() }, 1000) }
200                 });
201             }
202         });
203         
204         $('a.remove-from-shelf').click(function(event) {
205             event.preventDefault();
206             link = $(this);
207             $.post(link.attr('href'), function(data, textStatus) {
208                 link.parent().remove();
209             });
210         });
211         
212         $('#user-info').show();
213     });
214 })(jQuery)