var Ratings = new Class({

    initialize: function(rating)
    {
        this.captions = ["шедевр",
        "понравился",
        "кому-то может понравиться",
        "не понравился",
        "можно не смотреть"];
        this.size=70;
        
        this.el = $('rating');
        if( this.el.className)
        {
            this.stars = this.el.className.substring(5)/2;
            this.setStars();
        }
        else
        {
            this.el.addEvent( 'mousemove', function( e) 
            {
                var coords = this.el.getCoordinates();
                var offset = e.event.clientX-coords.left;
                if( offset > this.size)
                    return 0;
                var stars = Math.ceil( offset*5.0 / this.size);
                this.stars = stars;
                this.setStars();
            }.bind( this));
            this.el.addEvent( 'mouseout', function( e) {
                this.el.setStyle( 'background-position','0px 0px');
                this.el.set( 'text',' ');
            }.bind( this));
            this.el.addEvent( 'click', function( e) {
                this.el.set( 'text','рейтинг сохранен, спасибо!');
                this.el.setStyle( 'cursor','arrow');
                this.el.removeEvents();
                var href = document.location.href;
                if( href.indexOf( '#')!=-1)
                    href = href.substring( 0, href.indexOf( '#')); 
                 document.location.href = href+'&rating='+(this.stars*2)
            }.bind( this));
        }        
    },
    setStars:function() {
        var style = (this.stars)*16;
        this.el.setStyle( 'background-position','0px -'+style+'px');
        this.el.set( 'text',this.captions[5-this.stars]);
    }
});

window.addEvent( 'domready', function() {new Ratings();});