﻿    Rating = function (autoPostBack,
                      callbackID,
                      clientID,
                      clientStateFieldID,
                      emptyStarCssClass,
                      filledStarCssClass,
                      waitingStarCssClass,
                      starCssClass,
                      maxRating,
                      rating,
                      tag,
                      id) {

        this._autoPostBack = autoPostBack;
        this._callbackID = callbackID;
        this._clientID = clientID;
        this._clientStateFieldID = clientStateFieldID;
        this._emptyStarCssClass = emptyStarCssClass;
        this._filledStarCssClass = filledStarCssClass;
        this._waitingStarCssClass = waitingStarCssClass;
        this._starCssClass = starCssClass;
        this._maxRatingValue = maxRating;
        this._ratingValue = rating;
        this._tag = tag;
        this._id = id;

        this._ratingDirection = 0;
        this._readOnly = false;
        this._stars = null;
        this._currentRating = rating;

        this._mouseOutHandler = Function.createDelegate(this, this._onMouseOut);
        this._starClickHandler = Function.createDelegate(this, this._onStarClick);
        this._starMouseOverHandler = Function.createDelegate(this, this._onStarMouseOver);

        this.initialize();

        this.hazmeclic = function () {

        }
    }

    Rating.prototype = {
        initialize: function () {
            this._stars = [];
            this._currentRating = $('#' + this._clientID + "_ClientState").val();
            this._ratingValue = this._currentRating;

            var elt = $('#' + this._clientID + '_A');
            for (var i = 1; i <= this._maxRatingValue; i++) {
                starElement = $('#' + this._clientID + '_Star_' + i);
                starElement.val(i);
                Array.add(this._stars, starElement);
                starElement.bind('click', this._starClickHandler)
                starElement.bind('mouseover', this._starMouseOverHandler)
            }
            elt.bind('mouseout', this._mouseOutHandler);
            this._update();
        },

        dispose: function () {
            var elt = $('#' + this._clientID + '_A');
            for (var i = 1; i <= this._maxRatingValue; i++) {
                starElement = $('#' + this._clientID + '_Star_' + i);
                starElement.val(i);
                Array.add(this._stars, starElement);
                starElement.unbind('click', this._starClickHandler)
                starElement.unbind('mouseover', this._starMouseOverHandler)
            }
            elt.unbind('mouseout', this._mouseOutHandler);
        },

        _onError: function (message, context) {
            alert(message);
        },

        _receiveServerData: function (arg, context) {
        },

        _onMouseOut: function (e) {
            if (this._readOnly) {
                return;
            }
            this._currentRating = this._ratingValue;
            this._update();
        },

        _onStarClick: function (e) {
            if (this._readOnly) {
                return;
            }
            if (this._ratingValue != this._currentRating) {
                this.set_Rating(this._currentRating);
            }
        },

        _onStarMouseOver: function (e) {

            if (this._readOnly) {
                return;
            }
            if (this._ratingDirection == 0) {
                this._currentRating = e.target.value;
            } else {
                this._currentRating = this._maxRatingValue + 1 - e.target.value;
            }
            this._update();
        },

        _waitingMode: function (activated) {
        },

        _update: function () {
            $('#' + this._clientID + "_A").attr('title', this._currentRating);

            for (var i = 0; i < this._maxRatingValue; i++) {
                var starElement;
                if (this._ratingDirection == 0) {
                    starElement = this._stars[i];
                } else {
                    starElement = this._stars[this._maxRatingValue - i - 1];
                }

                if (this._currentRating > i) {
                    starElement.removeClass(this._emptyStarCssClass);
                    starElement.addClass(this._filledStarCssClass);
                }
                else {
                    starElement.removeClass(this._filledStarCssClass);
                    starElement.addClass(this._emptyStarCssClass);
                }
            }
        },

        set_Rating: function (value) {
            if (this._ratingValue != value) {
                this._ratingValue = value;
                this._currentRating = value;

                if ((value < 0) || (value > this._maxRatingValue)) {
                    return;
                }

                this._update();

                $('#' + this._clientID + "_ClientState").val(this._ratingValue);

                var args = this._currentRating + ";" + this._tag;
                var id = this._callbackID;

                if (this._autoPostBack) {
                    __doPostBack(id, args);
                }
                else {
                    WebForm_DoCallback(id, args, this._receiveServerData, this, this._onError, true)
                }
            }
        }
    }
