/*
   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.
ip_ratings.Msgs = new Array
(
   "Poor",
   "Fair",
   "Good",
   "Very Good",
   "Excellent"
);

// Path for all images.
var path = "http://imgs.indiaproperty.com/ratingimages/";

// Image for set units.
ip_ratings.UnitY = path + "star_red16.gif";

// Image for set units <= the mouse over point.
ip_ratings.UnitYMouseOver = path + "star_g_r16.gif";

// Image for set units > the mouse over point.
ip_ratings.UnitYMouseLess = path + "star_brdr_red16.gif";

// Image for unset units.
ip_ratings.UnitN = path + "star_brdr_g16.gif";

// Image for unset units <= the mouse over point.
ip_ratings.UnitNMouseOver = path + "star_g16.gif";

function ip_ratings(id)
{
   // 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="mintxt" id="' + id + '"';
   h1 = 'onMouseOut="return ip_ratings_mouseOut(' + id + ');"';
/*
   d.write('<div ' + attributes + ' ' + h1 + '>');
   d.write('<div class="mintxt">&nbsp;</div>');
   */
   Str = '<div ' + attributes + ' ' + h1 + '>';
   Str +='<div class="mintxt">&nbsp;</div>';
	
   for (i = 1; i <= ip_ratings.Msgs.length; i++)
   {
      h1 = 'onMouseOver="return ip_ratings_mouseOver(' + id + ', ' + i + ');"';
      h2 = 'onClick="return ip_ratings_click(' + id + ', ' + i + ');"';
      Str +='<span class="mintxt"' + h1 + ' ' + h2 + '>';
      Str +='<img src="' + ip_ratings.UnitN + '" />';
      Str +='<input type="hidden" name="' + id + i +'" value="n" />';
      Str +='</span>';

   }

   Str +='</div>';
	//alert(Str)
   d.write(Str);
   
   this.parent = document.getElementById(id);
   this.images = this.parent.getElementsByTagName("img");
   this.inputs = this.parent.getElementsByTagName("input");
   this.msg = this.parent.getElementsByTagName("div")[0];
}

function ip_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 ip_ratings_setMsg(m)
{
   var children = this.msg.childNodes;
   var node;
//alert(m)
   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 ip_ratings_get()
{
   return this.rating;
}

function ip_ratings_update(n, oflag)
{
   // The oflag parameter is true when the mouse is outside of the ratings
   // component. The n parameter is the 
   if (oflag)
      this.setMsg("");
   else
      this.setMsg(ip_ratings.Msgs[n - 1]);
   for (i = 1; i <= ip_ratings.Msgs.length; i++)
   {
      
	  if (i == this.rating)
      {   this.inputs[i - 1].setAttribute("value", "y"); }
      else
      {   this.inputs[i - 1].setAttribute("value", "n"); }

      if (oflag)
      {
         if (i <= this.rating)
          {  this.images[i - 1].src = ip_ratings.UnitY; }
         else
          {  this.images[i - 1].src = ip_ratings.UnitN; }
      }
      else
      {
         if (i <= n)
         {
            if (i <= this.rating)
               this.images[i - 1].src = ip_ratings.UnitYMouseOver;
            else
               this.images[i - 1].src = ip_ratings.UnitNMouseOver;
         }
         else
         {
            if (i <= this.rating)
               this.images[i - 1].src = ip_ratings.UnitYMouseLess;
            else
               this.images[i - 1].src = ip_ratings.UnitN;
         }
      }
   }

   return true;
}

function ip_ratings_click(obj, n)
{
   obj.set(n, false);
   document.getElementById("rating").style.display = "";
   document.getElementById("rating").innerHTML = "Your Rating: " + n + " / " + "5";
   return true;
}

function ip_ratings_mouseOver(obj, n)
{
   obj.update(n, false);
   return true;
}

function ip_ratings_mouseOut(obj)
{
   obj.update(0, true);
   return true;
}

ip_ratings.prototype.set = ip_ratings_set;
ip_ratings.prototype.setMsg = ip_ratings_setMsg;
ip_ratings.prototype.get = ip_ratings_get;
ip_ratings.prototype.update = ip_ratings_update;

