var UT_RATING_IMG = '/img/star_empty.gif';
var UT_RATING_IMG_HALF = '/img/star_half.gif';
var UT_RATING_IMG_BG = '/img/star_full.gif';

function UTRating(ratingElementId, maxStars, objectName, ratingMessageId)
{
        this.ratingElementId = ratingElementId;
        this.maxStars = maxStars;
        this.objectName = objectName;
        this.ratingMessageId = ratingMessageId

        this.starTimer = null;
        this.starCount = 0;

        // pre-fetch image
        (new Image()).src = UT_RATING_IMG;
        (new Image()).src = UT_RATING_IMG_HALF;

        function showStars(starNum, skipMessageUpdate) {
                this.clearStarTimer();
                this.greyStars();
                this.colorStars(starNum);
                if(!skipMessageUpdate)
                        this.setMessage(starNum);
        }

        function setMessage(starNum) {
                messages = new Array("Оцените:", "Отстой!", "Так себе", "Нормально", "Хорошо", "Супер!");
                //document.getElementById(this.ratingMessageId).innerHTML = messages[starNum];
        }

        function colorStars(starNum) {
            var stars_div=$('#'+this.ratingElementId);
			i = 0;
			$('img',stars_div).each(function(){
				i++;							 

                if (starNum >= i)
                {
                    $(this).attr('src',UT_RATING_IMG_BG);
                    //document.getElementById('star_' + i).src = UT_RATING_IMG_BG;
                }
                else if (starNum < i && starNum > (i-1))
                {
                    $(this).attr('src',UT_RATING_IMG_HALF);
                    //document.getElementById('star_' + i).src = UT_RATING_IMG_HALF;
                }
                else
                {
                    $(this).attr('src',UT_RATING_IMG);
                    //document.getElementById('star_' + i).src = UT_RATING_IMG;
                }
			})            
        }

        function greyStars() {
            var stars_div=$('#'+this.ratingElementId);			
			i = 0;
			$('img',stars_div).each(function(){
				$(this).attr('src',UT_RATING_IMG);
				i++;
			})    			
        }

        function setStars(starNum) {
                this.starCount = starNum;
                this.drawStars(starNum);
                var ratingElementId = this.ratingElementId;
        }


        function drawStars(starNum, skipMessageUpdate) {
                this.starCount=starNum;
                this.showStars(starNum, skipMessageUpdate);
        }

        function clearStars() {
                this.starTimer = setTimeout(this.objectName + ".resetStars()", 300);
        }

        function resetStars() {
                this.clearStarTimer();
                if (this.starCount)
                        this.drawStars(this.starCount);
                else
                        this.greyStars();
                this.setMessage(0);
        }

        function clearStarTimer() {
                if (this.starTimer) {
                        clearTimeout(this.starTimer);
                        this.starTimer = null;
                }
        }

        this.clearStars = clearStars;
        this.clearStarTimer = clearStarTimer;
        this.greyStars = greyStars;
        this.colorStars = colorStars;
        this.resetStars = resetStars;
        this.setStars = setStars;
        this.drawStars = drawStars;
        this.showStars = showStars;
        this.setMessage = setMessage;

}



