c9d5457d2e789f3b077361146648b95e88d4938f
[wolnelektury.git] / wolnelektury / media / 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         $('#registration-form').ajaxForm({
39             dataType: 'json',
40             beforeSubmit: function() {
41                 $('#registration-form input[type=submit]')
42                     .attr('disabled', 'disabled')
43                     .after('<img src="/media/img/indicator.gif" style="margin-left: 0.5em"/>');
44             },
45             success: function(response) {
46                 if (response.success) {
47                     location.reload(true);
48                 } else {
49                     $('#registration-form span.error').remove();
50                     $.each(response.errors, function(id, errors) {
51                         $('#id_registration-' + id).before('<span class="error">' + errors[0] + '</span>');
52                     });
53                     $('#registration-form input[type=submit]').removeAttr('disabled');
54                     $('#registration-form img').remove();
55                 }
56             }
57         });
58         
59         $('#login-form').ajaxForm({
60             dataType: 'json',
61             beforeSubmit: function() {
62                 $('#login-form input[type=submit]')
63                     .attr('disabled', 'disabled')
64                     .after('<img src="/media/img/indicator.gif" style="margin-left: 0.5em"/>');
65             },
66             success: function(response) {
67                 if (response.success) {
68                     location.reload(true);
69                 } else {
70                     $('#login-form span.error').remove();
71                     $.each(response.errors, function(id, errors) {
72                         $('#id_login-' + id).before('<span class="error">' + errors[0] + '</span>');
73                     });
74                     $('#login-form input[type=submit]').removeAttr('disabled');
75                     $('#login-form img').remove();
76                 }
77             }
78         });
79         
80         $('#login-register-window').jqm({
81             target: target[0],
82             overlay: 60,
83             trigger: '.login-register-link',
84             onShow: function(hash) {
85                 var offset = $(hash.t).offset();
86                 hash.w.css({position: 'absolute', left: offset.left - hash.w.width() + $(hash.t).width(), top: offset.top});
87                 $('div.header', hash.w).css({width: $(hash.t).width()});
88                 hash.w.show();
89             }
90         });
91         
92         $('ul.shelf-list li').hover(function() {
93             $(this).css({background: '#EEE', cursor: 'pointer'});
94         }, function() {
95             $(this).css({background: 'transparent'});
96         }).click(function() {
97             location.href = $('a.visit-shelf', this).attr('href');
98         });
99         
100         $('.delete-shelf').click(function() { 
101             var link = $(this);
102             var shelf_name = $('.visit-shelf', link.parent()).text();
103             if (confirm('Czy na pewno usunąć półkę ' + shelf_name + '?')) {
104                 $.post(link.attr('href'), function(data, textStatus) {
105                     link.parent().remove();
106                 });
107             }
108             return false;
109         });
110         
111         $('#user-shelves-window').jqm({
112             ajax: '@href',
113             target: $('#user-shelves-window div.target')[0],
114             overlay: 60,
115             trigger: '#user-shelves-link',
116             onShow: function(hash) {
117                 var offset = $(hash.t).offset();
118                 hash.w.css({position: 'absolute', left: offset.left - hash.w.width() + $(hash.t).width(), top: offset.top});
119                 $('div.header', hash.w).css({width: $(hash.t).width()});
120                 hash.w.show();
121             },
122             onLoad: function(hash) { 
123                 $('form', hash.w).ajaxForm({
124                     target: $('#user-shelves-window div.target'),
125                     success: function() { setTimeout(function() { $('#user-shelves-window').jqmHide() }, 1000) }
126                 });
127                 
128                 $('input', hash.w).labelify({labelledClass: 'blur'});
129                 
130                 $('ul.shelf-list li', hash.w).hover(function() {
131                     $(this).css({background: '#EEE', cursor: 'pointer'});
132                 }, function() {
133                     $(this).css({background: 'transparent'});
134                 }).click(function() {
135                     location.href = $('a.visit-shelf', this).attr('href');
136                 });
137                 
138                 $('.delete-shelf').click(function() {
139                     var link = $(this);
140                     var shelf_name = $('.visit-shelf', link.parent()).text();
141                     console.log(shelf_name);
142                     if (confirm('Czy na pewno usunąć półkę ' + shelf_name + '?')) {
143                         $.post(link.attr('href'), function(data, textStatus) {
144                             link.parent().remove();
145                         });
146                     }
147                     return false;
148                 });
149             }
150         });
151     });
152 })(jQuery)