function Ratings(score, max, scale) {
  this.enabled = true;
  this.max_value = (!isNaN(max))?max:5;
  this.min_value = 1;
  this.scale = (!isNaN(scale))?scale:10.0;
  this.score = (!isNaN(score))?score:false;
  this.id = "ratings_block_" + Math.floor(Math.random()*1000);
}

Ratings.prototype.show = function() {
  var rating = document.createElement("div");
  var container = document.createElement("div");
  container.className = "rating_block rating_block_off";
  container.id = this.id;
  if (!this.score) this.score = 0;
  score = this.score * this.max_value/this.scale;
  for (i=0; i<this.max_value; i++) {
    var rating_star = document.createElement("div");
    if ( i < parseInt(score))
      rating_star.className = "rating_star rating_star_full";
    else if ( i < score)
      rating_star.className = "rating_star rating_star_half";
    else
      rating_star.className = "rating_star";
    container.appendChild(rating_star);   
  }
  rating.appendChild(container);
  document.write(rating.innerHTML);
}

Ratings.prototype.render = function(targetID) {
  var self = this;
  var target = document.getElementById(targetID);
  if (!this.score)
    this.score = parseInt(target.getAttribute('value'));
  var container = document.createElement("div");
  container.className = "rating_block";
  container.id = this.id;
  score = this.score * this.max_value/this.scale;
  for (i=0; i<this.max_value; i++) {
    var rating_star = document.createElement("div");
    if ( i < parseInt(score))
      rating_star.className = "rating_star rating_star_full";
    else if ( i < score)
      rating_star.className = "rating_star rating_star_half";
    else
      rating_star.className = "rating_star";
    rating_star.value = (i + 1) * (this.scale/this.max_value);
    rating_star.id = this.id + "_" + i;
    rating_star.onmouseover = function() { self.highlightRating(this.value, 'rating_star_over'); }
    rating_star.onmouseout = function() { self.showScore(); }
    rating_star.onclick = function() { self.submitRating(this.value); }
    container.appendChild(rating_star);   
  }
  target.appendChild(container);
}

Ratings.prototype.setRatingIcon = function(value, type) {
  try {
    target = this.id + "_" + value;
    star = document.getElementById(target);
    star.className = type;
  } catch(e) {}
}

Ratings.prototype.highlightRating = function(value, type) {
  if (!this.enabled) return false;
  if (type == null) type="rating_star_full";
  score = value * this.max_value/this.scale;
  for (i=0; i < this.max_value; i++) {
    if (i < parseInt(score))
      this.setRatingIcon(i, "rating_star " + type);
    else if (i < score)
      this.setRatingIcon(i, "rating_star rating_star_half");
    else
      this.setRatingIcon(i, 'rating_star');
  }
}

Ratings.prototype.setRating = function(value) {
  if (!this.enabled) return false;
  score = value * this.max_value/this.scale;
  document.voteForm.normalScriptRadio[score-1].checked = true;
  this.score = value;
}

Ratings.prototype.disable = function() {
  this.enabled = false;
  document.getElementById(this.id).className = "rating_block rating_block_off";
}

Ratings.prototype.submitRating = function(value) {
  this.setRating(value);
  this.enabled = false;
  document.getElementById(this.id).className = "rating_block rating_block_set";
  document.voteForm.submit();
}

Ratings.prototype.showScore = function() {
  this.highlightRating(this.score, "rating_star rating_star_full");
}