/*
   Class that defines a standard ratings component.
*/

// Messages for display above the individual ratings units. The length of the
// array also defines how many units to include in the component.

yg_Ratings.Msgs = new Array
(
   "Poor",
   "Fair",
   "Good",
   "Very Good",
   "Excellent"
);

// Path for all images.
var path = "http://i.yimg.jp/images/mobile/widgets/pc/stars/";

// Image for set units.
yg_Ratings.UnitY = path + "mc40_star1.gif";

// Image for set units <= the mouse over point.
yg_Ratings.UnitYMouseOver = path + "mc40_star2.gif";

// Image for set units > the mouse over point.
yg_Ratings.UnitYMouseLess = path + "mc40_star3.gif";

// Image for unset units.
yg_Ratings.UnitN = path + "mc40_star4.gif";

// Image for unset units <= the mouse over point.
yg_Ratings.UnitNMouseOver = path + "mc40_star5.gif";

function yg_Ratings(id,prm_class,prm_heading)
{
   // The id parameter is the name (a string) of the variable to which the
   // instance is assigned. (The variable is sent along to event handlers,
   // so it must be in the global scope.)
   var i, t;
   var attributes;
   var h1, h2;
   var d = document;

   this.rating = 0;

   attributes = 'class="'+ prm_class +'" id="' + id + '"';
   h1 = 'onmouseout="return yg_Ratings_mouseOut(' + id + ');"';

   d.write('<tr ' + attributes + ' ' + h1 + '>\n');
   d.write('<td>'+ prm_heading +'</td>\n');

   for (i = 1; i <= 5; i++)
   {
      h1 = 'onMouseOver="return yg_Ratings_mouseOver(' + id + ', ' + i + ');"';
      h2 = 'onClick="return yg_Ratings_click(' + id + ', ' + i + ');"';
      d.write('<td><span ' + h1 + ' ' + h2 + '>');
      d.write('<img src="' + yg_Ratings.UnitN + '" width="16" height="15" hspace="1" alt="">');
      d.write('</span></td>\n');
   }
   d.write('<td><input type="hidden" name="' + id +'_val" value="0"></td>\n');
   d.write('</tr>');

   this.parent = document.getElementById(id);
   this.images = this.parent.getElementsByTagName("img");
   this.inputs = this.parent.getElementsByTagName("input");
   this.msg = this.parent.getElementsByTagName("tr")[0];
}

function yg_Ratings_set(n, oflag)
{
   // The n parameter is the unit to set (starting at 1). Set oflag to true
   // when the mouse is outside of the ratings component, or you're not sure.
   if (arguments.length < 2)
      oflag = true;

   this.rating = n;
   this.update(n, oflag);
}

function yg_Ratings_setMsg(m)
{
   var children = this.msg.childNodes;
   var node;

   for (var i = 0; i <  children.length; i++)
   {
      node = children[i];

      if (node.nodeType == 3)
      {
         // Using 0xA0 prevents the browser from collapsing empty messages.
         if (m == "")
            node.nodeValue = unescape("%A0");
         else
            node.nodeValue = m;
      }
   }
}

function yg_Ratings_get()
{
   return this.rating;
}

function yg_Ratings_update(n, oflag)
{
   // The oflag parameter is true when the mouse is outside of the ratings
   // component. The n parameter is the 

   this.inputs[0].setAttribute("value", this.rating);
   for (i = 1; i <= 5; i++)
   {
      if (oflag)
      {
         if (i <= this.rating)
            this.images[i - 1].src = yg_Ratings.UnitY;
         else
            this.images[i - 1].src = yg_Ratings.UnitN;
      }
      else
      {
         if (i <= n)
         {
            if (i <= this.rating)
               this.images[i - 1].src = yg_Ratings.UnitYMouseOver;
            else
               this.images[i - 1].src = yg_Ratings.UnitNMouseOver;
         }
         else
         {
            if (i <= this.rating)
               this.images[i - 1].src = yg_Ratings.UnitYMouseLess;
            else
               this.images[i - 1].src = yg_Ratings.UnitN;
         }
      }
   }

   return true;
}

function yg_Ratings_click(obj, n)
{
   obj.set(n, false);
   return true;
}

function yg_Ratings_mouseOver(obj, n)
{
   obj.update(n, false);
   return true;
}

function yg_Ratings_mouseOut(obj)
{
   obj.update(0, true);
   return true;
}

yg_Ratings.prototype.set = yg_Ratings_set;
yg_Ratings.prototype.setMsg = yg_Ratings_setMsg;
yg_Ratings.prototype.get = yg_Ratings_get;
yg_Ratings.prototype.update = yg_Ratings_update;
