var dlRating = Class.create ();

dlRating.prototype.clearRating = function (e) {

    if (this.registered == true) {
        return false;
    }

    if (!$("dlRating")) {return false;}

    var element = $("dlRating");

    var imgs = Element.childElements (element).each(function (img) {
        img.setAttribute ("src", "/comments/widgets/commentsAndRatings/images/star_open.gif");
    });

    Event.stop (e);

    $("ratingText").update ('');
}

dlRating.prototype.displayRater = function () {

    var that = this;
    this.registered = false;

    if (typeof (arguments [0]) == "undefined") {
        var div = new Element ('div', {
            'id' : 'dlRating',
            'class' : 'dlRater'
        });

        Event.observe (div, "mouseout", function (e) { that.clearRating.call (that, e);});

    } else {
        var div = new Element ('div', {
            'class' : 'dlRater'
        });
    }
    for (var i = 1; i <= 5; i ++) {

        if (typeof (arguments [0]) == "undefined") {

            var starImg = new Element ('img', {
                'id'          : 'dlRating_' + i,
                'style'       : 'border: none',
                'class'       : 'ratingStar',
                'src'         : '/comments/widgets/commentsAndRatings/images/star_open.gif'
            });

        } else {

            var starImg = new Element ('img', {
                'style'       : 'border: none',
                'class'       : 'ratingStar',
                'src'         : '/comments/widgets/commentsAndRatings/images/star_open.gif'
            });
        }

        var filledImg = new Element ('img', {
            'style' : 'border: none;',
            'class' : 'ratingStar',
            'src'   : '/comments/widgets/commentsAndRatings/images/star_filled.gif'
        });

        if (typeof (arguments [0]) == "undefined") {

            Event.observe (starImg, "mouseover", function (e) {
                that.hoverRating (e);
            });

            Event.observe (starImg, "click", function (e) {
                that.registerRating (e);
            });

            div.insert (starImg);
        } else {
            if (arguments [0] >= i) {
                div.insert (filledImg);
            } else {
                div.insert (starImg);
            }
        }
    }
    return div;
}

dlRating.prototype.hoverRating = function (e) {

    var node = e.target;

    var starNo = node.id.split ('_') [1];

    for (var i = 1; i <= starNo; i ++) {
        $(node.parentNode.id + "_" + i).setAttribute ("src", "/comments/widgets/commentsAndRatings/images/star_filled.gif");
    }

    var starText = "";

    if (starNo < 5) {

        var x = parseInt (starNo) + 1;

        for (var i = parseInt (starNo) + 1; i <= 5; i ++) {
            $(node.parentNode.id + "_" + i).setAttribute ("src", "/comments/widgets/commentsAndRatings/images/star_open.gif");
        }
    }

    if (starNo == 1) {starText = "Yuck!";}
    if (starNo == 2) {starText = "Not so good";}
    if (starNo == 3) {starText = "OK";}
    if (starNo == 4) {starText = "Good";}
    if (starNo == 5) {starText = "Delicious!";}

    $("ratingText").update (starText);
}

dlRating.prototype.registerRating = function (e) {

    this.registered = true;

    $("ratingText").update ('Thank you. To post your rating/review click "Submit."');

    var node = e.target;
    var that = Object.clone (this);
    var rating = node.id.substr (9, 1);

    // It will take way to long to figure out how to call Event.stopObserving (exact call to observe params here)
    // So just zero out the event handlers, not going to get stray called now.

    dlRating.prototype.clearRating = function () {}	
    dlRating.prototype.hoverRating = function () {}
    dlRating.prototype.registerRating = function () {}

    $('dlRatingInput').value = rating;
}
