bgneal@312: function dlRatingOver(event) bgneal@312: { bgneal@312: var div = $(this).parent('div'); bgneal@312: var stars = $('img', div); bgneal@312: for (var i = 0; i <= event.data; ++i) bgneal@312: { bgneal@312: var star = $(stars[i]); bgneal@312: star.attr('src', '/static/icons/stars/rating_over.gif'); bgneal@312: } bgneal@312: } bgneal@312: bgneal@312: function dlRatingOut(event) bgneal@312: { bgneal@312: var div = $(this).parent('div'); bgneal@312: var stars = $('img', div); bgneal@312: for (var i = 0; i <= event.data; ++i) bgneal@312: { bgneal@312: var star = $(stars[i]); bgneal@312: star.attr('src', '/static/icons/stars/rating_' + star.attr('class') + '.gif'); bgneal@312: } bgneal@312: } bgneal@312: bgneal@312: function dlRatingClick(event) bgneal@312: { bgneal@312: var star = $(this); bgneal@312: var id = star.attr('id'); bgneal@312: if (id.match(/star-(\d+)-(\d+)/)) bgneal@312: { bgneal@312: $.ajax({ bgneal@312: url: '/downloads/rate/', bgneal@312: type: 'POST', bgneal@312: data: { id: RegExp.$1, rating: parseInt(RegExp.$2) + 1}, bgneal@312: dataType: 'text', bgneal@312: success: function(rating) { bgneal@312: rating = parseFloat(rating); bgneal@312: if (rating < 0) bgneal@312: { bgneal@312: alert("You've already rated this download."); bgneal@312: return; bgneal@312: } bgneal@312: alert('Thanks for rating this download!'); bgneal@312: var div = star.parent('div'); bgneal@312: var stars = $('img', div); bgneal@312: rating = parseFloat(rating); bgneal@312: for (var i = 0; i < 5; ++i) bgneal@312: { bgneal@312: var s = $(stars[i]); bgneal@312: s.removeClass(s.attr('class')); bgneal@312: if (rating >= 1.0) bgneal@312: { bgneal@312: s.attr('src', '/static/icons/stars/rating_on.gif'); bgneal@312: s.addClass('on') bgneal@312: rating -= 1.0; bgneal@312: } bgneal@312: else if (rating >= 0.5) bgneal@312: { bgneal@312: s.attr('src', '/static/icons/stars/rating_half.gif'); bgneal@312: s.addClass('half') bgneal@312: rating = 0; bgneal@312: } bgneal@312: else bgneal@312: { bgneal@312: s.attr('src', '/static/icons/stars/rating_off.gif'); bgneal@312: s.addClass('off') bgneal@312: } bgneal@312: } bgneal@312: }, bgneal@312: error: function (xhr, textStatus, ex) { bgneal@312: alert('Oops, an error occurred. ' + xhr.statusText + ' - ' + bgneal@312: xhr.responseText); bgneal@312: } bgneal@312: }); bgneal@312: } bgneal@312: } bgneal@312: bgneal@312: $(document).ready(function() { bgneal@312: $('.rating').each(function(n) { bgneal@312: var div = $(this); bgneal@312: var id = div.attr('id'); bgneal@312: var numeric_id = -1; bgneal@312: if (id.match(/rating-(\d+)/)) bgneal@312: { bgneal@312: numeric_id = RegExp.$1; bgneal@312: } bgneal@312: var rating = div.html(); bgneal@312: div.html(''); bgneal@312: for (var i = 0; i < 5; ++i) bgneal@312: { bgneal@312: var star = $(''); bgneal@312: if (rating >= 1) bgneal@312: { bgneal@312: star.attr('src', '/static/icons/stars/rating_on.gif'); bgneal@312: star.addClass('on') bgneal@312: --rating; bgneal@312: } bgneal@312: else if (rating >= 0.5) bgneal@312: { bgneal@312: star.attr('src', '/static/icons/stars/rating_half.gif'); bgneal@312: star.addClass('half') bgneal@312: rating = 0; bgneal@312: } bgneal@312: else bgneal@312: { bgneal@312: star.attr('src', '/static/icons/stars/rating_off.gif'); bgneal@312: star.addClass('off') bgneal@312: } bgneal@312: star.attr('alt', 'star'); bgneal@312: star.attr('id', 'star-' + numeric_id + '-' + i); bgneal@312: star.bind('mouseover', i, dlRatingOver); bgneal@312: star.bind('mouseout', i, dlRatingOut); bgneal@312: star.click(dlRatingClick); bgneal@312: div.append(star); bgneal@312: } bgneal@312: }); bgneal@312: });