var Stars.prototype = {
	_x: 0,
	_y: 0,
	initialize: function(options)
	{
		this._initialized = false;
		this.options = {
			bindField: null,
			maxRating: 6,
			container: null,
			imagePath: 'images/star',
			callback: null,
			actionURL: null,
			value: 0,
			locked: false,
			voted: false,
			redirectURL: null,
			post_id: 0,
			poster_id: 0
		};
		Object.extend(this.options, options);
		this.locked = this.options.locked ? true : false;
		this._starSrc = {
			empty: this.options.imagePath + "_empty.png",
			full: this.options.imagePath + "-p.png",
			half: this.options.imagePath + "-p_half.png"
		};			
		for(var x in this._starSrc)
		{
			var y = new Image();
			y.src = this._starSrc[x];
		}
		document.getElem
		this._setStarSrc = {			
			empty: this.options.imagePath + "_empty.png",
			full: this.options.imagePath + ".png",
			half: this.options.imagePath + "_half.png"
		};
		this._star_v_Src = {
			empty: this.options.imagePath + "_empty.png",
			full: this.options.imagePath + "-v.png",
			half: this.options.imagePath + "-v_half.png"
		};
		if ( this.options.voted )
		{
			this._setStarSrc = this._star_v_Src;
		}
		
		for(var x in this._setStarSrc)
		{
			var y = new Image();
			y.src = this._setStarSrc[x];
		}

		this.value = -1;
		this.stars = [];
		this._clicked = this.options.voted;


		if(this.options.container)
		{
			this._container = $(this.options.container);
			this.id = this._container.id;
		}
		else
		{
			this.id = 'starsContainer.' + Math.random(0, 100000);
			document.write('<span id="' + this.id + '"></span>');
			this._container = $(this.id);
		}
		this._display();
		this.setValue(this.options.value);
		this._initialized = true;
		
		this.message = false;
	},
	_display: function()
	{
		for(var i = 0; i < this.options.maxRating; i++)
		{
			var star = new Image();
			star.src = this.locked ? this._starSrc.empty : this._setStarSrc.empty;
			if ( this.locked ) 
			{
				star.title = s_lockedText;
				if ( this.options.redirectURL ) star.style.cursor = 'pointer';
			}
			else
			{
				star.style.cursor = 'pointer';
				star.title = s_text[i];
			}
			Event.observe(star, 'mouseover', this._starHover.bind(this));
			Event.observe(star, 'click', this._starClick.bind(this));
			Event.observe(star, 'mouseout', this._starClear_mouseout.bind(this));
			this.stars.push(star);
			this._container.appendChild(star);
		}
	},
	_starHover: function(e)
	{
		if(this.locked) 
		{
			_alert('s_r_' + this.options.post_id, s_lockedText);
			return;
		}
		if(!e) e = window.event;
		var star = Event.element(e);
		var greater = false;
		for(var i = 0; i < this.stars.length; i++)
		{
			this.stars[i].src = greater ? this._starSrc.empty : this._starSrc.full;
			if(this.stars[i] == star) 
			{
				greater = true;
				_alert('s_r_' + this.options.post_id, s_text[i]);
			}
		}
	},
	_starClick: function(e)
	{
		if( this.locked )
		{
			if ( this.options.redirectURL == null )
			{
				return;
			}
			else
			{
				document.location.href = this.options.redirectURL;
				return;
			}
		}
		if(!e) e = window.event;
		var star = Event.element(e);
		this._clicked = true;
		for(var i = 0; i < this.stars.length; i++)
		{
			if(this.stars[i] == star)
			{
				this.setValue(i+1);
				break;
			}
		}
	},
	_starClear: function(e)
	{
		if(this.locked && this._initialized) 
		{
			return;
		}
		var greater = false;
		for(var i = 0; i < this.stars.length; i++)
		{
			if(i > this.value) 
			{
				greater = true;
			}
			if((this._initialized && this._clicked) || this.value == -1)
			{
				if ( this._clicked ) 
				{
					this.stars[i].src = greater ? (this.value + .5 == i) ? this._star_v_Src.half : this._star_v_Src.empty : this._star_v_Src.full;
				}
				else
				{
					this.stars[i].src = greater ? (this.value + .5 == i) ? this._starSrc.half : this._starSrc.empty : this._starSrc.full;
				}
			}
			else
			{
				this.stars[i].src = greater ? (this.value + .5 == i) ? this._setStarSrc.half : this._setStarSrc.empty : this._setStarSrc.full;
			}
		}
	},
	_starClear_mouseout: function(e)
	{
		if(!this.message) _alert('s_r_' + this.options.post_id, '')
		else _alert('s_r_' + this.options.post_id, this.message);
		
		if(this.locked && this._initialized) return;
		var greater = false;
		for(var i = 0; i < this.stars.length; i++)
		{
			if(i > this.value) 
			{
				greater = true;
			}
			if((this._initialized && this._clicked) || this.value == -1)
			{
				if ( this._clicked ) 
				{
					this.stars[i].src = greater ? (this.value + .5 == i) ? this._star_v_Src.half : this._star_v_Src.empty : this._star_v_Src.full;
				}
				else
				{
					this.stars[i].src = greater ? (this.value + .5 == i) ? this._starSrc.half : this._starSrc.empty : this._starSrc.full;
				}
			}
			else
			{
				this.stars[i].src = greater ? (this.value + .5 == i) ? this._setStarSrc.half : this._setStarSrc.empty : this._setStarSrc.full;
			}
		}
	},
	setValue: function(val)
	{
		if(this.locked && this._initialized) return;
		this.value = val-1;
		if(this.options.bindField)
			$(this.options.bindField).value = val;
		if(this._initialized)

		$jq.post(this.options.actionURL + val, {}, this.options['callback'],"json");

		this._starClear();
	},
	
	_display_vote: function(r)
	{
		this.value = r-1;
		this._starClear();
	},
	
	_reset: function()
	{
		this.value = this.options.value - 1;
		this._clicked = this.options.voted;
		this._starClear();
	}
};

function ajaxRating(data)
{
	a = data.alert;
	post_id = data.p;
	poster_id = data.u;
	if ( a )
	{
		s[post_id].message = a;
		_alert('s_r_' + post_id, a);
		s[post_id]._reset();
	}
	else
	{
		var m = data.message;
		if ( m )
		{
			s[post_id].message = m;
			_alert('s_r_' + post_id, m);
		}
		var r = data['r_' + post_id];
		s[post_id]._display_vote(r);
		$jq('span[name="ur_' + poster_id + '"]').html(data['ur_' + poster_id]);
	}
}