/**
 * @author gwilliams
 * @classDescription Applies a star style rating to 5 radio buttons
 * @version 0.9
 * @namespace jQueryRating-0.9
 */


jQuery.fn.rating = function(settings){
	return this.each(function(){

		settings = jQuery.extend({
			radio: "rating",
			hidden: "rating_value",
			mouseover: "over",
			normal: "star"
		}, settings);
		
		var t = this;
		var checked = -1;
		var radios = jQuery(this).find(":radio");

		/**
		 * If there are no radio buttons, quit
		 */
		
		if(!jQuery(radios).length){
			if(window.console){
				window.console.warn("Did not apply rating to form, could not find radio buttons with name: " + settings.radio);
			}
			return false;
		}
		
		/**
		 * Get the index of the checked radio button
		 * @param {Object} i
		 */
		
		jQuery(radios).each(function(i){
			if(jQuery(this).attr("checked")){
				checked = i;
			}

		});
		
		
		/**
		 * Add a new hidden input to handle the rating
		 */
		jQuery(this).append("<input type='hidden' name='" + settings.hidden + "' value='0' />");
		
		var rating = jQuery(this).find(":hidden[@name=" + settings.hidden + "]:first");
		
		var fields = jQuery(this).find("input").not("[@type=radio][@type=submit]");

		/**
		 * Hide the submit button
		 */
		jQuery(this).find(":submit").hide();
		
		/**
		 * Apply actions and styles to all of the radio buttons
		 * @param {Object} i
		 */
		jQuery(radios).each(function(i){
			jQuery(this).hide();
			jQuery(this).next().addClass(settings.normal);
			
			if(i <= checked && i >= 0) jQuery(this).next().addClass(settings.mouseover);
			
			jQuery(this).next().attr("index", i);
			jQuery(this).next().hover(
				function(){
				
					label = jQuery(this);
					label.addClass(settings.mouseover);
					index = label.attr("index");
					for(i = index; i >= 0; i--){
						jQuery(t).find("label[@index=" + i +"]").addClass(settings.mouseover);
					}
			},
				function(){
					label = jQuery(this);
					label.removeClass(settings.mouseover);
					index = label.attr("index");
					for(i = index; i >= 0; i--){
						if(i > checked){
							jQuery(t).find("label[@index=" + i +"]").removeClass(settings.mouseover);
						}
					}
				}
			);
			
			/**
			 * Disable the rating form, send the data and highlight the stars based on the response
			 */
			
			jQuery(this).next().bind("click", function(i){
				var value = jQuery(this).prev().val();
				// checked = value;
				// jQuery(rating).val(value);
				
				jQuery(radios).val(value);
				
				/**
				 * Set the radio button as checked
				 * do this by looping through the buttons
				 * unchecking buttons that don't equal the
				 * value and checking the one that does
				 * @param {Object} i
				 */
				/*
				jQuery(radios).each(function(i){
				
					if(jQuery(this).val() == value){
						jQuery(this).attr("checked", "checked");
					}else{
						jQuery(this).removeAttr("checked");
					}
				});*/
				
				jQuery(radios).each(function(i){
					if(i < value){
						jQuery(this).next().addClass(settings.mouseover);
					}else{
						jQuery(this).next().removeClass(settings.mouseover);
					}
					jQuery(this).next().unbind();
				});
				
			});
					
		});

		return this;
		

	});
	
};