// 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
	


************************************************/
/*
*	convert a set of radio buttons to star rating type of question
*/

jQuery.fn.rating = function(settings) {
	settings = jQuery.extend({
		 cancel:'Cancel Rating',
		 currentValue:''
	}, 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()
				.lt(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).lt(settings.currentValue).addClass('star_on').end();
		}
	};
	return this.each(function (i)
					{

						if(i == 0)//prepend cancel option at the begining
						{
												
							valueElem = jQuery('<input type="hidden" name="' + this.name + '" value="" >');
							jQuery(this).before(valueElem);
								
								
									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();
													return false;
											});				
						}
						
						//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(){
									
									//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);
								
							});						
								prevElem = preElemTemp;
								preElemTemp = null;
								//remove this checkbox
								$(this).remove();
								if(i + 1 == this.length)
								{    
									event.reset();									
								}
        								
							}								
							
														 );
   
   

	



};

