﻿GenericMediaGalleryPostRating = function
  (
    ratingControlID,
    ratingValueControlId,
    imagesPath,
    imageOn,
    imageOff,
    readOnlyImageOn,
    readOnlyImageOff,
    maximumRating,
    readOnly
  )
{
  this._ratingControl = document.getElementById(ratingControlID);
  this._ratingValueControl = document.getElementById(ratingValueControlId);
  this._imagesURLPath = imagesPath;
  this._imageOnPath = this._imagesURLPath + '/' + imageOn;
  this._imageOffPath = this._imagesURLPath + '/' + imageOff;
  this._readOnlyimageOnPath = this._imagesURLPath + '/' + readOnlyImageOn;
  this._readOnlyimageOffPath = this._imagesURLPath + '/' + readOnlyImageOff;
  this._maxRating = maximumRating;
  this._isReadOnly = readOnly;
  
  // Holds the current Rating.
  this._currentRating = this._ratingValueControl.value;
  
  // Gets the current rating value.
  this.GetRatingValue = function()
  {
    return document.getElementById(this._ratingValueContainer).value;
  } // GetRatingValue()
  
  // Sets the current rating value.
  this.SetRatingValue = function(value)
  {
    document.getElementById(this._ratingValueContainer).value = value;
  } // SetRatingValue()
  
  // Initialises the rating control (creates child controls for the rating images).
  this.BuildControl = function()
  {
    var i;
    
    for(i=1;i <= this._maxRating;i++)
    {
      var image = document.createElement("img");
      image.setAttribute("id", this._ratingControl.id + '_' + i);
      image.setAttribute("alt", "Rating " + i);
      
      // Only apply the element events if the control is not in read only mode.
      if(this._isReadOnly != 'true')
      {
        image.setAttribute("src", this._imageOffPath);
        image.onmouseover = new Function(this._ratingControl.id + ".Hover(" + i + ");");
        image.onmouseout = new Function(this._ratingControl.id + ".Clear();");
        image.onclick = new Function(this._ratingControl.id + ".Click('" + image.id + "', " + i + ");");
      } // End If
      else
      {
        image.setAttribute("src", this._readOnlyimageOffPath);
      } // End else
      
      this._ratingControl.appendChild(image);
    } // End for
    
    // Initialise the control with the current rating.
    this.ShowRating();
  } // BuildControl
  
  // Event handler for rating hovering.
  this.Hover = function(rating)
  {
    for(i=0;i < this._maxRating;i++)
    {
      if(i < rating)
        this._ratingControl.childNodes[i].setAttribute("src", this._imageOnPath);
      else
        this._ratingControl.childNodes[i].setAttribute("src", this._imageOffPath);
    } // End for
  } // Hover()
  
  // Event handler for rating selection.
  this.Click = function(control, rating)
  {
    document.getElementById(control).setAttribute("src", this._imageOnPath);
    this._currentRating = rating;
    this._ratingValueControl.value = this._currentRating;
  } // Click()
  
  // Event handler for clearing the current selection.
  this.Clear = function()
  {
    for(i=0;i < this._maxRating;i++)
    {
      if(i >= this._currentRating)
        this._ratingControl.childNodes[i].setAttribute("src", this._imageOffPath);
    } // End for
  } // Clear()
  
  // Sets the control to display the currently selected rating.
  this.ShowRating = function()
  {
    for(i=0;i <= this._maxRating;i++)
    {
      if(i < this._currentRating)
      {
        if(this._isReadOnly == 'true')
          this._ratingControl.childNodes[i].setAttribute("src", this._readOnlyimageOnPath);
        else
          this._ratingControl.childNodes[i].setAttribute("src", this._imageOnPath);
      } // End If
    } // End for
  } // SetRating()
  
  // Initialise the control when it is constructed.
  this.BuildControl();
}