var Ratetastic = new Class({
    initialize: function(element,initrate){
    	this.rated = false;
        this.rater = $(element);
        if (typeof initrate == 'undefined' ){
       		this.initrate = 0;
       	} else {
       		this.initrate = initrate;
       	}
        var rater = this.rater;
        this.rater.set('send', {onComplete:function(){
    		rater.set('tween', {onComplete: function(e){
    			rater.set('html','<div class="thanks">Thanks for rating!</div>');
				rater.fade(1);
    		}});
    		rater.tween('opacity',0);
    	}, onFailure:function(){
    		rater.set('tween', {onComplete: function(e){
    			rater.set('html','<div class="problem">Rating problem.</div>');
				rater.fade(1);
    		}});
    		rater.tween('opacity',0);
    	}});
        this.rater.getElement('select').setStyle('display','none');
        this.rater.getElements('select option').each(function(el,i){
        	newel = new Element('a').set('href','#').set('text',i+1);
        	newel.setOpacity(0);
        	this.rater.adopt(newel);
        	newel.addEvent('click',function(event){
        		event.stop();
        		this.rated = true;
        		this.submitrating(i);
        	}.bind(this));
        	newel.addEvent('mouseenter',this.hoverin.bind(this,(i+1)));
        	newel.addEvent('mouseleave',this.clearstars.bind(this));
        	newel.addClass('rate' + (i+1));
        	newel.setOpacity(0);
        	newel.fade(1);
        }, this);
        if(elementextras = this.rater.getElements('input')){
        	elementextras.each(function(el,i){el.setStyle('display','none');});
        }
        this.rebound(this.initrate);
    },
    fullto: function(i){
    	if(this.rated == false){
			for(j=1;j<i;j++){
				this.rater.getElement('.rate' + j).addClass('full');
			};
    	}
    },
    clearstars: function(){
    	if(this.rated == false){
    		this.rater.getElements('a').removeClass('full');
    		this.rebound(this.initrate);	
    	}
    },
    rebound: function(i){
    	if(this.rated == false){
			for(j=1;j<i+1;j++){
				this.rater.getElement('.rate' + j).addClass('was');
			};
    	}
    },
    hoverin: function(i){
    	if(this.rated == false){
    		this.rater.getElements('a').removeClass('was');
    		this.fullto(i+1);
    	}
    },
    submitrating: function(i){
    	this.rater.getElement('select').selectedIndex = i;
    	this.rater.send();
    }
});