
var ns_topshare;

if (!ns_topshare)
	ns_topshare = {};

ns_topshare.ns_rating;

if (!ns_topshare.ns_rating)
{
	ns_topshare.ns_rating = {};

	ns_topshare.ns_rating.Widget = function()
	{
		this.initialPosition = new Array();
		this.canVote = false;
		this.average = 0;
		this.votes = 0;
	};

	ns_topshare.ns_rating.Widget.prototype.SubmitRating = function()
	{
		var form = document.getElementById('frm');
		var url = '/node/asp/modSaveFloatingForm.aspx'
		var str = this.makePostString(form);
		parent.postForm(str, url);
	};

	ns_topshare.ns_rating.Widget.prototype.ResetValues = function()
	{
		if(this.canVote)
		{
			for (var i = 1; i < this.initialPosition.length; i++)
				document.getElementById('ph' + i).style.backgroundPosition = 
					'0pt ' + this.initialPosition[i];
		}
	};

	ns_topshare.ns_rating.Widget.prototype.Opt = function(p_vote)
	{
		if(this.canVote)
		{
			// We need to change This image + all the images before this one;
			for (var i = 1; i <= p_vote; i++)
				document.getElementById('ph' + i).style.backgroundPosition = '0pt -28pt';
		}
	};

	ns_topshare.ns_rating.Widget.prototype.Vote = function(p_vote)
	{
		if(this.canVote)
		{
			document.getElementById('nbrRating').value = p_vote;
			this.SubmitRating();
			this.VoteAnimate(p_vote);

			this.average = ((this.average * this.votes) + p_vote) / (this.votes + 1);
			this.SetVotes(this.votes + 1);
			this.RedrawRating();
		}
	};

	ns_topshare.ns_rating.Widget.prototype.VoteAnimate = function(p_vote)
	{
		document.getElementById('ph' + p_vote).style.backgroundPosition = '0pt -25pt';
	};
	
	ns_topshare.ns_rating.Widget.prototype.AddInitialPosition = function(p_placeholder, p_value)
	{
		this.initialPosition[p_placeholder] = p_value;
	};

	ns_topshare.ns_rating.Widget.prototype.EnableVoting = function()
	{
		this.canVote = true;
	};

	ns_topshare.ns_rating.Widget.prototype.DisableVoting = function()
	{
		this.canVote = false;
	};

	ns_topshare.ns_rating.Widget.prototype.SetAverage = function(p_average)
	{
		this.average = p_average;
	};

	ns_topshare.ns_rating.Widget.prototype.GetAverage = function()
	{
		return this.average;
	};

	ns_topshare.ns_rating.Widget.prototype.SetVotes = function(p_votes)
	{
		this.votes = p_votes;
	};

	ns_topshare.ns_rating.Widget.prototype.GetVotes = function()
	{
		return this.votes;
	};

	ns_topshare.ns_rating.Widget.prototype.ResetImages = function()
	{
		for (var i = 1; i < 6; i++)
		{
			document.getElementById('ph' + i).style.backgroundPosition = '0 -7';
		}
	};

	ns_topshare.ns_rating.Widget.prototype.RedrawRating = function()
	{
		this.ResetImages();

		for (var i = 0; i < 5; i++)
		{
			var shift = -7;
			var difference = this.average - i;
			
			if (difference > 0.25 && difference < 0.75)
				shift = -97; 

			if (difference > 0.75)
				shift = -67;

			var elt = document.getElementById('ph' + (i + 1));
			elt.onmouseover = '';
			elt.onmouseout = '';
			elt.onclick = '';

			elt.style.backgroundPosition = '0 ' + shift;
		}
		var votelabel = this.GetVotes() + ' vote';
		
		if(this.GetVotes() != 1)
			votelabel = votelabel + 's';

		document.getElementById('rateVotes').innerHTML = votelabel;

	};

	ns_topshare.ns_rating.Widget.prototype.makePostString = function(form)
	{
		var str = '';
		var length = form.length;
		for (var i = 0; i < length; i++)
		{
			if(form.elements[i].name != '')
			{	
				
				var elem = form.elements[i];
				if((elem.type != 'radio' && elem.type != 'checkbox') || elem.checked == true)
				{
					if(str.length > 0) str = str + '&'; 
					str = str + elem.name + '=' + escape(elem.value);
				}
			}
		}
		return str;
	}
		
	
};


