/*
 ### jQuery Star Rating Plugin v2.1 - 2008-03-12 ###
 By Diego A, http://www.fyneworks.com, diego@fyneworks.com
 - 'read only' feature by by Keith Wood, http://keith-wood.name/, kbwood@virginbroadband.com.au
 
 Project: http://plugins.jquery.com/project/MultipleFriendlyStarRating
 Website: http://www.fyneworks.com/jquery/star-rating/
	
	This is a modified version of the star rating plugin from:
 http://www.phpletter.com/Demo/Jquery-Star-Rating-Plugin/
*/
// ORIGINAL COMMENTS:
/*************************************************
 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
************************************************/


/*# AVOID COLLISIONS #*/
;if(jQuery) (function($){
/*# AVOID COLLISIONS #*/

// default settings
$.rating = {
	cancel: 'Cancel Rating', // advisory title for the 'cancel' link
	cancelValue: '',         // value to submit when user click the 'cancel' link
	half: false,             // just a shortcut to settings.split = 2
	split: 0,                // split the star into how many parts?
	required: false,         // disables the 'cancel' button so user can only select one of the specified values
	readOnly: false,         // disable rating plugin interaction/ values cannot be changed
	big: false,
	
	// required properties:
	groups: {},// allows multiple star ratings on one page
 event: {// plugin event handlers
  fill: function(n, el, state){ // fill to the current mouse position.
	  this.drain(el);
		if ($(el).attr("class").indexOf("star_big") == -1) {
			$(el).prevAll('.star').addClass('star_' + (state || 'hover'));
			$(el).addClass('star_' + (state || 'hover'));
		}else{
			$(el).prevAll('.star_big').addClass('star_big_' + (state || 'hover'));
			$(el).addClass('star_big_' + (state || 'hover'));
			
		}
  },
  drain: function(obj) { // drain all the stars.
	if ($(obj).attr("class").indexOf("star_big") == -1) {
		$(obj).removeClass('star_on').removeClass('star_hover');
		$(obj).siblings().removeClass('star_on').removeClass('star_hover');
	}else{
		$(obj).removeClass('star_big_on').removeClass('star_big_hover');
		$(obj).siblings().removeClass('star_big_on').removeClass('star_big_hover');
	}

  },
  reset: function(n, el, settings){ // Reset the stars to the default index.
		if ($(el).attr("class").indexOf("star_big") == -1) {
			$($.rating.groups[n].current).prevAll('.star').addClass('star_on');
			if ($.rating.groups[n].current) $($.rating.groups[n].current).addClass('star_on');
		}else {
			$($.rating.groups[n].current).prevAll('.star_big').addClass('star_big_on');
			if ($.rating.groups[n].current) $($.rating.groups[n].current).addClass('star_big_on');
		
		}

  }
 }// plugin events
};

$.fn.rating = function(instanceSettings){
			$.rating.big = instanceSettings.big
			jQuery.extend({
				dir: function( elem, dir ){
				var matched = [];
				var cur = elem[dir];
				while ( cur && cur != document ) {
					if ( cur.nodeType == 1 )
						matched.push( cur );
					cur = cur[dir];
				}
				return matched;
			}});
		
		jQuery.each({
			parent: "a.parentNode",
			parents: "jQuery.dir(a,'parentNode')",
			next: "jQuery.nth(a,2,'nextSibling')",
			prev: "jQuery.nth(a,2,'previousSibling')",
			nextAll: "jQuery.dir(a,'nextSibling')",
			prevAll: "jQuery.dir(a,'previousSibling')",
			siblings: "jQuery.sibling(a.parentNode.firstChild,a)",
			children: "jQuery.sibling(a.firstChild)",
			contents: "jQuery.nodeName(a,'iframe')?a.contentDocument||a.contentWindow.document:jQuery.makeArray(a.childNodes)"
		}, function(i,n){
			jQuery.fn[ i ] = function(a) {
				var ret = jQuery.map(this,n);
				if ( a && typeof a == "string" )
					ret = jQuery.multiFilter(a,ret);
				return this.pushStack( jQuery.unique(ret) );
			};
		});
	
	
	if(this.length==0) return this; // quick fail
	
	instanceSettings = $.extend(
  {}/* new object */,
		$.rating/* global settings */,
		instanceSettings || {} /* just-in-time settings */
	);
	
	// loop through each matched element
 this.each(function(i){
		
		var settings = $.extend(
			{}/* new object */,
			instanceSettings || {} /* current call settings */,
			($.metadata? $(this).metadata(): ($.meta?$(this).data():null)) || {} /* metadata settings */
		);
		
		//console.log([this.name, settings.half, settings.split], '#');
		
		// grouping:
		var formu = $(this).parents().parents().children('form');
		formu = formu.attr("id");
		var n = formu + "" + this.name;

		if(!$.rating.groups[n]) $.rating.groups[n] = {count: 0};
		i = $.rating.groups[n].count; $.rating.groups[n].count++;
	
		// Things to do with the first element...
		if(i == 0){
			$.rating.groups[n].valueElem = $('<input type="hidden" name="' + n + '" value=""' + (settings.readOnly ? ' disabled="disabled"' : '') + '>');
			$(this).before($.rating.groups[n].valueElem);
		}
		if ($.rating.big){
			eStar = $('<div class="star_big"><a title="' + (this.title || this.value) + '">' + this.value + '</a></div>');
		}else{
			eStar = $('<div class="star"><a title="' + (this.title || this.value) + '">' + this.value + '</a></div>');
		}
		
		
		$(this).after(eStar);

			// Attach mouse events
			$(eStar)
			.mouseover(function(){
				$.rating.event.drain(this);
				$.rating.event.fill(n, this, 'hover'); 
			})
			.mouseout(function(){
				$.rating.event.drain(this);
				$.rating.event.reset(n, this, settings); 
			})
			if ($.rating.big) {
				$(eStar).addClass('star_big_live');
			}else{
				$(eStar).addClass('star_live');
			}
			
			
		if(this.checked) $.rating.groups[n].current = eStar;
		$(this).remove();
		if(i + 1 == this.length) $.rating.event.reset(n, this, settings);
		
		
		if (this.checked) {
			if ($(eStar).attr("class").indexOf("star_big") == -1) {
				$(eStar).prevAll('.star').addClass('star_on');
				$(eStar).addClass('star_on');
			}else {
				$(eStar).prevAll('.star_big').addClass('star_big_on');
				$(eStar).addClass('star_big_on');
				
			}
		}
		
		
	
	}); // each element
	
	
	
	return this; // don't break the chain...
};

})(jQuery);

