﻿function RatingSystem(){
  this.currentRating = 0;
  this.hoverRating = 0;
  this.ratingStars = new Array();
}

RatingSystem.prototype.initRatingSystem=function()
{
  for(var i=1;i<6;i++){
    this.ratingStars[i-1] = document.getElementById("rating"+i);
    this.setStarFunctions(i-1);
  }
  var temp=this;
  this.ratingStars[0].parentNode.onmouseout=function(){temp.restoreCurrentRating();}
}

RatingSystem.prototype.createRatingSystem=function(parentElement)
{
  for(var i=0;i<5;i++){
    var star = this.ratingStars[i] = document.createElement("SPAN");
    star.className = "emptyStar";
    parentElement.appendChild(star);
    star.innerHTML="&nbsp;";
    this.setStarFunctions(i);
  }
  var temp=this;
  parentElement.style.width="65px";
  parentElement.onmouseout=function(){temp.restoreCurrentRating();}
}

RatingSystem.prototype.setStarFunctions=function(i)
{
  var index = i+1;
  var temp=this;
  this.ratingStars[i].onmouseover=function(){temp.setStarHighlighting(index);}
}

RatingSystem.prototype.restoreCurrentRating=function()
{
  this.setStarHighlighting(this.currentRating);
}

RatingSystem.prototype.setStarHighlighting=function(level)
{
  var i;
  this.hoverRating = level;
  for(i=0;i<level;i++){
    this.ratingStars[i].className="filledStar";
  }
  for(;i<5;i++){
    this.ratingStars[i].className="emptyStar";
  }
}

RatingSystem.prototype.setRating=function(level)
{
  if(level==this.currentRating)return;
  this.currentRating=level;
  this.setStarHighlighting(this.currentRating);
}

RatingSystem.prototype.setReadOnly=function(value)
{
  for(var i=0;i<5;i++){
    if(value){
      this.ratingStars[i].onmouseover=function(){}
      this.ratingStars[i].onmousedown=function(){}
    }
    else {
      this.setStarFunctions(i);
    }
  }
}

RatingSystem.prototype.setRatingCallback=function(callback)
{
  var temp=this;
  for(var i=0;i<5;i++){
    this.ratingStars[i].onmousedown=function(){callback(temp.hoverRating);}
  }
}

var ratingSystem = new RatingSystem();

