(function($) {

$.fn.voting = function (settings) {
    
    settings = $.extend({
        'update_target': null,
        'score_url': '',
        'post_msg': '',
        'effect': 'expand',
        'only_one_vote': true
    }, settings);
    
    return this.each(function () {
        var container = this;
        var form = $("form", container);
        
        var extra_args = {};
        $("input[type=hidden]", form).each(function (i, input) {
            var name = $(input).attr("name");
            var value = $(input).val();
            extra_args[name] = value;
        });
        var instance_settings = $.extend({}, settings, extra_args);

        $("input[type=submit]", container).click(function () {
            var button = $(this);
            var vote = $(this).val();
            var url = form.attr("action");
            var csrf = $("input[name=csrfmiddlewaretoken]", form).val();
            $.post(url, {vote: vote, is_ajax: true, csrfmiddlewaretoken: csrf},
                function () {
                    if (instance_settings.only_one_vote) {
                        $("input", form).attr("disabled", "disabled");
                    }
                    $("span.vote-message", container).html(instance_settings.post_msg);
                    update_target();
            });
            return false;
        });

        function save_classes() {
            $("input", form).each(function (i, input) {
                $.data(input, "original_class", $(input).attr("class"));
            });
        }

        function restore_classes() {
            $("input", form).each(function (i, input) {
                $(input).attr("class", $.data(input, "original_class"));
            });        
        }
        
        save_classes();

        $("input", form).mouseover(function () {
            $(this).prevAll().attr("class", "star");
            $(this).attr("class", "star");
            $(this).nextAll().attr("class", "blank");
        }).mouseout(function () {
            restore_classes();
        });
        
        var eye_candy_effects = {
            'expand': function (target) {
                var spacing = instance_settings.letter_spacing_effect;
                target.animate({letterSpacing: "20px"}, "slow")
                      .animate({letterSpacing: "0"}, "slow");
            },
            'hide-and-show': function (target) {
                function show_previous() {
                    $(this, target).prev("img").fadeIn("fast", show_previous);
                }
    
                function hide_next() {
                    var next = $(this, target).next("img")
                    if (next.length > 0) {
                        next.fadeOut("fast", hide_next);
                    } else { // no more stars
                        $("img:last", target).fadeIn("fast", show_previous);
                    }
                }
                $("img:first", target).fadeOut("fast", hide_next);
            }
        };

        function update_target() {
            if (instance_settings.update_target != null) {
                $(instance_settings.update_target).load(instance_settings.score_url, function () {
                    eye_candy_effects[instance_settings.effect]($(this));
                });
            }
        };

    }); // end of each

}; // end of voting

})(jQuery);
