var rating = function(id, value)
{
	this.construct.apply(this, arguments);
};

rating.prototype =
{
	construct: function(id, stars, value, rerate, hinput)
	{
		this.__timeout = -1;
		this.__listeners = {};
		this.__timeouts = {};
		this.id = id;
		this.imageOff = 'librerias/javascript/valoracion/assets/blank.gif';
		this.imageOn = 'librerias/javascript/valoracion/assets/gSelected.gif';
		this.imageOut = 'librerias/javascript/valoracion/assets/bSelected.gif';
		this.timeout = 0;
		this.stars = stars;
		this.rerate = rerate; 
		this.hinput = hinput;

		this.setValue(value);

		var outsideEl = document.getElementById(id);
		
		for(var i=1; i<=this.stars; i++)
		{
			var imgID = 'img'+i;
			var newimg = document.createElement('img');
			newimg.id = imgID;
			newimg.src = this.imageOff;
			
			outsideEl.appendChild(newimg);
			
			YAHOO.util.Event.addListener(document.getElementById(imgID), "mouseover", curry(this.mouseOver, this, i));							
			YAHOO.util.Event.addListener(document.getElementById(imgID), "click", curry(this.clickMethod, this, i));
		}

		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)
	{
		if(this.rerate)
		{
			this.clearTimeout(this.__timeout);
			this.__timeout = -1;

			this.renderStars(rating, true);
		}
	},
	
	clickMethod: function(rating)
	{
		this.onClick(rating);
	},

	mouseOut: function()
	{
		if(this.rerate)
		{
			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("img" + i).src = this.imageOn;
			}
			else
			{
				document.getElementById("img" + i).src = this.imageOut;
			}
		}

		for (i = units + 1; i <= this.stars; i++)
		{
			document.getElementById("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.setValue(value);
		document.getElementById(this.hinput).value = this.value;

		if(!this.rerate)
		{
			for(i=1; i<=this.stars; i++)
			{
				YAHOO.util.Event.removeListener(document.getElementById('img'+i), "mouseover");
				YAHOO.util.Event.removeListener(document.getElementById('img'+i), "click");
			}
			
			YAHOO.util.Event.removeListener(document.getElementById(this.id), "mouseout");
			
			this.renderStars(this.value, false);

		}	
	},
	
	setValue: function(value)
	{
		this.value = value;
	},
	
	getValue: function()
	{
		return this.value;	
	}
}
