

function curry(fn, scope)
{
	var scope = scope || window;
	var args = Array.prototype.slice.call(arguments, 2) || [];

	return function()
	{
		fn.apply(scope, args);
	};
};

var rating = function(id, value)
{
	this.construct.apply(this, arguments);
};

rating.prototype =
{
	construct: function(id, stars, value, rerate, rated)
	{
		this.__timeout = -1;
		this.__listeners = {};
		this.__timeouts = {};
		this.id = id;
		this.imageOff = PINT.themeRootDirectory + '/images/blank.gif';
		this.imageOn = PINT.themeRootDirectory + '/images/gSelected.gif';
		this.imageOut = PINT.themeRootDirectory + '/images/bSelected.gif';
		this.timeout = 0;
		this.value = value;
		this.stars = stars;
		this.rerate = rerate; 
		this.rated = rated;
		
		var outsideEl = document.getElementById(id);
		
		for(i=1; i<=this.stars; i++){
			var imgID = this.id+'img'+i;
			var newimg = document.createElement('img');
			newimg.id = imgID;
			newimg.src = this.imageOff;
			
			outsideEl.appendChild(newimg);
			
			if (!this.rated) {
				YAHOO.util.Event.addListener(document.getElementById(imgID), "mouseover", curry(this.mouseOver, this, i)); //<--- Currying Used Here
				YAHOO.util.Event.addListener(document.getElementById(imgID), "click", curry(this.clickMethod, this, i));   //<--- Currying Used Here
			} else YAHOO.util.Dom.addClass(this.id, 'rated');
		}

		this.addMethodListener("mouseOut", this.id, "mouseout");
		
		this.renderStars(this.value, false);
	},
	
	addMethodListener: function(method, el, event)
	{
		var that = this;
	
		this.__listeners["method:" + name + ":" + el.id + ":" + event] = function()
		{
			that[method].apply(that, arguments);
		};
	
		YAHOO.util.Event.addListener(el, event, this.__listeners["method:" + name + ":" + el.id + ":" + event]);
	},

	mouseOver: function(rating)
	{
		this.clearTimeout(this.__timeout);
		this.__timeout = -1;

		this.renderStars(rating, true);
	},
	
	clickMethod: function(rating)
	{
		this.value = rating;
		this.onClick(rating);
	},

	mouseOut: function()
	{
		this.clearTimeout(this.__timeout);
		this.__timeout = this.setTimeout('onTimeOut',this.timeout);
	},
	
	onTimeOut: function()
	{	
		if(this.__timeout != -1)
		{
			this.renderStars(this.value, false);
		}
	},

	renderStars: function(units, startColor)
	{
		for (var i = 1; i <= units; i++)
		{
			if(startColor == true)
			{
				document.getElementById(this.id + "img" + i).src = this.imageOn;
			}
			else
			{
				document.getElementById(this.id + "img" + i).src = this.imageOut;
			}
		}

		for (i = units + 1; i <= this.stars; i++)
		{
			document.getElementById(this.id + "img" + i).src = this.imageOff;
		}
	},

	setTimeout: function(method, period)
	{
		this.clearTimeout(method);

		var that = this;
		var args = Array.prototype.slice.call(arguments, 2) || [];

		this.__timeouts[method] = setTimeout(function()
		{
			that[method].apply(that, args);
		}, period);
	},

	clearTimeout: function(method)
	{
		if (this.__timeouts[method] > 0)
		{
			clearTimeout(this.__timeouts[method]);
			this.__timeouts[method] = 0;
		}
	},

	onClick: function(value)
	{
		this.value = value;
		
		if(!this.rerate){
			
			for(i=1; i<=this.stars; i++)
			{
				YAHOO.util.Event.removeListener(document.getElementById(this.id+'img'+i), "mouseover");
				YAHOO.util.Event.removeListener(document.getElementById(this.id+'img'+i), "click");
			}
			
			YAHOO.util.Event.removeListener(document.getElementById(this.id), "mouseout");
			
			this.renderStars(this.value, false);
		}	
	}
}
