﻿(function($) {
	$.fn.binary = function(callback, options) {
		var settings = jQuery.extend({
			defaultClass: "lt_gradable_default",
			showCheckbox: false
		}, options);

		return this.each(function() {
			var ths = $(this);
			ths.find("input:radio").hide();
			$(this).find("li").each(function(i) {
				var radio = $(this).find("input:radio");
				var select = $("<div></div>").addClass("binaryRating").click(function() {
					radio.attr("checked", "true");
					callback(radio.val());
					setBackgrounds();
				});

				select.appendTo($(this));
			});
			setBackgrounds();

			function setBackgrounds() {
				ths.find("li").each(function(i) {
					var xpos = 25 * i;
					var select = $(this).find(".binaryRating");
					var radio = $(this).find("input:radio");
					if (radio.attr("checked")) {
						select.css({ backgroundPosition: "-" + xpos + "px -50px" });
					} else {
						select.css({ backgroundPosition: "-" + xpos + "px 0px" });
					}
				});
			}
		});
	};
})(jQuery);