﻿//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();
        //alert(opts.rating);
        if (opts.rating == undefined) opts.rating = $on.width() / opts.size;
        if (opts.count == undefined) opts.count = 0;
        if (opts.id == undefined) opts.id = $this.attr('id');

        $.fn.rater.setrating($this, opts);

        $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;
            $on.width(width);
        }).hover(function(e) { $on.addClass('ui-rater-starsHover'); }, function(e) {
            $on.removeClass('ui-rater-starsHover'); $on.width(opts.rating * opts.size);
        }).click(function(e) {
            var r = Math.round($on.width() / $off.width() * (opts.units * opts.step)) / opts.step;
            $off.unbind('click').unbind('mousemove').unbind('mouseenter').unbind('mouseleave');
            $off.css('cursor', 'default'); $on.css('cursor', 'default');
            $.fn.rater.rate($this, opts, r);
        }).css('cursor', 'pointer'); $on.css('cursor', 'pointer');
    });
};

$.fn.rater.defaults = {
    postHref: location.href,
    units: 5,
    step: 1,
    rating: 0,
    count: 0,
    contentId: ""

};

$.fn.rater.setrating = function($this, opts) {
    $this.find('.ui-rater-rating').text(opts.rating);
    $this.find('.ui-rater-rateCount').text(opts.count);
    var $on = $this.find('.ui-rater-starsOn');
    width = opts.rating * opts.size;
    $on.width(width);
};

$.fn.rater.rate = function($this, opts, rating) {
    var $on = $this.find('.ui-rater-starsOn');
    var $off = $this.find('.ui-rater-starsOff');
    var contentIdString = opts.contentId;
    $off.fadeTo(600, 0.4, function() {
        $.post(opts.postHref
            , { rating: rating, ContentIdString: contentIdString }
            , function(data) {
                if (data.error != undefined) {
                    alert(data.error);
                    return;
                }

                opts.rating = data.averageRating;
                opts.count = data.rateCount;

                $.fn.rater.setrating($this, opts);

            }
            , "json");
        //        $.ajax({
        //            url: opts.postHref,
        //            type: "POST",
        //            data: 'id=' + opts.id + '&rating=' + rating,
        //            complete: function(req) {
        //                if (req.responseText!= "") {
        //    alert("7");
        //                    opts.rating = parseFloat(req.responseText);
        //                    $off.fadeTo(600, 0.1, function() {
        //    alert("8");
        //                        $on.removeClass('ui-rater-starsHover').width(opts.rating * opts.size);
        //                        var $count = $this.find('.ui-rater-rateCount');
        //                        $count.text(parseInt($count.text()) + 1);
        //                        $this.find('.ui-rater-rating').text(opts.rating.toFixed(1));
        //                        $off.fadeTo(600, 1);
        //                        $this.attr('title', 'Your rating: ' + rating.toFixed(1));
        //    alert("9");
        //                    });
        //                }
        //            }
        ////                if (req.status == 200) { //success
        //                    opts.rating = parseFloat(req.responseText);
        //                    $off.fadeTo(600, 0.1, function() {
        //                        $on.removeClass('ui-rater-starsHover').width(opts.rating * opts.size);
        //                        var $count = $this.find('.ui-rater-rateCount');
        //                        $count.text(parseInt($count.text()) + 1);
        //                        $this.find('.ui-rater-rating').text(opts.rating.toFixed(1));
        //                        $off.fadeTo(600, 1);
        //                        $this.attr('title', 'Your rating: ' + rating.toFixed(1));
        //                    });
        //                } else { //failure
        //                    alert(req.status.toString() + " : " + req.responseText);
        //                    $on.removeClass('ui-rater-starsHover').width(opts.rating * opts.size);
        //                    $this.rater(opts);
        //                    $off.fadeTo(2200, 1);
        //                }
        //            }
        //        });
    });
};
