var doRating = function(rate, addevents, options) {
    // mootools star rating system by dimitar christoff
    // v2.1 for mootools 1.2.1
    // last modified: 09/12/2008 19:37:15
    
    // defaults
    var options = $merge({
        background: '#fff', // vote cell background
        colourBase: '#f4edaf', // start colour (left side)
        colourTarget: '#f9e526', // end colour (right side)
        maxStars: 10, // number of stars 
        starWidth: 24, // box with
        starHeight: 22, // box height
        starSpacing: 0, // space between stars
        imageURL: "/images/star.gif", // url to box background image
        tipPadding: 8, // padding around the vote text
        tipSize: "12px", // size of text in tip
        tipShown: false, // toggle tips
        border: "1px solid #ffffff", // border around the "stars"
        clickEvent: $empty() // what to pass as a function on click
    }, options);
    
    var ratinger = new Element("div", {
        styles: {
            height: options.starHeight,
            width: (options.starWidth + options.starSpacing + 2) * options.maxStars,
            "margin-top": 0,
            float: "left"
        }
    });

    for (ii = 1; ii <= options.maxStars; ++ii) {
        var bgc = options.background, percent = ((ii / options.maxStars) *100).round(), targetgbc = new Color(options.colourBase).mix(options.colourTarget, percent);
        new Element("div", {
            "class": (addevents) ? "cur rating"+options.targetObject.id : "rating"+options.targetObject.id,
            "data-index": ii,
            id: "v"+ii,
            "data-colour": targetgbc,
            styles: {
                border: options.border,
                height: options.starHeight,
                width: options.starWidth,
                "margin-right": options.starSpacing,
                float: "left",
                "background-color": bgc,
                "background-image": "url("+options.imageURL+")",
                "background-repeat": "no-repeat",
                "background-position": "left bottom"
            }
        }).inject(ratinger);
    }
    
    
    ratinger.inject(options.targetObject.empty());
    
    if (rate != 0 && options.tipShown) {
        new Element("div", {
            html: (rate + " / " + options.maxStars),
            styles: {
                float: "left",
                "font-size": options.tipSize,
                padding: options.tipPadding,
                "padding-bottom": 0
            }
        }).inject(options.targetObject);
    }

    // we support multiple rating rows on the same page.
    $$(".rating"+options.targetObject.id).each(function(el, i) {
        if (i < Math.round(rate)) {
            el.setStyle("background-color", "rgb("+el.get("data-colour") +")");
            el.setStyle("background-position", "left top");
        }
        else {
            el.addClass("bnw"); // in case we ever want to refrence them by class.
        }

        if (addevents)
            el.addEvents({
                mouseenter: function() {
                	$$(".rating"+options.targetObject.id).each(function(plains, i) {
                        plains.setStyle("background-color", (i < el.get("data-index")) ? "rgb("+plains.get("data-colour") +")" : bgc);
                        plains.setStyle("background-position", (i < el.get("data-index")) ? "left top" : "left bottom");
                    });
                },
                mouseleave: function() {
                    el.setStyles({
                        "border": options.border
                    });
                    $$(".rating"+options.targetObject.id).each(function(plains, i) {
                        plains.setStyle("background-color", (i < rate) ? "rgb("+plains.get("data-colour") +")" : bgc);
                        plains.setStyle("background-position", (i < rate) ? "left top" : "left bottom");
                    });
                },
                click: function() {
                    options.targetObject.set("data-value", el.get("data-index"));
                    // we can have a pre-defined click event that sends selected value to a function
                    // for example. an ajax call to save the rating.
                    if ($type(options.clickEvent) == "function")
                        options.clickEvent.run({
                            ratingID: options.targetObject.get("id"), 
                            newRating: options.targetObject.get("data-value")
                        });
                    
                    // re-draw with selected     
                	doRating(el.get("data-index"), false, options);
                }
            }); // end addEvents

    });
} // end doRating
   