(function($) { function rating(container) { this.container = container; }
rating.prototype = { load : function(rate) { this.loadRate(); if ('number' == typeof rate) this.setRate(rate); }, lock : function (voteCount) { var self = this; this.setInfo(this.txt['votes count'].replace('%', voteCount)); $('.empty', this.container).hover( function(){ self.onLockedMouseOver(); }, function() { self.onLockedMouseOut(); }); }, activate : function(direct_link) { this.direct_link = direct_link; this.setInfo(this.txt['vote please']); this.loadOptions(); }, min : 1, max : 5, step : 1, txt : { 'vote please' : '&nbsp;', 'options description' : ['Špatné', 'Nic moc', 'Průměrné', 'Velmi dobré', 'Skvělé!'], 'working' : 'Zpracovává se', 'vote accepted' : 'Započteno', 'votes count' : 'Hlasováno: %x', 'multiple votes' :'Již hodnoceno', 'unexpected error' : 'Chyba' }, container : null, infoDiv : null, loadRate : function() { $(this.container).html('<div class="empty"><div class="full"></div></div>'); }, loadOptions : function() { if (this.optionsDiv) return; var self = this; var options = $('<div class="options">').bind('mouseover',function(e){self.onOptionsMouseOver(e.target.value)}).bind('mouseout', function(e){self.onOptionsMouseOut(e.target.value)}).bind('click', function(e){self.onOptionsClick(e.target.value)}); var p = 100 / (this.max-this.min+1); for (var i=this.min; i<=this.max; i++) { var o = $('<div class="stars" style="width:'+p+'%;">'); o.get(0).value = i; options.append(o); } $('.empty', this.container).append(options);}, rate : null, info : null, setRate : function(v) { this.showRate(this.rate = Math.min(Math.max(v, this.min), this.max)); }, setInfo : function(v) { this.showInfo(this.info = v); }, showRate : function(v) { $('.full', this.container).css('width', ('number' == typeof v && !isNaN(v)) ? ((v - this.min + 1) / (this.max - this.min + 1) * 100) + '%' : '0%'); }, showInfo : function(v) { this.infoEl.innerHTML = v; }, restoreRate : function() { this.showRate(this.rate); }, restoreInfo : function() { this.showInfo(this.info); }, onOptionsMouseOver : function(value) { if ('number' == typeof value) { this.showRate(value); this.showInfo(this.txt['options description'][value - this.min]); } }, onOptionsMouseOut : function(value) { this.restoreRate(); this.restoreInfo(); }, onOptionsClick : function(value) { if ('number' == typeof value) { this.vote(value); } }, onLockedMouseOver : function() { this.showInfo(this.txt['multiple votes']); }, onLockedMouseOut : function() { this.restoreInfo(); }, onSuccess : function(responce) { var self = this; var a = responce.split('|', 2); this.setInfo(this.txt['vote accepted']); this.setRate(a[0]); window.setTimeout(function(){self.lock(a[1])}, 1000); }, onFail : function() { this.setInfo(this.txt['unexpected error']); }, uiqurl : null, vote : function(v) { $('.options', this.container).remove(); var self = this; this.setInfo(this.txt['working']); $.ajax({ type: "POST", url: this.direct_link, cache : false, data : {rate : v}, success : function(t) {self.onSuccess(t)}, error: function () {self.onFail()} }); } }
$.fn.extend({ rating : function() { this.each(function() { this.rating = new rating(this); this.rating.load(parseFloat(this.innerHTML)); }); return this; }, ratingEnable : function(direct_link) { this.each(function() { this.rating.activate(direct_link); }); return this; }, ratingDisable : function(count) { this.each(function() { this.rating.lock(count); }); return this; }, ratingInfo : function(el) { this.each(function() { this.rating.infoEl = el; }); return this; } });
})(jQuery); 