﻿; (function($)
{
    $.fn.starRater = function(options)
    {
        options = $.extend(
        {
            count: 10,  //max 10 ???
            url: '',
            isStatic: false
        },
        options);

        //local variables
        var list = $(this); //this = jQuery object of list element (ul or ol)
        var itemID = null;
        var rating = 0;
        var votes = 0;
        var avg = isNaN(list.attr('avg')) ? 0 : parseFloat(list.attr('avg'));

        if (!options.isStatic)
        {
            itemID = list.attr('itemid');
            rating = isNaN(list.attr('rating')) ? 0 : parseInt(list.attr('rating'));
            votes = isNaN(list.attr('votes')) ? 0 : parseInt(list.attr('votes'));
        }

        function init()
        {
            var upTo = 0;
            var css = '';
            if (rating == 0)
            {
                upTo = avg;
                css = 'star-avg'
            }
            else
            {
                upTo = rating;
                css = 'star-on';
            }
            for (var i = 1; i <= options.count; i++)
            {
                var li = $(document.createElement('li'));
                li.attr('title', i);

                if (i - .5 == upTo)
                {
                    li.addClass('star-half');
                }
                else if (i <= upTo)
                {
                    li.addClass(css);
                }

                list.append(li);
            }
        };
        init();

        //<li> items
        var items = list.children();

        //number of votes / ratings for the current item
        if (!options.isStatic)
        {
            var span = $(document.createElement('span')).text('(' + votes + ')');
            list.after(span);

            function toggleStars(e)
            {
                var upTo = 0;
                switch (e.type)
                {
                    case 'mouseenter':
                        upTo = parseInt($(this).attr('title'));
                        break;
                    default:
                        upTo = rating;
                        break;
                }
                items.removeClass('star-on');
                var starsOn = items.filter(function(e) { return parseInt($(this).attr('title')) <= upTo; });
                starsOn.addClass('star-on');
            };

            //add progressive star highlighting
            items.hover(toggleStars, toggleStars);
            items.click(function(e)
            {
                rating = parseInt($(this).attr('title'));
                saveState();
            });
        }

        function saveState()
        {
            $.get(options.url, { id: itemID, rating: rating, action: 'saveRatings' }, saveState_callback, 'json');
        };

        function saveState_callback(results)
        {
            if (results != null && !results.error)
            {
                votes = results.votes;
                rating = results.rating;
                avg = results.average;
                items.removeClass('star-on');
                items.removeClass('star-avg');
                var starsOn = items.filter(function(e) { return parseInt($(this).attr('title')) <= rating; });
                starsOn.addClass('star-on');
                span.text('(' + votes + ')');
            }
        };

    };
})(jQuery);
