comparison media/js/downloads/rating.js @ 1:dbd703f7d63a

Initial import of sg101 stuff from private repository.
author gremmie
date Mon, 06 Apr 2009 02:43:12 +0000
parents
children b43e1288ff80
comparison
equal deleted inserted replaced
0:900ba3c7b765 1:dbd703f7d63a
1 function dlRatingOver(event)
2 {
3 var div = $(this).parent('div');
4 var stars = $('img', div);
5 for (var i = 0; i <= event.data; ++i)
6 {
7 var star = $(stars[i]);
8 star.attr('src', '/static/downloads/stars/rating_over.gif');
9 }
10 }
11
12 function dlRatingOut(event)
13 {
14 var div = $(this).parent('div');
15 var stars = $('img', div);
16 for (var i = 0; i <= event.data; ++i)
17 {
18 var star = $(stars[i]);
19 star.attr('src', '/static/downloads/stars/rating_' + star.attr('class') + '.gif');
20 }
21 }
22
23 function dlRatingClick(event)
24 {
25 var star = $(this);
26 var id = star.attr('id');
27 if (id.match(/star-(\d+)-(\d+)/))
28 {
29 $.post('/downloads/rate/', { id: RegExp.$1, rating: parseInt(RegExp.$2) + 1},
30 function(rating) {
31 rating = parseFloat(rating);
32 if (rating < 0)
33 {
34 alert("You've already rated this download.");
35 return;
36 }
37 alert('Thanks for rating this download!');
38 var div = star.parent('div');
39 var stars = $('img', div);
40 rating = parseFloat(rating);
41 for (var i = 0; i < 5; ++i)
42 {
43 var s = $(stars[i]);
44 s.removeClass(s.attr('class'));
45 if (rating >= 1.0)
46 {
47 s.attr('src', '/static/downloads/stars/rating_on.gif');
48 s.addClass('on')
49 rating -= 1.0;
50 }
51 else if (rating >= 0.5)
52 {
53 s.attr('src', '/static/downloads/stars/rating_half.gif');
54 s.addClass('half')
55 rating = 0;
56 }
57 else
58 {
59 s.attr('src', '/static/downloads/stars/rating_off.gif');
60 s.addClass('off')
61 }
62 }
63 },
64 'text');
65 }
66 }
67
68 $(document).ready(function() {
69 $('.rating').each(function(n) {
70 var div = $(this);
71 var id = div.attr('id');
72 var numeric_id = -1;
73 if (id.match(/rating-(\d+)/))
74 {
75 numeric_id = RegExp.$1;
76 }
77 var rating = div.html();
78 div.html('');
79 for (var i = 0; i < 5; ++i)
80 {
81 var star = $('<img />');
82 if (rating >= 1)
83 {
84 star.attr('src', '/static/downloads/stars/rating_on.gif');
85 star.addClass('on')
86 --rating;
87 }
88 else if (rating >= 0.5)
89 {
90 star.attr('src', '/static/downloads/stars/rating_half.gif');
91 star.addClass('half')
92 rating = 0;
93 }
94 else
95 {
96 star.attr('src', '/static/downloads/stars/rating_off.gif');
97 star.addClass('off')
98 }
99 star.attr('alt', 'star');
100 star.attr('id', 'star-' + numeric_id + '-' + i);
101 star.bind('mouseover', i, dlRatingOver);
102 star.bind('mouseout', i, dlRatingOut);
103 star.click(dlRatingClick);
104 div.append(star);
105 }
106 });
107 });