--- jrating.module	2007-03-16 11:54:42.000000000 +0100
+++ jrating.module	2007-03-23 16:29:46.000000000 +0100
@@ -218,12 +218,12 @@
   global $user;
   $vote = new stdClass;
   
-  if ($user->uid && user_access('rate content')) {
+  if (user_access('rate content')) {
     $vote->value = max(0, min(100, $form_values['rating']));
     $vote->value_type = 'percent';
     $vote->tag = 'vote';
     
-    $vote->value ? votingapi_set_vote('node', $form_values['nid'], $vote) : votingapi_unset_vote('node', $form_values['nid']);
+    _jrating_set_vote($vote, $form_values['nid']);
     
     $node = node_load(array('nid' => $form_values['nid']));
     
@@ -249,14 +249,76 @@
   }
 }
 
+
+/**
+* Actually set the vote. The idea for this is taken from fivestar module.
+*/
+function _jrating_set_vote($vote, $nid) {
+  // Prep variables for anonymous vs. registered voting
+  global $user;
+  if ($user->uid) {
+    $uid = $user->uid;
+  }
+  else {
+    $uid = 0;
+    if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
+      $hostname = $_SERVER['HTTP_X_FORWARDED_FOR'];
+    }
+    else {
+      $hostname = $_SERVER['REMOTE_ADDR'];
+    }
+  }
+  
+  // we always cast votes on nodes
+  $type = 'node';
+  
+  // handle voting for logged in users
+  if ($uid) {
+    if ($vote->value == 0) {
+      votingapi_unset_vote($type, $nid);
+    }
+    else {
+      votingapi_set_vote($type, $nid, $vote);
+    }
+  }
+  //handle login for anonymous users
+  else {
+    // query database to find existing votes with the same IP within the past day
+    $sql = "SELECT vote_id FROM {votingapi_vote} WHERE content_type='%s' AND content_id=%d AND value_type='%s' AND uid=%d AND hostname='%s' AND timestamp > %d";
+    $result = db_query($sql, $type, $nid, $vote->value_type, $uid, $hostname, time() - (60 * 60 * 24));
+    $old_vote = db_fetch_object($result);
+    
+    if ($vote->value == 0) {
+      if ($old_vote) {
+        votingapi_delete_vote($old_vote);
+      }
+    }
+    else {
+      if ($old_vote) {
+        votingapi_change_vote($old_vote, $vote->value);
+      }
+      else {
+        votingapi_add_vote($type, $nid, $vote->value, $vote->value_type, $vote->tag, $uid);
+      }
+    }
+    
+    // for anonymous votes we have to take care to trigger the recalculation ourselves as we bypass the high-level voting api
+    if (variable_get('votingapi_calculation_schedule', 'immediate') != 'cron') {
+      votingapi_recalculate_results($type, $nid);
+    }
+  }
+}
+
 /**
 * Get user rating for a node.
 */
 function jrating_get_user_rating($nid, $uid = FALSE) {
   global $user;
-  if (!$uid)
+  if (!$uid) {
     $uid = $user->uid;
+  }
   
+  if ($uid != 0) {
     if ($votes = votingapi_get_user_votes('node', $nid, $uid)) {
       foreach ($votes as $vote) {
         if ($vote->value_type == 'percent' && $vote->tag == 'vote') {
@@ -265,13 +327,18 @@
         }
       }
     }
-    
-    $result = array(
-      'rating' => $rating ? sprintf("%.1f", round($rating->value / 20,1)) : '0.0',
-      'percent' => $rating ? $rating->value : '0',
-      );
-    
-    return $result;
+  }
+  else {
+    // anonymous users
+    $rating = 0;
+  }
+  
+  $result = array(
+    'rating' => $rating ? sprintf("%.1f", round($rating->value / 20,1)) : '0.0',
+    'percent' => $rating ? $rating->value : '0',
+    );
+  
+  return $result;
 }
 
 /**
