// JavaScript Document
/*************************************************
	This is hacked version of star rating created by <a href="http://php.scripts.psu.edu/rja171/widgets/rating.php">Ritesh Agrawal</a>
	It thansform a set of radio type input elements to star rating type and remain the radio element name and value,
	so could be integrated with your form. It acts as a normal radio button.
	modified by : Logan Cai (cailongqun[at]yahoo.com.cn)
	website:www.phpletter.com


	modifications by fai:
	cleaned up source, added settings:
		showCancel	- set to true (default) to show the cancel button
		click		- function(value) to call when a rating has been clicked on

************************************************/
/*
*	convert a set of radio buttons to star rating type of question
*/

jQuery.fn.rating = function(settings) {
	settings = jQuery.extend({
		showCancel:true,
		cancel:'Cancel Rating',
		currentValue:'',
		click: null
	}, settings);
	var prevElem = null;
	var valueElem = null;
	var container = jQuery(this);
	var CancelElem = null;
	var event = {
		fill: function(el){ // fill to the current mouse position.
			var stars = jQuery(valueElem).siblings('.star');
			var index = stars.index(el) + 1;
			jQuery(stars)
				.children('a').css('width', '100%').end()
				.slice(0,index).addClass('star_on').end();
		},
		drain: function() { // drain all the stars.
			var stars = jQuery(valueElem).siblings('.star');
			jQuery(stars)
				.filter('.star_on').removeClass('star_on').end()
				.filter('.star_hover').removeClass('star_hover').end();
		},
		reset: function(){ // Reset the stars to the default index.
			var stars = jQuery(valueElem).siblings('.star');
			jQuery(stars).slice(0,settings.currentValue).addClass('star_on').end();
		}
	};
	return this.each(function (i) {

		if(i == 0) {
			valueElem = jQuery('<input type="hidden" name="' + this.name + '" value="" >');
			jQuery(this).before(valueElem);

			//prepend cancel option at the begining
			if (settings.showCancel) {

				var CancelElem = jQuery('<div class="cancel"><a href="#" title="' + settings.cancel + '">' + settings.cancel + '</a></div>');
				prevElem = CancelElem;
				jQuery(this).before(prevElem);

				jQuery(CancelElem)
					.mouseover(function(){
						event.drain();
						jQuery(this).addClass('star_on')
					})
					.mouseout(function(){
						event.reset();
						jQuery(this).removeClass('star_on')
					});

				// click events.
				jQuery(CancelElem).click(function(){
					settings.currentValue = jQuery(this).children('a').attr('title');
					$(valueElem).val(settings.currentValue);
					event.drain();

					if (settings.click != null) {
						settings.click(settings.currentValue);
					}
					return false;
				});
			} else {
				prevElem = this;
			}
		}

		//insert rating option right after preview element
		preElemTemp  = jQuery('<div class="star"><a href="#" title="' + this.value + '">' + this.value + '</a></div>');
		jQuery(prevElem).after(preElemTemp);
		jQuery(preElemTemp)
			.mouseover(function(){
				event.drain();
				event.fill(this);

			})
			.mouseout(function(){
				event.drain();
				event.reset();
			});

		jQuery(preElemTemp).click(function(e){
			//alert(jQuery(this).children('a').attr('title'));
			settings.currentValue = jQuery(this).children('a').attr('title');
			//alert(jQuery(this).children('a').attr('title'));
			jQuery(valueElem).val(settings.currentValue);
			event.drain();
			//event.reset();
			event.fill(this);

			if (settings.click != null) {
				settings.click(settings.currentValue);
			}
			e.preventDefault();

		});
		prevElem = preElemTemp;
		preElemTemp = null;
		//remove this checkbox
		$(this).remove();
		if(i + 1 == this.length) {
			event.reset();
		}
	});

};

