//copyright 2008 Jarrett Vance
//http://jvance.com
$.fn.rater = function(options) {
    var opts = $.extend({}, $.fn.rater.defaults, options);
    return this.each(function() {
        var $this = $(this);
        var $on = $this.find('.ui-rater-starsOn');
        var $off = $this.find('.ui-rater-starsOff');
        opts.size = $on.height();
        opts.rating = $this.prev("input").val();
        if (opts.id == undefined) opts.id = $this.attr('id');

        $off.mousemove(function(e) {
			var left = e.clientX - $off.offset().left;
            var width = $off.width() - ($off.width() - left);
            width = Math.ceil(width / (opts.size / opts.step)) * opts.size / opts.step;
			width = width > 90 ? 90 : width;
            $on.width(width);
			
        }).mouseleave(function(e) {
			width = $this.prev("input").val() * 18;
			$on.width(width);
			
		}).hover(function(e) {
			$on.addClass('ui-rater-starsHover');
		}, function(e) {
            $on.removeClass('ui-rater-starsHover');
			
        }).click(function(e) {
			var r = Math.round($on.width() / $off.width() * (opts.units * opts.step)) / opts.step;
			$this.prev("input").val(r);
			
        }).css('cursor', 'pointer');
		$on.css('cursor', 'pointer');
    });
};

$.fn.rater.defaults = {
    units: 5,
    step: 1
};

$.fn.rater.rate = function($this, opts, rating) {
    var $on = $this.find('.ui-rater-starsOn');
    var $off = $this.find('.ui-rater-starsOff');
	var	$input = $this.prev("input");
	
    $off.fadeTo(600, 0.4, function() {
		$on.removeClass('ui-rater-starsHover').width(rating * opts.size);
		$input.val(rating);
		$off.fadeTo(600, 1);
    });
};

