/*
   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.
adorRatings.Msgs = new Array (
	"Poor.",
	"Okay.",
	"Good.",
	"Great.",
	"Awesome!"
);

adorRatings.Labels = new Array (
	"one star",
	"two stars",
	"three stars",
	"four stars",
	"five stars"
);

// Path for all images.
var path = "Images/";

// Image for set units.
adorRatings.UnitY = path + "star-gold.gif";

// Image for set units <= the mouse over point.
adorRatings.UnitYMouseOver = path + "star-gold.gif";

// Image for set units > the mouse over point.
adorRatings.UnitYMouseLess = path + "star-grey.gif";

// Image for unset units.
adorRatings.UnitN = path + "star-grey.gif";

// Image for unset units <= the mouse over point.
adorRatings.UnitNMouseOver = path + "star-gold.gif";

function adorRatings(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;
	this.defaultMsg = '&nbsp;';

	attributes = 'class="ygrtngs" id="' + id + '"';
	h1 = 'onMouseOut="return adorRatingsMouseOut(' + id + ');"';
	d.write('<div ' + attributes + ' ' + h1 + '>');
	d.write('<div class="msg">' +
	    '<span class="yratemsg" id="yratemsg' + id + '">' +
	    this.defaultMsg + '</span></div>');

	for (i = 1; i <= adorRatings.Msgs.length; i++) {
		h1 = 'onMouseOver="return adorRatingsMouseOver(' + id
		    + ', ' + i + ');"';
		h2 = 'onClick="return adorRatingsClick(' + id + ', '
		    + i + ');"';
		d.write('<span class="unit "' + h1 + ' ' + h2 + '>');
		d.write('<img class="stars" src="' + adorRatings.UnitN + '"'
		    + ' title="' + adorRatings.Msgs[i-1] + '" />');
		d.write('</span>');
	}

	//d.write('<input type="hidden" name="' + id +  '_" value="" />');    
	d.write('</div>');
 
	this.parent = document.getElementById(id);
	this.images = this.parent.getElementsByTagName("img");
	this.field = this.parent.childNodes[6];
	this.msg = document.getElementById("yratemsg" + id);
	this.msg.style.fontWeight = "bold"; // initially
	this.id = id;
}

function adorRatingsSet(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.defaultMsg = adorRatings.Msgs[n-1];
	this.update(n, oFlag);
	if (THIS_PAGE == "details")
		adorRatingsShowSubmit('btnSave' + this.id); 
	this.field.value = n;
}

function adorRatingsShowSubmit(sBtn)
{
	document.getElementById(sBtn).style.display = "block";
}

function adorRatingsSetMsg(m)
{
	var children = this.msg.childNodes;
	var node;

	for (var i = 0; i <  children.length; i++) {
		node = children[i];

		if (node.nodeType == 3) {
			var s = document.getElementById("yratemsg" + this.id);
			if (m == "" ||
			    m == "&nbsp;") {
			    	if ( this.defaultMsg == "&nbsp;") {
				    this.defaultMsg = "Click*";
				    node.nodeValue = "Click*";
				} else {
				node.nodeValue = this.defaultMsg;
				s.style.fontWeight = "bold";
				}
			} else {
				node.nodeValue = m;
				s.style.fontWeight = "normal";
			}
		}
	}
}

function adorRatingsGet()
{
	return this.rating;
}

function adorRatingsUpdate(n, oFlag)
{
	// The oFlag parameter is true when the mouse is outside of the ratings
	// component.
	if (oFlag)
		this.setMsg(this.defaultMsg);
	else
		this.setMsg(adorRatings.Msgs[n - 1]);

	for (i = 1; i <= adorRatings.Msgs.length; i++) {
		if (oFlag) {
			if (i <= this.rating)
				this.images[i - 1].src = adorRatings.UnitY;
			else
				this.images[i - 1].src = adorRatings.UnitN;
		} else {
			if (i <= n) {
				if (i <= this.rating)
					this.images[i - 1].src =
					    adorRatings.UnitYMouseOver;
				else
					this.images[i - 1].src =
					    adorRatings.UnitNMouseOver;
			} else {
				if (i <= this.rating)
					this.images[i - 1].src =
					    adorRatings.UnitYMouseLess;
				else
					this.images[i - 1].src =
					    adorRatings.UnitN;
			}
		}
	}

	return true;
}

function adorRatingsMouseOver(obj, n)
{
	obj.update(n, false);
	return true;
}

function adorRatingsMouseOut(obj)
{
	obj.update(0, true);
	return true;
}

adorRatings.prototype.set = adorRatingsSet;
adorRatings.prototype.setMsg = adorRatingsSetMsg;
adorRatings.prototype.get = adorRatingsGet;
adorRatings.prototype.update = adorRatingsUpdate;
