function rateSystem ()
{
	// Atributos publicos:
	this.img = new Array()
	this.img['full'] = "StarFull.gif";
	this.img['half'] = "StarHalf.gif";
	this.img['none'] = "StarNone.gif";
	this.img['gold'] = "StarGold.gif";
	
	this.label = new Array();
	this.label[1] = "Péssimo";
	this.label[2] = "Ruim";
	this.label[3] = "Regular";
	this.label[4] = "Bom";
	this.label[5] = "Ótimo";

	// Atributos privados
	this.star = new Array();
	this.starRate = new Array();
	this.globalRate = 0;
	this.starLabel = false;
	
	this.request = new c_xml_http_request;
	this.request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
}


rateSystem.prototype.init = function ( id_content, rateValue, ctxId, vote )
{
	this.globalRate = rateValue;
	this.contentId = id_content;
	
	for (c = 1 ; c <= 5 ; c++)
	{
		if ( c <= this.globalRate )
			this.starRate[c] = 'full';
		else if ( c >= (this.globalRate + 1) )
			this.starRate[c] = 'none';
		else
			this.starRate[c] = 'half';
		this.makeStar(c, ctxId, vote);
	}
	
	this.starLabel = document.createElement("div");
	this.starLabel.className = "star_label";
	this.starLabel.innerHTML = "";
	
	myCtx = document.getElementById(ctxId);
	myCtx.appendChild(this.starLabel);

}


rateSystem.prototype.makeStar = function ( starId, ctxId, vote )
{
	var myStar = document.createElement("img");
	myStar.src = "/images/" + this.img[this.starRate[starId]];
	myStar.id = "rate_star_" + starId;
	if (vote)
	{
			myStar.style.cursor = 'pointer';
			
			if (myStar.onmouseover == null)
				myStar.onmouseover = function () { rating.selectStar(this); };
			else
				myStar.setAttribute("onMouseOver","rating.selectStar(this);");

			if (myStar.onmouseout == null)
				myStar.onmouseout = function () { rating.unselectStar(); };
			else
				myStar.setAttribute("onMouseOut","rating.unselectStar();");

			if (myStar.onclick == null)
				myStar.onclick = function () { rating.voteStar(this); };
			else
				myStar.setAttribute("onClick","rating.voteStar(this);");
	}
	
	myCtx = document.getElementById(ctxId);
	myCtx.appendChild(myStar);
	this.star[starId] = myStar;
}

rateSystem.prototype.selectStar = function ( star )
{
	var n = star.id.toString().split("_");
	var starId = n[2];

	for (c = 1 ; c <= 5 ; c++)
	{
		if ( c <= starId )
			this.star[c].src = "/images/" + this.img['gold'];
		else 
			this.star[c].src = "/images/" + this.img['none'];
	}
	
	this.starLabel.innerHTML = this.label[starId];
}

rateSystem.prototype.unselectStar = function ( )
{
	for (c = 1 ; c <= 5 ; c++)
	{
		if ( c <= this.globalRate )
			this.star[c].src = "/images/" + this.img['full'];
		else if ( c >= (this.globalRate + 1) )
			this.star[c].src = "/images/" + this.img['none'];
		else
			this.star[c].src = "/images/" + this.img['half'];
	}

	this.starLabel.innerHTML = "";
}

rateSystem.prototype.voteStar = function ( star )
{
	var n = star.id.toString().split("_");
	var starId = n[2];

	this.starLabel.innerHTML = "Votando...";

	for (c = 1 ; c <= 5 ; c++)
	{
		var myStar = this.star[c];
		
		myStar.style.cursor = 'default';
		
		if (myStar.onmouseover)
			myStar.onmouseover = function () {  };
		else
			myStar.setAttribute("onMouseOver","");

		if (myStar.onmouseout)
			myStar.onmouseout = function () {  };
		else
			myStar.setAttribute("onMouseOut","");

		if (myStar.onclick)
			myStar.onclick = function () {  };
		else
			myStar.setAttribute("onClick","");
	}
	
	var msg = this.request.load("POST","/action/rate.php","id_content=" + this.contentId + "&vote=" + starId );
	
	for (c = 1 ; c <= 5 ; c++)
	{
		if ( c <= starId )
			this.star[c].src = "/images/" + this.img['full'];
		else
			this.star[c].src = "/images/" + this.img['none'];
	}

	this.starLabel.innerHTML = msg;
}
