var moorating = new Class({
	 
	initialize: function(options) {
		
		this._initialized = false;
		
		this.options = Object.extend({
			bindField:			null,		// Form Field to bind the value to
			maxRating:			5,			// Maximum rating, determines number of stars
			feedbackcontainer:	null,		// feedback display
			ratingfeedback:		null,		// rating feedback (one for each star)
			container:			null,		// Container of stars
			imagePath:			'images/',	// Path to star images
			callback:			null,		// Callback function, fires when the stars are clicked
			actionURL:			null,		// URL to call when clicked. The rating will be appended to the end of the URL (eg: /rate.php?id=5&rating=)
			value:				0,			// Initial Value
			locked:				false
		}, options || {});
		
		this.locked = this.options.locked ? true : false;

		if($defined(this.options.feedbackcontainer))
		{
			this.feedbackcontainer = $(this.options.feedbackcontainer);
			this.ratingfeedback = this.options.ratingfeedback;
			this.feedbackcontainer.setHTML(this.ratingfeedback[0]);
			this.clickedratingfeedback = this.ratingfeedback[0];
		}
		
		/**
		 * Image sources for hover and user-set state ratings
		 */
		this._starSrc = {
			empty: this.options.imagePath + "star1-empty.gif",
			full: this.options.imagePath + "star1.gif",
			half: this.options.imagePath + "star1-half.gif"
		};
		/**
		 * Preload images
		 */
		for(var x in this._starSrc)
		{
			var y = new Image();
			y.src = this._starSrc[x];
		}

		//document.getElem
		
		/**
		 * Images to show for pre-set values, changes when hovered, if not locked.
		 */
		this._setStarSrc = {
			empty: this.options.imagePath + "star1-ps-empty.gif",
			full: this.options.imagePath + "star1-ps.gif",
			half: this.options.imagePath + "star1-ps-half.gif"
		};

		/**
		 * Preload images
		 */
		for(var x in this._setStarSrc)
		{
			var y = new Image();
			y.src = this._setStarSrc[x];
		}

		this.value = -1;
		this.stars = [];
		this._clicked = false;


		if(this.options.container)
		{
			this._container = $(this.options.container);
			this.id = this._container.id;
		}
		else
		{
			this.id = 'starsContainer.' + Math.random(0, 100000);
			this._container = new Element('div').setProperty('id', this.id).injectInside(document.body);
			
		}
		
		this._display();
		this.setValue(this.options.value);
		this._initialized = true;
	},
	_display: function()
	{
	
		for(var i = 0; i < this.options.maxRating; i++)
		{
			star = new Element('img').injectInside(this._container);
			star.src = this.locked ? this._starSrc.empty : this._setStarSrc.empty;
			star.style.cursor = 'pointer';
			//star.title = 'Rate as ' + (i + 1);
			star.title = this.ratingfeedback[i + 1];
			star.id = 'starRate' + (i + 1);

			star.addEvent('click', function(e) {
				if (!this.locked)
				{
					this._starClick(e);
				}
			}.bind(this));

			star.addEvent('mouseover', function(e) {
				if (!this.locked)
				{
					this._starHover(e);
				}
			}.bind(this));
			
			star.addEvent('mouseout', function(e) {
				if(!this.locked)
				{
					this._starClear(e);
				}
			}.bind(this));
			
			this.stars.push(star);
		}
	},
	_starHover: function(evt)
	{
		if(this.locked) return;
		if(!evt) return;
		var star = new Event(evt).target;
		
		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;
		}
		if($defined(this.options.feedbackcontainer))
			this.feedbackcontainer.setHTML(star.title);
	},
	_starClick: function(evt)
	{
		if(this.locked) return;
		if(!evt) return;
		var star = new Event(evt).target;

		this._clicked = true;
		for(var i = 0; i < this.stars.length; i++)
		{
			if(this.stars[i] == star)
			{
				this.setValue(i+1);
				break;
			}
		}
		if($defined(this.options.feedbackcontainer))
		{
			this.feedbackcontainer.setHTML(star.title);
			this.clickedratingfeedback = star.title;
		}
	},
	_starClear: function()
	{
		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)
				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;
		}
		
		if($defined(this.options.feedbackcontainer))
		{
			if((this._initialized && this._clicked) || this.value == -1)
				this.feedbackcontainer.setHTML(this.clickedratingfeedback);
			else
				this.feedbackcontainer.setHTML(this.ratingfeedback[0]);
		}
	},
	/**
	 * Sets the value of the star object, redraws the UI
	 * @param {Number} value to set
	 * @param {Boolean} optional, do the callback function, default true
	 */
	setValue: function(val)
	{
		var doCallBack = arguments.length > 1 ? !!arguments[1] : true;
		if(this.locked && this._initialized) return;
		this.value = val-1; //0-based
		if(this.options.bindField)
			
			var radios = $$(this.options.bindField);

			for(var i = 0; i < 5; i++) {
			
				radios[i].checked = false;
				if(radios[i].value == val) {
					radios[i].checked = true;
				}
			}

			
		if(this._initialized && doCallBack)
		{
			if(this.options.actionURL)
				new Ajax.Request(this.options.actionURL + val, {onComplete: this.options['callback'], method: 'get'});
			else
				if(this.options.callback)
					this.options['callback'](val);
		}
		this._starClear();
	}


});