function rate(ob, value) {
  id = ob.id.split('_')[2];
  new Ajax.Request('/rating/comment/'+id+'/'+value, { asynchronous: true, evalScripts: true, onFailure: errorAlert });
  if (logged_in) {
    div = $('rating_widget_'+id)
    div.replaceElement($div({ id:div.id, 'class':div.className }, 'Your rating: '+(value > 0 ? '+' : '')+value));
  }
  return false;
}
function show_comment(ob) {
  comment_id = ob.id.split('_')[3];
  $('comment_'+comment_id).show();
  $('comment_toggle_'+comment_id).hide();
  widget = $('rating_widget_'+comment_id);
  if (widget) {
    widget.show();
  }
  return false;
}
function hide_comments() {
  threshold = Number($F('comment-threshold'));
  if (threshold==null) { threshold = -3; }
  $A($('comments').getElementsByTagName('div')).each(function(div) {
    if (div.className == 'rating_widget') {
      comment_id = div.id.split('_')[2];
      score = $('score_'+comment_id).firstChild.nodeValue;
      if (score[0] == '+') { score = score.substr(1, score.length); }
      score = Number(score);
      widget = $('rating_widget_'+comment_id);
      if (score < threshold) {
        $('comment_'+comment_id).hide();
        $('comment_toggle_'+comment_id).show();
	if (widget) {
	  widget.hide();
        }
      } else {
        $('comment_'+comment_id).show();
        $('comment_toggle_'+comment_id).hide();
        if (widget) {
          widget.show();
        }
      }
    }
  });
}

Event.onReady(function() {
  hide_comments();
});
