
(function($) {

    $.rating = {
        id          : 0,
        uid         : 0,
        sid         : 0,
        size        : 5,
        value       : 0,
        step        : 1,
        method      : 'ajax',
        title       : 'Average rating: %s',
        readonly    : false,
        roClick     : function() {
            alert('This rating is readonly!'); return false;
        },
        notify  : function(msg) {
            alert(msg);
        },
        test        : {
            action  : function(val, opt, r) {
                // switch to readonly to avoid double voting
                r.find('div').unbind().click(function() {
                    opt.roClick(); return false;
                }).removeClass('hover').removeClass('gray');
                $.rating.notify('method: \''+opt.method+'\' clicked: '+val);
            }
        },
        ajax        : {
            url     : '/catalog/rating/ajax/vodrating.php',
            type    : 'POST',
            action  : function(val, opt, r) {
                // switch to readonly to avoid double voting
                r.find('div').unbind().click(function() {
                    opt.roClick(); return false;
                }).removeClass('hover').removeClass('gray');
                $.ajax({
                    url     : opt.ajax.url,
                    type    : opt.ajax.type,
                    global  : false,
                    dataType: 'json',
                    target  : r,
                    data    : {
                        id      : opt.id,
                        sid     : opt.sid,
                        uid     : opt.uid,
                        value   : val
                    },
                    success: function(res) {
                        if (res.error > 0) {
                            alert('Error: '+res.msg);
                        } else {
                            // re-render with new value
                            this.target.rating({
                                value: res.value,
                                readonly: true
                            });
                            if (res.msg && res.msg.length > 0) {
                                $.rating.notify(res.msg);
                            }
                        }
                    },
                    error: function(xhr, errmsg, e) {
                        var emsg = 'Error '+xhr.status+' '+xhr.statusText;
                        emsg += '\n('+errmsg+')';
                        emsg += (e ? '\nException: '+e.toString() : '');
                        alert(emsg);
                    }
                });
            }
        }
    };

    $.fn.rating = function(options) {
        if(0 == this.length) return this;
        var opts = $.extend({}, $.rating, options);
        return this.each(function() {
            var $this = $(this);
            var o = $.metadata ? $.extend({}, opts, $this.metadata()) : opts;
            var r = $($.fn.rating.buildAll(o));
            if (o.readonly) {
                r.click(function() { 
                    o.roClick(); return false;
                });
            } else {
                r.find('div').hover(
                    function() {
                        $(this).add($(this).prevAll()).addClass('hover');
                        $(this).nextAll().addClass('gray');
                    },
                    function() {
                        $(this).add($(this).prevAll()).removeClass('hover');
                        $(this).nextAll().removeClass('gray');
                    }
                    ).click(
                    function() {
                        var n = $(this).prevAll().length*o.step+o.step
                        o[o.method].action(n, o, r); return false;
                    }
                    );
            }
            $this.replaceWith(r);
        });
    };

    $.fn.rating.buildOne = function(i, o) {
        var showPart = false; var w;
        if (o.value > i && o.value < i + o.step) {
            showPart = true; w = Math.round((o.value - i)*100*o.step);
        }
        return '<div class="star'+(o.value > i ? ' on' : '')+'" title="'
        +''+'"><a href="#"'+(showPart ? ' style="width:'+w+'%;"' : '')
        +'></a></div>\n';
    }

    $.fn.rating.buildAll = function(o) {
        var out = '<div id="rating-'+o.id+'" class="rating"'
        +(o.title ? ' title="'+o.title.replace(/%s/, o.value)+'"' : '')+'>';
        for (var i = 0; i < o.size; i ++) {
            out += $.fn.rating.buildOne(i, o);
        }
        return out + '</div>';
    }

    if ($.browser.msie) {
        try {
            document.execCommand("BackgroundImageCache", false, true)
        } catch(e) {}
    }
})(jQuery);

