﻿// -----------------------------------------------------------------------------------
//
//	Protorater v1
//	A CoralFly Creation (www.coralfly.com)
//	Last Modified 12-04-2008
//
//	For Information, instructions, and new releases, visit:
//	http://www.coralfly.com/Creations/Protorater
//
//	Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/
//  	- Free for use in both personal and commercial projects
//		- Attribution requires leaving author name, author link, and the license info intact
//	
//  Thanks: Lokesh Dhakar (http://www.lokeshdhakar.com) for inspiration of site
//
// -----------------------------------------------------------------------------------

// Configuration //
RaterOptions = Object.extend({
    offSrc:  'images/hpa-plus-grey.gif',     
    onSrc:   'images/hpa-plus-red.gif'
}, window.RaterOptions || {});

// Rater //
var Rater = Class.create({
    initialize : function(id) {    
        this.id = id;
        this.offSrc = RaterOptions.offSrc;
        this.onSrc = RaterOptions.onSrc;
        this.onRate = null;
        this.value = 0;
        this.maxValue = 5;
    },
    
    // Draws the rating control
    draw : function(rateable) {
        var ref = this;
        var wrapper = $(this.id);
        wrapper.innerHTML = "";

        for(var i=1;i<=this.maxValue;i++)
        {
            var img = new Element('img', {
                'id':   this.id + '_' + i,
                'src':  this.offSrc,
                'hspace': 2
            });
            if(rateable)
            {
                Element.observe(img, "mouseover", function(){
                    var value = this.id.split('_')[1];
                    this.setStyle({cursor: 'pointer'});
                    ref.hover(value);
                });
                Element.observe(img, "mouseout", function(){
                    var value = this.id.split('_')[1];
                    this.setStyle({cursor: 'default'});
                    ref.leave();
                });
                Element.observe(img, "click", function(){
                    var value = this.id.split('_')[1];
                    ref.rate(value);
                });
            }
            // Append Image
            wrapper.insert(img);
        }
    },
    
    // Draws the control unrated
    drawUnrated : function() {
        this.draw(true);
    },
    // Draws the control rates
    drawRated : function(value) {
        this.draw(false);
        this.setValue(value);
    },
    // Fires on rate value mouseover
    hover : function(value){
        this.setValue(value);               
    },
    // Fires on rate value mouseout
    leave : function(){
        this.setValue(0);
    },
    // Sets the rating value
    rate : function(value) {
        this.value = value;   
        this.draw(false);
        this.setValue(value);
        
        if(this.onRate != null)
        {
            this.onRate(value); 
        }
    },
    // Sets the value in the UI 
    setValue : function(value) {
        for(var i=1;i<=this.maxValue;i++)
        {
            var img = $(this.id + "_" + i);
            if(i <= value)
            {
                img.src = this.onSrc;
            }
            else
            {
                img.src = this.offSrc;
            }
        }
    }
});