; (function ($) {
    $.fn.rating = function (settings) {
        options = $.extend({}, $.fn.rating.defaults, settings);

        // Return if id is not specified
        if (this.attr('id') === undefined) {
            return;
        }
        $global = $(this);

        var $this = $global,
			id = $this.attr('id'),
			start = 0,
			starFile = options.starOn,
			hint = '';

        // Get the start value
        if (!isNaN(options.start) && options.start > 0) {
            start = (options.start > options.number) ? options.number : options.start;
        }

        // Remove all child
        $this.children().remove();

        if (!options.readOnly) {
            for (var i = 1; i <= options.number; i++) {
                starFile = (start >= i) ? options.starOn : options.starOff;
                hint = (options.number <= options.hintList.length && options.hintList[i - 1] !== null) ? options.hintList[i - 1] : i;
                $this.append('<img id="' + id + '-' + i + '" src="' + options.path + starFile + '" alt="' + i + '" title="' + hint + '" class="' + id + '" style=";margin-top: 3px;"/>');
            }
        } else {
            $this.append('<ul class="rating square"></ul>').attr('title', Math.round(options.start * 100)/100).css('cursor', 'default');
            var ulRating = $this.children('ul');
            for (i = 0; i < parseInt(options.start); i++) {
                ulRating.append('<li><span style="width: 100%;"></span></li>');
            }

            var remainder = parseInt((options.start - parseInt(options.start)) * 100);
            var remaining = '<li><span style="width: ' + remainder + '%"></span></li>';
            ulRating.append(remaining);
        }

        // Store the value
        $('<input id="' + id + '-score" type="hidden" name="' + options.scoreName + '"/>').appendTo($this).val(start);

        if (!options.readOnly) {
            $this.css('cursor', 'pointer');
            bindAll($this, options);
        }
        return $this;
    };

    // Default options
    $.fn.rating.defaults = {
        hintList: ['1', '2', '3', '4', '5'],
        number: 5,
        path: '/Templates/ClickTalk/Images/',
        readOnly: false,
        scoreName: 'score',
        start: 0,
        starOff: 'star_off.png',
        starOn: 'star_on.png'
    };

    $.fn.rating.start = function (score) {
        initialize($global, score, options);
        return $.fn.rating;
    };

    $.fn.rating.click = function (score) {
        initialize($global, score, options);

        if (options.onClick) {
            options.onClick.apply($global, [score]);
        } else {
            debug('You should add the "onClick: function(score) { }" option.');
        }
        return $.fn.rating;
    };

    // bindAll function
    function bindAll(context, options) {
        var id = context.attr('id'),
			score = $('input#' + id + '-score'),
			qtyStar = $('img.' + id).length;

        context.live('mouseleave', function () {
            initialize(context, score.val(), options);
        });

        $('img.' + id)
		.live('mouseenter', function () {
		    for (var i = 1; i <= qtyStar; i++) {
		        if (i <= this.alt) {
		            $('img#' + id + '-' + i).attr('src', options.path + options.starOn);
		        } else {
		            $('img#' + id + '-' + i).attr('src', options.path + options.starOff);
		        }
		    }
		}).live('click', function () {
		    score.val(this.alt);
		    if (options.onClick) {
		        options.onClick.apply(context, [this.alt]);
		    }
		});
    };

    // Initialize the images
    function initialize(context, score, options) {
        var id = context.attr('id'),
			qtyStar = $('img.' + id).length;

        if (score < 0 || isNaN(score)) {
            score = 0;
        } else if (score > options.number) {
            score = options.number;
        }

        $('input#' + id + '-score').val(score);

        for (var i = 1; i <= qtyStar; i++) {
            if (i <= score) {
                $('img#' + id + '-' + i).attr('src', options.path + options.starOn);
            } else {
                $('img#' + id + '-' + i).attr('src', options.path + options.starOff);
            }
        }

        if (options.readOnly || context.css('cursor') == 'default') {
            setHint(context, score, options);
        }
    };

    // setHint
    function setHint(context, score, options) {
        if (score != 0) {
            score = parseInt(score);
            hint = (score > 0 && options.number <= options.hintList.length && options.hintList[score - 1] !== null) ? options.hintList[score - 1] : score;
        }
        context.attr('title', hint).children('img').attr('title', hint);
    };
})(jQuery);
