/// <reference path="jquery-1.3.2-vsdoc.js" />

/*
 * jquery.rateup.js (version 1.0)
 *
 * HTML:
 * <div class="RateBox">
 * 
 *   Rate this article<br />
 *   <a href="javascript:void(0);" class="RateUp" title="Rate Up"><span>Rate Up</span></a>
 *   <a href="javascript:void(0);" class="RateDown" title="Rate Down"><span>Rate Down</span></a>
 *   <a href="javascript:void(0);" class="AddComment" title="Add Comment"><span>Add Comment</span></a>
 * 
 *   <div class="CommentBox">
 *     <span>Your comment:</span><br />
 *     <div class="Message"></div>
 *     <textarea rows="3" cols="80" style="width: 200px; height: 50px;"></textarea><br />
 *     <a href="javascript:void(0);" class="Submit">Submit</a>
 *     <a href="javascript:void(0);" class="Cancel">Cancel</a><br />
 *   </div>
 * 
 * </div>
 *
 * CSS:
 * (see sample)
 *
 * JavaScript:
 * $('.RateBox').rateup();
 *
 * I made this. (SmallSharpTools.com - 2008)
 *
 * License: Creative Commons
 *
 */
(function($) {

    $.fn.rateup = function(options) {

        var opts = $.extend({}, $.fn.rateup.defaults, options);

        return this.each(function() {

            var context = $(this);

            var rateValue = 0;
            var voted = false;
            var commented = false;
            var initialized = false;

            $('div:first').before($('.CommentBox', context).clone());
            var commentBox = $('.CommentBox:first');

            var rate = function(vote) {
                if (voted && !opts.allow_multiple_votes) {
                    return;
                }
                voted = true;
                if ($.isFunction(opts.rating_callback)) {
                    var data = {
                        url: location.href,
                        vote: vote
                    };
                    opts.rating_callback(data);
                }
                rateValue += vote;
                if (!opts.allow_multiple_votes) {
                    $('a.RateUp', context).addClass('RateUpDisabled');
                    $('a.RateDown', context).addClass('RateDownDisabled');
                }
            };

            $('a.RateUp', context).click(function(e) {
                $(this).blur();
                e.preventDefault();
                rate(1);
            });

            $('a.RateUp', context).hover(function() {
                $(this).addClass('RateUpActive');
            }, function() {
                $(this).removeClass('RateUpActive');
            });

            $('a.RateDown', context).click(function(e) {
                $(this).blur();
                e.preventDefault();
                rate(-1);
            });

            $('a.RateDown', context).hover(function() {
                $(this).addClass('RateDownActive');
            }, function() {
                $(this).removeClass('RateDownActive');
            });

            $('a.AddComment', context).click(function(e) {
                $(this).blur();
                e.preventDefault();

                if (commented) {
                    return;
                }
                var pos = $(context).position();
                var rbWidth = $(context).width();
                var rbHeight = $(context).height();
                var cbWidth = $(commentBox).width();

                var topPos = pos.top + rbHeight + opts.top_offset;
                var leftPos = pos.left - cbWidth + rbWidth;

                $('.Message', commentBox).hide();
                $(commentBox).css('top', topPos + 'px').css('left', leftPos + 'px').fadeIn(250, function() {
                    $('textarea', this).focus();
                });
            });

            $('a.AddComment', context).hover(function() {
                $(this).addClass('AddCommentActive');
            }, function() {
                $(this).removeClass('AddCommentActive');
            });

            $('a.Submit', commentBox).click(function(e) {
                $(this).blur();
                e.preventDefault();

                if (commented && !opts.allow_multiple_comments) {
                    return;
                }

                var comment = $('textarea', commentBox).val();
                if (comment) {
                    commented = true;

                    if ($.isFunction(opts.comment_callback)) {
                        var data = {
                            url: location.href,
                            comment: comment
                        };
                        opts.comment_callback(data);
                    }

                    if (!opts.allow_multiple_comments) {
                        $('a.AddComment', context).addClass('AddCommentDisabled');
                    }

                    $('.Message', commentBox).text(opts.thanks_for_comment).slideDown(250, function() {
                        setTimeout(function() {
                            $(commentBox).fadeOut();
                        }, 2000);
                    });
                }
                else {
                    $('.Message', commentBox).text(opts.please_enter_comment).slideDown(250, function() {
                        setTimeout(function() {
                            $('.Message', commentBox).slideUp();
                        }, 2000);
                    });
                }
            });

            $('a.Cancel', commentBox).click(function(e) {
                $(this).blur();
                e.preventDefault();
                $(commentBox).fadeOut();
            });

        });

    };

    // publicly accessible defaults
    $.fn.rateup.defaults = {
        thanks_for_comment: 'Thanks for your comment!',
        please_enter_comment: 'Please enter a comment!',
        rating_callback: function() { },
        comment_callback: function() { },
        allow_multiple_votes: false,
        allow_multiple_comments: false,
        top_offset: 20
    };

})(jQuery);
