(function($){
  try{document.execCommand("BackgroundImageCache",false,true);}catch(e){};
  $.fn.ajaxRating = function() {
    return this.each(function() {
      var $t = $(this); /* .stars */
      var activity = '';
      
      var info = null;
      var $info = function() {
        if ( info )
          return info;
        info = $('.callback', $t.parent());
        $('.indicator', info).show();
        activity = info.html();
        info.empty().show();
        return info;
      }
      
      var links = $('a', $t).each(function() {
        var href = $(this).attr('href').substr(1);
        $(this).click(function() {
          $info().removeClass('error').html(activity);
          $.get(href, {}, function(data) { 
            if( !data.success )
            {
              $info().addClass('error').html(data.error);
            }
            else if (data.result.rating)
            {
              $('.current', $t ).css('width', (parseFloat(data.result.rating)*100/links.length) + '%').show();
              $t.parent().find('span#video_votes').text(data.result.votes);
              $info().html(data.message);
              if ( data.revote != 1 )
                links.remove();
            }
          },'json');
          return false;
        }).bind('mouseover', function() { $t.children('.current').hide(); }).bind('mouseout', function() { $t.children('.current').show(); });
      });   
    });
  }
  
  $(document).ready(function(){ $('.stars').ajaxRating(); });
})(jQuery);