/**
 * RadioRater
 * 
 * @module radiorater
 * @version 1.01.091119
 * @requires  LBi, jQuery
 * @author    LBi Lost Boys
 */
(function($) {

	/**
	 * The RadioRater class transforms a list of radio inputs into a clickable rating component.
	 * A jQuery function is provided to create instances automaticaly. See the jQuery functions.
	 * 
	 * @class LBi.RadioRater
	 * @constructor
	 * @param {Node} container The node to transform.
	 * @param {settings} settings Optional settings (unused in this version)
	 * @return {LBi.RadioRater} rater
	 */
	var RadioRater = function(container, settings) {
		this.container = container;
		this.settings = $.extend({}, RadioRater.Defaults, settings);

		// find radios for rating and (optionally radios for unrating/resetting)
		this.radios = $("input:radio", container);
		this.ratingRadios = this.radios.filter("[value!=]");
		this.clearRadio = this.radios.filter("[value=]");
		
		this.numberOfRatingOptions = this.ratingRadios.length;
		this.ratingOptionWidth = 100 / this.numberOfRatingOptions; // gets the width for each option as percentage %

		this.ratingAnchor = $('<a href="#">rater</a>');
		this.clearAnchor = $('<a href="#">clearer</a>');
		this.currentRating = $('<li class="current" style="width: 0%;">0 / '+this.numberOfRatingOptions+'</li>');

		// add the LI with the current rating
		$(this.container).prepend(this.currentRating);	
		
		this.ratingAnchor.bind("click", this.rateIt.bind(this));
		
		this.transformRatingRadios();
		
		if (this.clearRadio.length > 0) {
			this.clearAnchor.bind("click", this.unRateIt.bind(this));
			this.transformClearTrigger();
		}
	};

	RadioRater.prototype = {
		constructor: RadioRater,

		rateIt: function(e) {
			var clickedAnchor = $(e.target);
			var rating = clickedAnchor.attr("rel");

			// get a reference to the corresponding radio button
			var radio = this.radios.filter("[value='"+rating+"']");
			radio.click();
			this.visualizeRating(rating);
			// stop event
			e.preventDefault();
		},
		
		unRateIt: function(e) {
			this.clearRadio.click();
			this.visualizeRating(null);
			// stop event
			e.preventDefault();
		},

		visualizeRating: function(rating) {
			if (rating && rating > 0) {
				var currentRatingAnchor = $('a.rating'+rating, this.container);

				this.currentRating.siblings().removeClass("active");
				currentRatingAnchor.parent("li").addClass("active");
				this.currentRating.css("width", (this.ratingOptionWidth * rating) + "%");
				return true;
			}
			// reset the rating if no rating was given
			this.currentRating.siblings().removeClass("active");
			this.currentRating.css("width", "0");
		},	

		transformClearTrigger: function() {
			// hide radio button
			this.clearRadio.hide();
			this.clearAnchor.insertAfter(this.clearRadio);
			// add a css class to the container element so we can create space for the clearer Anchor
			$(this.container).addClass("with-clearer");
		},

		transformRatingRadios: function() {
			var i = 1;
			var anchor = this.ratingAnchor;
			var self = this;
			
			this.ratingRadios.each(function () {
				
				// clone & setup the anchor 
				var clonedAnchor = anchor.clone(true);
				clonedAnchor.attr("class", "rating"+i);
				clonedAnchor.attr("rel", i);
				clonedAnchor.text(i);
				// and insert it after the radiobutton 
				clonedAnchor.insertAfter($(this));
				
				// if a radio is checked - we'll visualize this
				if (this.checked) {
					self.visualizeRating(this.value);
				}
				
				$(this).hide();
				i++;
			});
		}
	};

	LBi.namespace('RadioRater', RadioRater);
	
	/**
	 * Settings for RadioRater instances, currently unused.
	 * 
	 * @class LBi.RadioRater.Defaults
	 * @static
	 */
	RadioRater.Defaults = {
	};
	
	/**
	 * Creates a RadioRater instance for all elements in the jQuery result. A settings object
	 * may be passed to this method. Instances are added to the LBi.RadioRater.instances array, 
	 * since the jQuery call must maintain the jQuery chain.
	 *
	 * @for jQuery.functions
	 * @method radioRater
	 * @param {Object} settings
	 * @return {jQuery} result
	 */
	RadioRater.instances = $.registerPlugin('radioRater', RadioRater);

})(jQuery);



