var Rating = function(name, nStars, ratingSelected, listStarsNames, submitted, callback, starSize)
{
    this.init = function(name, nStars, ratingSelected, listStarsNames, submitted, callback, starSize)
    {
    	submitted = submitted || false;
    	callback = callback || false;

    	this.name = name;
		this.nStars = nStars * 2;
		this.hd = YAHOO.util.Dom.get(name + '_Hd');
		this.hd.value = ratingSelected;
		this.ratingdiv = YAHOO.util.Dom.get(name +'_Container');
		this.stardiv = document.createElement('div');
		this.ratingSelected = ratingSelected * 2;
		this.listStarsNames = listStarsNames;
		this.starSize = starSize;
		this.blocked = submitted;
		this.submitted = (submitted)? this.ratingSelected : false;		
		this.callback = callback;
		this.make_stardiv();
    };

    this.disabled = function()
    {
    	this.submitted = this.ratingSelected * 2;
    }
    
    this.make_stardiv = function()
    {
        YAHOO.util.Dom.addClass(this.stardiv, 'rating');
        YAHOO.util.Dom.addClass(this.stardiv, 'size'+this.starSize);
                
        // make the stars
        
        for (var i=1; i<=this.nStars; i++) {
            // first, make a div and then an a-element in it
            var star = document.createElement('div');
            star.id = this.name + '_star' + i;
            var span = document.createElement('span');
            
            var indexName = (this.blocked)? parseInt((this.ratingSelected-1)/2) : parseInt((i-1)/2);
            span.title = this.listStarsNames[indexName];
            span.alt = this.listStarsNames[indexName];
            
            span.innerHTML = i;
            YAHOO.util.Dom.addClass(star, (i%2)? 'star starLeft' : 'star starRight');
            star.appendChild(span);
            star.i = i;
            this.stardiv.appendChild(star);
            
            // add needed listeners to every star
            var thisClass = this;            
                        
            YAHOO.util.Event.addListener(star, 'mouseover', function() { thisClass.hover_star( this.i ) });
            YAHOO.util.Event.addListener(star, 'mouseout', function() { thisClass.reset_stars() });
            YAHOO.util.Event.addListener(star, 'click', function() { thisClass.submit_rating(this.i) });
        }        
        this.ratingdiv.appendChild(this.stardiv);
        
        // show the average
        this.reset_stars();
    };
    
    this.hover_star = function(which_star)
    {
    	if (this.blocked) return false;
    	    	
        /* hovers the selected star plus every star before it */    		
    	for (var i=1; i<=this.nStars; i++)
    	{
    		var star = YAHOO.util.Dom.get(this.name + '_star' + i);
    		YAHOO.util.Dom.removeClass(star, (i%2)? 'onLeft' : 'onRight');
    	}
    	
        for (var i=1; i<=which_star; i++)
        {
            var star = YAHOO.util.Dom.get(this.name + '_star' + i);
            var a = star.firstChild;
            YAHOO.util.Dom.addClass(star, (i%2)? 'hoverLeft' : 'hoverRight');
            YAHOO.util.Dom.setStyle(a, 'width', '100%');
        }
    };

    /* Resets the status of each star */
    this.reset_stars = function()
    {
        // if form is not submitted, the number of stars on depends on the given average value
        if (!this.blocked)
        {
        	var stars_on = parseInt(this.ratingSelected);
        }
        else
        {
        	var stars_on = this.submitted;
        }
        
        for (var i=1; i<=this.nStars; i++)
        {
            var star = YAHOO.util.Dom.get(this.name + '_star' + i);
            var a = star.firstChild;
            
            // first, reset all stars
            YAHOO.util.Dom.removeClass(star, (i%2)? 'hoverLeft' : 'hoverRight');
            YAHOO.util.Dom.removeClass(star, (i%2)? 'onLeft' : 'onRight');

            // for every star that should be on, turn them on
            if (i<=stars_on && !YAHOO.util.Dom.hasClass(star, (i%2)? 'onLeft' : 'onRight'))
            {
            	YAHOO.util.Dom.addClass(star, (i%2)? 'onLeft' : 'onRight');
            }                
            
            // and for the last one, set width if needed
            if (i == stars_on)
            {
            	YAHOO.util.Dom.setStyle(a, 'width', '100%');	
            }
        }
    };
    
    this.submit_rating = function(num)
    {    	
        // If the form has not been submitted yet and submission is not in progress
        if (!this.blocked)
        {
        	this.submitted = num;
        	this.blocked = true;
        	
        	this.ratingSelected = num;
            
            // change the rating-value for the form and submit the form
        	this.hd.value = num/2;
            
            // Exec Callback
        	if (this.callback !==false) this.callback(num/2);
        }
    };
    
    this.init(name, nStars, ratingSelected, listStarsNames, submitted, callback, starSize);
    
}
