
function fmStarReview(id, voteFunction, subject_id)
{

	this.build = function()
	{

		var id = this.id;

		this.e = document.getElementById(this.id);
		this.e.innerHTML = "";

		this.e.className = "fmStarReview";

		this.eDisplay = document.createElement('div');
		this.eDisplay.className = "fmDisplay";

		this.eDisplayValue = document.createElement('div');
		this.eDisplayValue.className = "fmDisplay fmValue";

		this.eDisplayVoteContainer = document.createElement('div');
		this.eDisplayVoteContainer.className = "fmDisplay fmVoteContainer";

		this.eThrobbler = document.createElement('div');
		this.eThrobbler.className = "fmThrobbler";

		this.e.appendChild(this.eDisplay);
		this.e.appendChild(this.eDisplayValue);
		this.e.appendChild(this.eDisplayVoteContainer);
		this.e.appendChild(this.eThrobbler);

		this.e.fmStarReview = this;


		if (this.eDisplayVoteContainer.currentStyle)
			var backgroundImage = this.eDisplayVoteContainer.currentStyle["backgroundImage"];
		else if (window.getComputedStyle)
			var backgroundImage = window.getComputedStyle(this.eDisplayVoteContainer,null).getPropertyValue("background-image");

		backgroundImage = backgroundImage.substring(backgroundImage.indexOf("(")+1,backgroundImage.lastIndexOf(")"));
		if(backgroundImage.charAt(0)=="\"")
			backgroundImage = backgroundImage.substring(1,backgroundImage.length-1);

		this.eDisplay.style.width = this.eDisplay.offsetWidth + "px";

		var i;
		this.eDisplayVote = new Array();
		for(i=0;i<5;i++)
		{

			this.eDisplayVote[i] = document.createElement('img');
			this.eDisplayVote[i].className = "fmDisplay fmVote";
			this.eDisplayVote[i].style.left = i * this.eDisplay.offsetWidth / 5 + "px";
			this.eDisplayVote[i].style.top = "0px";

			this.eDisplayVoteContainer.appendChild(this.eDisplayVote[i]);

			this.eDisplayVote[i].onmousemove = function(){ this.fmStarReview.setPrevote(this.i)}

			this.eDisplayVote[i].onclick = function(){ this.fmStarReview.setVote(this.i)}

			this.eDisplayVote[i].src = backgroundImage;

			this.eDisplayVote[i].i = i;
			this.eDisplayVote[i].fmStarReview = this;


		}

		this.initialized = true;

		this.setValue();

		if(this.enabled) this.enable();
		else this.disable();


	}

	this.setVote = function(value)
	{
		this.wait();
		this.disable();
		this.voteFunction(this.id, this.subject_id, value+1);
	}

	this.setPrevote = function(value)
	{

		if (this.eDisplayVote[0].currentStyle)
			var height = this.eDisplayVote[0].currentStyle["height"];
		else if (window.getComputedStyle)
			var height = document.defaultView.getComputedStyle(this.eDisplayVote[0],null).getPropertyValue("height");

		height = parseInt(height)/2;

		var currentState;
		var inactiveState = 0+"px";
		var activeState = -height+"px";
	
		for(i=0;i<5;i++)
		{
			currentState = this.eDisplayVote[i].style.top;
	
			if(value<i)
			{
				if(currentState!=inactiveState)
					this.eDisplayVote[i].style.top = inactiveState;
			}
			else
			{
				if(currentState!=activeState)
					this.eDisplayVote[i].style.top = activeState;
			}
	
		}

	}

	this.setValue = function(value)
	{

		if(value!=undefined) this.value = value;
		if(this.value<0 ||this.value>100) this.value = 0;

		if(!this.initialized || this.waiting) return false;

		if (this.eDisplay.currentStyle)
			var width = this.eDisplay.currentStyle["width"];
		else if (window.getComputedStyle)
			var width = window.getComputedStyle(this.eDisplay,null).getPropertyValue("width");

		this.eDisplayValue.style.width = parseInt(parseInt(width) * this.value / 100)+"px";

	}

	this.enable = function()
	{
		var id = this.id;
		var none = "none";
		
		this.enabled = true;
		if(!this.initialized) return false;
		
		this.e.onmousemove	= function(e)
		{
			this.fmStarReview.eDisplayVoteContainer.style.display = "block";
		}
		
		this.e.onmouseout	= function(e)
		{
			
			if (!e || e==undefined) var e = window.event;
			var tg = (window.event) ? e.srcElement : e.target;

			var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;

			if((tg.className.indexOf("fmStarReview")>-1 || tg.className.indexOf("fmDisplay")>-1)
			&& (reltg.className.indexOf("fmStarReview")>-1 || reltg.className.indexOf("fmDisplay")>-1) )
				return null;
			
			this.fmStarReview.eDisplayVoteContainer.style.display = "none";
		}

	}
	
	

	this.disable = function()
	{
		this.enabled = false;
		if(!this.initialized) return false;

		this.e.onmousemove = null;
		this.e.onmouseout = null;

		this.eDisplayVoteContainer.style.display = "none";

	}

	this.wait = function()
	{
		if(!this.initialized) return false
		this.waiting = true;

		this.eThrobbler.style.display = "block";
		this.eDisplay.style.display = "none";
		this.eDisplayValue.style.display = "none";
	}



	this.play = function()
	{
		if(!this.initialized) return false;
		this.waiting = false;

		this.eThrobbler.style.display = "none";
		this.eDisplay.style.display = "block";
		this.eDisplayValue.style.display = "block";

		this.setValue();
	}

	this.id = id;
	this.voteFunction = voteFunction;
	this.subject_id = subject_id;

	this.value = 0;
	this.enabled = true;
	this.waiting = false;

	this.initialized = false;

	if(window.addEventListener)
		window.addEventListener('load', function(){eval(id+".build()")}, false);
	else if (window.attachEvent)
		window.attachEvent('onload', function(){eval(id+".build()")});

}
