if(typeof Protoplasm=="undefined"){throw ("protoplasm.js not loaded, could not intitialize ratingbar")}if(window.Control==undefined){Control={}}Protoplasm.loadStylesheet("rating.css","ratingbar");Control.RatingBar=Class.create({initialize:function(b,a){this.element=$(b);this.stars=[];this.options=Object.extend({min:1,max:5,rating:0,starClass:"rating_star",onClass:"rating_on",halfClass:"rating_half",hoverClass:"rating_hover",waitForUpdate:false,onclick:Prototype.K,onhover:Prototype.K},a||{});this.rating=this.options.rating;this.loading=document.createElement("div");this.loading.className="rating_loading";this.animating=false;this.createRatingBar();this.element.onmouseout=function(){if(!this.animating){this.resetRating()}}.bind(this)},createRatingBar:function(){this.element.cleanWhitespace();this.element.style.height="20px";if(this.element.childNodes.length==1&&this.element.firstChild.nodeType==3){this.rating=this.element.firstChild.nodeValue;this.element.removeChild(this.element.firstChild)}if(!this.element.childNodes.length){for(var a=this.options.min;a<=this.options.max;++a){var b=new Element("div");b.addClassName(this.options.starClass);this.element.appendChild(b)}}else{this.rating=this.options.min;for(var a=1;a<this.element.childNodes.length;++a){var c=$(this.element.childNodes[a]);if(c.hasClassName(this.options.onClass)){this.rating++}else{if(c.hasClassName(this.options.halfClass)){this.rating+=0.5}}}}for(var a=0;a<this.element.childNodes.length;++a){var c=$(this.element.childNodes[a]);if(c.hasClassName(this.options.starClass)){this.setStarBehavior(c,a+this.options.min);this.stars.push(c)}}if(this.options.rating){this.rating=this.options.rating}if(this.rating){this.showRating(this.rating)}},setStarBehavior:function(b,a){b.onmouseover=this.hoverRating.bind(this,a);b.onclick=this.doRating.bind(this,a)},hoverRating:function(b){b=Math.ceil(b);for(var a=0;a<this.stars.length;++a){this.stars[a].className=this.options.starClass;var c=a+this.options.min;if(c<=b){this.stars[a].addClassName(this.options.hoverClass)}}if(this.options.onhover){this.options.onhover(this)}},doRating:function(a){this.rating=a;this.animateBar(a,1,150,this.resetRating.bind(this));if(this.options.onclick){this.options.onclick(this)}},animateBar:function(d,f,c,a,e,b){this.animating=true;if(!e){e=0}if(e<f){if(b){this.hoverRating(d);e++}else{this.showRating(d)}setTimeout(this.animateBar.bind(this,d,f,c,a,e,!b),c)}else{if(e>=f){setTimeout(function(){this.animating=false;a()}.bind(this),500)}}},showRating:function(b){for(var a=0;a<this.stars.length;++a){this.stars[a].className=this.options.starClass;var c=a+this.options.min;if(c<=b){this.stars[a].addClassName(this.options.onClass)}else{if(c-0.5<=b){this.stars[a].addClassName(this.options.halfClass)}}}},resetRating:function(){if(!this.animating){this.showRating(this.rating);if(this.options.input){$(this.options.input).value=this.rating}}},setLoading:function(a){if(a){if(!this.loading.parentNode){this.element.appendChild(this.loading)}}else{$(this.loading).remove()}}});Protoplasm.register("ratingbar",Control.RatingBar);