var Rating = Class.create(
{
    voteValue:0,
    voted: false,
    stars : [],
    defaultReviewText : null,
    userLogged: null,
    userRated: null,
    productId: null,

    initialize: function(productId, userLogged, userRated) {
        $$('.rating .star').each(function(element, index) {
            element.observe('click', this.vote.bind(this, index + 1));
            element.observe('mouseover', this.over.bind(this, index + 1));
            element.observe('mouseout', this.out.bind(this));
            this.stars.push(element);
        }.bind(this));

        this.defaultReviewText = $('productReviewText').innerHTML;
        this.userLogged = userLogged;
        this.userRated = userRated;
        this.productId = productId;
    },

    over:function(value) {
        if (this.voted) return;

        this.voteValue = value;
        for (var i = 0; i < value; i++) {
            this.stars[i].addClassName('over');
        }
        $('productReviewText').addClassName('voteValue');
        $('productReviewText').update(this.stars[value - 1].title);
        $('productAverageRating').hide();
    },

    out:function(event) {
//        if (!event) var event = window.event;
//        var reltg = $((event.relatedTarget) ? event.relatedTarget : event.toElement);
//		while (reltg.nodeName != 'BODY')
//		{
//			if(reltg.hasClassName('star'))
//			{
//				return;
//			}
//			reltg = $(reltg.parentNode);
//		}

        if (this.voted) return;

        this.voteValue = 0;
        $('productReviewText').removeClassName('voteValue');
        $('productReviewText').update(this.defaultReviewText);
        $('productAverageRating').show();
        this.clearReview();
    },

    clearReview: function()
    {
        this.stars.each(function(element) {
            element.removeClassName('over');
        });
    },

    vote:function(value, event) {
        Event.stop(event);

        if(!this.userLogged)
        {
            login.show();
            return;
        }

        if (this.userRated)
        {
            $('productReviewText').hide();
            $('productReviewError').show();
            $('productAverageRating').show();
            $('productReviewValue').hide();
            this.voted = true;
            this.clearReview();
            return;
        }

        if (this.voted) return;

        if (this.voteValue == value) {
            this.voted = true;

            var params = new Object();
            params['review[rating]'] = this.voteValue;
            params['review[productId]'] = this.productId;
            ajax('/catalog/index/save-review', params, 'post', this.onVoteSuccess.bind(this));
        }
    },

    onVoteSuccess: function(transport)
    {
        var data = transport.responseJSON;
        if(data.result == 'ok')
        {
            $('productReviewText').update(data.text);
            $('productReviewText').removeClassName('voteValue');
            $('productReviewText').hide();
            $('productReviewSuccess').show();
            $('productReviewValue').hide();
            this.clearReview();
            this.set(data.votes, data.totalRating);
            this.clearVoteSuccessText.bind(this).delay(2);
        }
    },

    clearVoteSuccessText: function()
    {
        Effect.Appear('productReviewText', {duration: 0.2});
        Effect.Fade('productReviewSuccess', {duration: 0.2});
    },

    set: function(votes, totalRating)
    {
        $('productAverageRating').show();
        $('productAverageRating').style.width = (totalRating / votes / 5) * 100 + '%';
    }
});

